Loans - Web directory - Web Advertising - Online Advertising - Online Advertising

PDA

View Full Version : Why does this not work?.....


karl_murphy
Apr 28th 2008, 2:07 am
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>&nbsp;";
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 "&nbsp;&nbsp;&nbsp;";
}
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>&nbsp;";
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>&nbsp;";
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>";
?>

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?

SilkySmooth
Apr 28th 2008, 2:45 am
Line 138 of your scripts reads:


$teacher_array = $_POST['recipients'];


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; ?>" />


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>";


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>";


Then change line 138 to:


$teacher_array = $_POST['select_recipient'];


That should give you what you want, if I am understanding your code correctly. But then again it is 5:40am where I am :D

ggggqqqqihc
Apr 28th 2008, 2:46 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.

karl_murphy
Apr 28th 2008, 3:05 am
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?

ggggqqqqihc
Apr 28th 2008, 3:33 am
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.

karl_murphy
Apr 28th 2008, 3:35 am
How would I do this using a foreach loop?

ggggqqqqihc
Apr 28th 2008, 3:39 am
Sorry, I modified the last post.

karl_murphy
Apr 28th 2008, 3:43 am
any ideas?

xrvel
Apr 28th 2008, 3:54 am
If i not mistaken, here's your line 139

$teacher_array = $_POST['recipients'];
foreach ($teacher_array as $teacher)


$teacher_array is a string. You cannot use foreach on $teacher_array.

karl_murphy
Apr 28th 2008, 4:02 am
Thats working now thanks. I now need to know how to send the checkbox selections to another script.

karl_murphy
Apr 28th 2008, 9:12 am
Any ideas?