A small problem......

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

  1. #1
    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?
     
    karl_murphy, Apr 16, 2008 IP
  2. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #2
    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):
     
    m0nkeymafia, Apr 16, 2008 IP
  3. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    I don't think that will work. Won't that create a submit button next to each element?
     
    karl_murphy, Apr 16, 2008 IP
  4. m0nkeymafia

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #4
    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
     
    m0nkeymafia, Apr 16, 2008 IP
  5. karl_murphy

    karl_murphy Member

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

    m0nkeymafia Well-Known Member

    Messages:
    399
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Show us what is actually output by the code, It'd help in debugging it
     
    m0nkeymafia, Apr 17, 2008 IP
  7. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    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.
     
    karl_murphy, Apr 17, 2008 IP
  8. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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:
     
    CreativeClans, Apr 17, 2008 IP
  9. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    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.
     
    karl_murphy, Apr 17, 2008 IP
  10. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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.
     
    CreativeClans, Apr 17, 2008 IP
  11. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    Can I just send over $teaherArray + get out the elements using to foreach loop?
     
    karl_murphy, Apr 17, 2008 IP
  12. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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.
     
    CreativeClans, Apr 17, 2008 IP
  13. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #13
    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>&nbsp;";
    				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 "&nbsp;&nbsp;&nbsp;";
    					}
    				}
    				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>&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:
    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
     
    karl_murphy, Apr 17, 2008 IP
  14. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #14
    Any ideas anyone?
     
    karl_murphy, Apr 17, 2008 IP
  15. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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.
     
    CreativeClans, Apr 17, 2008 IP
  16. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #16
    How would I go about putting it into a session?
     
    karl_murphy, Apr 17, 2008 IP
  17. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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:
     
    CreativeClans, Apr 17, 2008 IP