I'm trying to print to the screen the elements selected when their checkbox is ticked and the submit button is clicked. However, only the last element selected is printed. Here is the code: <form method = "post" action = "<?php echo $PHP_SELF; ?>"> HTML: $sql2 = "SELECT * FROM `ClassStudent` WHERE CS_PU_AdNo = '" . $ad_no . "'"; include("../includes/php/connect2.php"); while ($row2 = mysql_fetch_assoc($rst2)) { $classes[] = $row2['CS_CL_ClassName']; } foreach ($classes as $a_class) { $sql = "SELECT Class.CL_Subject, TeacherClass.TC_ST_Code FROM Class, TeacherClass WHERE Class.CL_ClassName = TeacherClass.TC_CL_ClassName AND Class.CL_ClassName = '" . $a_class . "'"; include("../includes/php/connect.php"); while ($row = mysql_fetch_assoc($rst)) { $subject = $row['CL_Subject']; $teacher = $row['TC_ST_Code']; } echo "<th align = 'left' width = '200'>"; echo "<font face = 'arial'>" . $subject . "</font>"; echo "</th>"; echo "<th align = 'left' width = '100'>"; echo "<font face = 'arial'>" . $teacher . "</font>"; echo "</th>"; echo "<td>"; echo "<input type = 'checkbox' value = $teacher name = 'teacher' unchecked>"; echo "</td>"; echo "</tr>"; } echo "</table>"; echo "<input type = 'submit' name = 'add_recipient' value = 'Add recipent' />"; echo "<input type = 'hidden' name = 'submitted' value = 'true'>"; echo "</form>"; $teacher = $_POST['teacher']; echo $teacher; PHP: Any ideas?
Put the echo code inside the while loop i.e. while ($row = mysql_fetch_assoc($rst)) { $subject = $row['CL_Subject']; $teacher = $row['TC_ST_Code']; //Put your echo stuff here echo "<th align = 'left' width = '200'>"; echo "<font face = 'arial'>" . $subject . "</font>"; echo "</th>"; echo "<th align = 'left' width = '100'>"; echo "<font face = 'arial'>" . $teacher . "</font>"; echo "</th>"; echo "<td>"; echo "<input type = 'checkbox' value = $teacher name = 'teacher' unchecked>"; echo "</td>"; } echo "</tr>"; Code (markup):
You can fix the html, i just copied and pasted it as an example. The problem was you were only outputting the LAST row of the query, as you looped round all rows setting them to the same variable, you need to output HTML each iteration
I have changed my code to the following, but it is still only printing the last element selected by the checkboxes: <form method = "post" action = "<?php echo $PHP_SELF; ?>"> HTML: <?php $sql2 = "SELECT * FROM `ClassStudent` WHERE CS_PU_AdNo = '" . $ad_no . "'"; include("../includes/php/connect2.php"); while ($row2 = mysql_fetch_assoc($rst2)) { $classes[] = $row2['CS_CL_ClassName']; } foreach ($classes as $a_class) { $sql = "SELECT Class.CL_Subject, TeacherClass.TC_ST_Code FROM Class, TeacherClass WHERE Class.CL_ClassName = TeacherClass.TC_CL_ClassName AND Class.CL_ClassName = '" . $a_class . "'"; include("../includes/php/connect.php"); while ($row = mysql_fetch_assoc($rst)) { $subject = $row['CL_Subject']; $teacher = $row['TC_ST_Code']; echo "<th align = 'left' width = '200'>"; echo "<font face = 'arial'>" . $subject . "</font>"; echo "</th>"; echo "<th align = 'left' width = '100'>"; echo "<font face = 'arial'>" . $teacher . "</font>"; echo "</th>"; echo "<td>"; echo "<input type = 'checkbox' value = $teacher name = 'select_recipient' unchecked>"; echo "</td>"; } } echo "</tr>"; echo "</table>"; echo "<input type = 'submit' name = 'add_recipient' value = 'Add recipent' />"; echo "</form>"; $teacher = $_POST['select_recipient']; echo $teacher; PHP: Any ideas?
Each checkbox is allocated to a username. If the usernames "IMT", "GOH" and "DJW" were checked and the submit button was clicked only "DJW" would be printed to the screen.
You need to add square brackets to the name of the checkboxes, so all selected values will arrive in an array to the PHP script: echo "<input type = 'checkbox' value = $teacher name = 'select_recipient[]' unchecked>"; PHP: Then, instead of just echoing $teacher, you'll have to loop through the array: $teacherArray = $_POST['teacher']; foreach ($teacherArray as $teacher) { echo $teacher } PHP:
The code now prints out the correct elements selected in the checkboxes. However, I want to pass these to another script, which inserts them into a database. How would I go about sending them to the other script + getting them out of the array as it only prints "Array" to the screen at the moment.
To get them 'out of the array', just treat it as any other array. I showed you how in my post. And you already loop through another array in your code, so you should know how to do that. To pass the array to another script, you might put it in a session.
I don't think you can send an array in the url. You might put the new script in the form action, that way all the form values will be sent directly to the new script. Otherwise, you'll have to save the array in a session. And yes, you use the foreach loop to get the values out of the array.
This is how the script looks now: <form method = "post" action = "<?php echo $PHP_SELF; ?>"> HTML: <?php $sql2 = "SELECT * FROM `ClassStudent` WHERE CS_PU_AdNo = '" . $ad_no . "'"; include("../includes/php/connect2.php"); while ($row2 = mysql_fetch_assoc($rst2)) { $classes[] = $row2['CS_CL_ClassName']; } foreach ($classes as $a_class) { $sql = "SELECT DISTINCT Class.CL_Subject, TeacherClass.TC_ST_Code FROM Class, TeacherClass WHERE Class.CL_ClassName = TeacherClass.TC_CL_ClassName AND Class.CL_ClassName = '" . $a_class . "'"; include("../includes/php/connect.php"); while ($row = mysql_fetch_assoc($rst)) { $subject = $row['CL_Subject']; $teacher = $row['TC_ST_Code']; echo "<th align = 'left' width = '200'>"; echo "<font face = 'arial'>" . $subject . "</font>"; echo "</th>"; echo "<th align = 'left' width = '100'>"; echo "<font face = 'arial'>" . $teacher . "</font>"; echo "</th>"; echo "<td>"; echo "<input type = 'checkbox' value = $teacher name = 'select_recipient[]' unchecked>"; echo "</td>"; echo "</tr>"; } } echo "<tr>"; echo "<td>"; echo "<br />"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<input type = 'submit' name = 'add_recipient' value = 'Add recipent' />"; if (isset($_POST['select_recipient'])) { ?> <input type = "hidden" name = "recipients" value = "<?php $all_recipients = $_POST['select_recipient']; ?>" /> <?php } echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<br />"; echo "</td>"; echo "</tr>"; echo "</table>"; echo "<table cellpadding = '0'>"; echo "<tr>"; echo "<th align = 'right'>"; echo "<font face = 'arial'>Recipients:</font> "; echo "</th>"; echo "<th align = 'left'><font face = 'arial'><font color = '#006600'>"; if ($_POST['select_recipient']) { $teacher_array = $_POST['select_recipient']; foreach ($teacher_array as $teacher) { echo $teacher; echo " "; } } echo "</font></th>"; echo "</tr>"; echo "</form>"; echo "<tr>"; echo "<td>"; echo "<br />"; echo "</td>"; echo "</tr>"; echo "<form method = 'post' action = 'send_teacher_msg.php?all_recipients=$teacher_array'>"; echo "<tr>"; echo "<th align = 'right'>"; echo "<font face = 'arial'>Subject:</font> "; echo "</th>"; echo "<td>"; echo "<input type = 'text' name = 'subject' size = '75' maxlength = '75' />"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<th align = 'right'>"; echo "<font face = 'arial'>Message:</font> "; echo "</th>"; echo "<td>"; echo "<textarea name = 'message' cols = '57' rows = '10'></textarea>"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "</td>"; echo "<td>"; echo "<input type = 'submit' name = 'submit' value = 'Send message' />"; echo "</td>"; echo "</tr>"; echo "</table>"; echo "</form>"; ?> PHP: As you can see I'm sending the array in the form action to the following script: $recipients = $_GET['all_recipients']; $subject = $_POST['subject']; $message = $_POST['message']; foreach ($recipients as $a_recipient) { echo $a_recipient; } PHP: I get the following error when the recieving script tries to run: Warning: Invalid argument supplied for foreach() in /home/tben2505/public_html/rms/v2/homepage/send_teacher_msg.php on line 31
Because you're putting the array in the url. The one thing I told you you can't do. Try putting it in a hidden field in the form. It might work. Otherwise put it in a session.
At the top of both scripts you add session_start(); PHP: In the first script add the following line: $_SESSION['all_recipients'] = $_POST['teacher']; PHP: In the second script substitute $recipients = $_GET['all_recipients']; PHP: with $recipients = $_SESSION['all_recipients']; PHP: