Hi, The following script selects a set of usernames from a database and prints them to the screen with a checkbox. The user can the select individual usernames and when they click the "Add recipient" button these our printed to the screen. The code is as follows: <?php session_start(); if($_SESSION['login_status'] != "success") { $sql = "INSERT INTO `RMS_Access` ( `ACC_ID` , `ACC_User` , `ACC_IP` , `ACC_Timestamp` , `ACC_Page` , `ACC_Type` ) VALUES (NULL , '" . $_SESSION['username'] . "', '" . $_SERVER['REMOTE_ADDR'] . "', NOW( ) , '" . $_SERVER['SCRIPT_NAME'] . "?class=" . $_GET['class'] . "', 'UNAUTH_ACCESS')"; include("../includes/php/connect.php"); Header("Location: http://www.thedatacentre.org/rms/v2/homepage/logout.php"); } $sql = "INSERT INTO `RMS_Access` ( `ACC_ID` , `ACC_User` , `ACC_IP` , `ACC_Timestamp` , `ACC_Page` , `ACC_Type` ) VALUES (NULL , '" . $_SESSION['username'] . "', '" . $_SERVER['REMOTE_ADDR'] . "', NOW( ) , '" . $_SERVER['SCRIPT_NAME'] . "?class=" . $_GET['class'] . "', 'AUTH_ACCESS')"; include("../includes/php/connect.php"); $sql = "SELECT * FROM `Teacher` WHERE `ST_Username` = '" . $_SESSION['username'] . "'"; include("../includes/php/connect.php"); while ($row = mysql_fetch_assoc($rst)) { $current_teacher_code = $row['ST_Code']; } if ($_POST['select_recipient']) { $for_all_recipients = split(" - ",$_POST['recipients']); } $ad_no = $_GET['ad_no']; $sql = "SELECT * FROM `Student` WHERE PU_AdNo = '" . $ad_no . "'"; include("../includes/php/connect.php"); while ($row = mysql_fetch_assoc($rst)) { $forename = $row['PU_Forename']; $surname = $row['PU_Surname']; $tutor = $row['PU_TutorGroup']; $photo = $row['PU_Photopath']; $pu_year = $row['PU_Year']; } if($pu_year == "9") { $year = "09"; } else { $year = $pu_year; } echo "<table width = '600' cellpadding = '0'>"; echo "<tr>"; echo "<th width = '400' align = 'left'>"; echo "<font face = 'arial'>" . $surname . " " . $forename ."</font>"; echo "</th>"; echo "<th width = '100'>"; echo "<font face = 'arial'>" . $year . $tutor . "</font>"; echo "</th>"; echo "<td width = '100'>"; echo "<img src=\images/rms/sleepaz/" . $photo .">"; echo "</td>"; echo "</tr>"; echo "</table>"; echo "<table cellpadding = '0'>"; echo "<tr>"; ?> <form method = "post" action = "<?php echo $PHP_SELF; ?>"> <?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 . "' AND TeacherClass.TC_ST_Code != '" . $current_teacher_code . "'"; 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['recipients'] ." - ". $_POST['select_recipient']; echo $all_recipients; ?>" /> <?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['recipients']; foreach ($teacher_array as $teacher) { echo $teacher; echo " "; } echo ($_POST['select_recipient']); } 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=$all_recipients'>"; 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: However, when the "Add recipient" button is clicked the following error message is displayed: Warning: Invalid argument supplied for foreach() in /home/tben2505/public_html/rms/v2/homepage/teacher_msg.php on line 139 any ideas?
Line 138 of your scripts reads: $teacher_array = $_POST['recipients']; Code (markup): POST recipients is not an array, it is a string as per line 120: <input type = "hidden" name = "recipients" value = "<?php $all_recipients = $_POST['recipients'] ." - ". $_POST['select_recipient']; echo $all_recipients; ?>" /> Code (markup): I really have no idea what your trying to achieve with that hidden field. But if we take a look at your checkbox code... echo "<input type = 'checkbox' value = $teacher name = 'select_recipient' unchecked>"; Code (markup): You can declare that as an array which can then be passed to your PHP code, note the [] after select_recipient...: echo "<input type='checkbox' value=$teacher name='select_recipient[]' unchecked>"; Code (markup): Then change line 138 to: $teacher_array = $_POST['select_recipient']; Code (markup): That should give you what you want, if I am understanding your code correctly. But then again it is 5:40am where I am
Your code is too long... Warning informations has told you the problem happened in foreach statement. You should use "recipients[]" as the checkboxes' name instead of "recipients" in HTML code. So PHP will get an array, instead of a normal variable.
That works fine thanks. If you notice further down the script I am trying the send the array to another script.... echo "<form method = 'post' action = 'send_teacher_msg.php?all_recipients=$all_recipients'>"; Is this the correct method? Also, how would I print out this array in this script?
This methed is not correct. Because $all_recipients is an array. It must be transformed into a string so that it can be transer as a URL parameter. That's a hard work... Use echo '<pre>'; print_r($all_recipients); echo '</pre>'; to print out this array in a beautiful style.
If i not mistaken, here's your line 139 $teacher_array = $_POST['recipients']; foreach ($teacher_array as $teacher) PHP: $teacher_array is a string. You cannot use foreach on $teacher_array.