Why does this not work?.....

Discussion in 'PHP' started by karl_murphy, Apr 28, 2008.

  1. #1
    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>";
    ?>
    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?
     
    karl_murphy, Apr 28, 2008 IP
  2. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #2
    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 :D
     
    SilkySmooth, Apr 28, 2008 IP
    snowbird likes this.
  3. ggggqqqqihc

    ggggqqqqihc Peon

    Messages:
    92
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    ggggqqqqihc, Apr 28, 2008 IP
  4. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    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?
     
    karl_murphy, Apr 28, 2008 IP
  5. ggggqqqqihc

    ggggqqqqihc Peon

    Messages:
    92
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    ggggqqqqihc, Apr 28, 2008 IP
  6. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    How would I do this using a foreach loop?
     
    karl_murphy, Apr 28, 2008 IP
  7. ggggqqqqihc

    ggggqqqqihc Peon

    Messages:
    92
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry, I modified the last post.
     
    ggggqqqqihc, Apr 28, 2008 IP
  8. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    any ideas?
     
    karl_murphy, Apr 28, 2008 IP
  9. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #9
    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.
     
    xrvel, Apr 28, 2008 IP
  10. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #10
    Thats working now thanks. I now need to know how to send the checkbox selections to another script.
     
    karl_murphy, Apr 28, 2008 IP
  11. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    Any ideas?
     
    karl_murphy, Apr 28, 2008 IP