0

I have form inside the while condition in echo because in while condition, some value comes from the database and according to each row, user perform an action in the form.

In the form, the checkbox is onclick of the checkbox, i want the PHP code to be executed in the same page which will update the database.

echo "<tr><td>Assign employee ID</td><td>Task Assign</td><td>Date of Assigning Task</td>
                <td>Given Task time</td><td>Time of given task</td><td colspan=3>Read message</td></tr></tr>";

                $message=mysql_query("select * from message where receiver_id='$myid' and is_read != 1 ");
                while($row=mysql_fetch_array($message))
                {
                $sender_id=$row["sender_id"];
                $receiver_id=$row["receiver_id"];
                $task_assign=$row["message"];
                $date=$row["date"];
                $time_of_given_task=$row["time"];

                echo "<tr>
                    <td>".$sender_id."</td><td>".$receiver_id."</td><td>".$task_assign."</td>
                    <td>".$date."</td><td>".$time_of_given_task."</td><td>
                        <form action='message_to_read.php?date=$date&time=$time_of_given_task&receive=$receiver_id' method=post>
                        <input type=checkbox value=checked name=check onclick=foo();></td>
                        </form>
                        </td>
                        </tr>";

                }

                echo "</table>";
                function foo()
                {
                $todaydate=$_REQUEST["date"];

                echo $todaydate;
                $time_of_given_task=$_REQUEST["time"];
                echo $time_of_given_task;
                $empid=$_REQUEST["receive"];
                echo $empid;
                    $test2=mysql_query("update message set is_read='$update' where receiver_id='$empid' and date='$todaydate' and time='$time_of_given_task' ");
                }
1
  • Your onclick=foo() is not going to call the PHP foo function... do you have a javascript foo() function? Your checkbox is also not setting a value. Commented Jun 4, 2013 at 19:22

1 Answer 1

1

Looks like an AJAX problem to me. If you are using JQuery simply use AJAX built in functionality.

$(document).on('change', '#YOUR_CHECKBOX', function() {
  if ($(this).is(':checked')) {
    //if checkbox is checked do ajax call
    $.ajax({
       type: 'post',
       url: URL_TO_YOUR_PHP_SCRIPT,
       data: { OPTIONAL : DATA },
       success: function(result) { }
    });
  }
});

change event would be better than click event in this situation

Sign up to request clarification or add additional context in comments.

3 Comments

This. Once the page loads on the client you can not run php on that page. You need to: call the server(php code), run the code, return response to client via some transport medium (socket/XHR/etc), and finally update page with the new data.
what are you talking about you can simply use AJAX to call PHP scripts without page reload. Please read about AJAX.
That's basically what I'm saying, just in step-by-step form. Sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.