Jump to content

Populating an array with values selected from a multi list


karl_murphy

Recommended Posts

Hello.

 

I'm trying to create a basic messaging system and I'm having a few problems.  What I want to do is have the user select the recipients from a multilist box.  The recipients are inserted into an array.  I'm having problems retrieving the values from the array, which I need to manipulate the database.  The script for the form where the user selects the recipients and creates the message is as follows:

<?php
$sql2 = "SELECT DISTINCT * FROM `Teacher` ORDER BY ST_Surname ASC";
include("../includes/php/connect2.php");

echo "<form method = 'post' action = $PHP_SELF>";
echo "<table>";
	echo "<tr>";
		echo "<td>";
			echo "<strong>Please select recipients</strong>";
		echo "</td>";
		echo "<td>";
			echo "<select name = 'multiple_recipients[]' multiple = 'true' size = 6>";
				while ($row2 = mysql_fetch_assoc($rst2)) 
				{ 
					$individual = $row2['ST_Code'] . " - " . $row2['ST_Surname'] . " " . $row2['ST_Forename'];
					echo "<option value = $individual>$individual</option>";
				}
			echo "</select>";
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td>";
			echo "<input type = 'submit' name = 'submit' value = 'Select recipent(s)' />";
		echo "</td>";
	echo "</tr>";
echo "</table>";
echo "<br />";
if (is_null($_POST['submit']))
{}

else 
{
	$recipients = ($_POST['multiple_recipients']); 	
		echo "<table border = '1'>";
			echo "<tr>";
				echo "<td>";
					echo "<label><strong>Recipent(s)</strong></label>";
				echo "</td>";

				if (is_null($recipients))
				{
					echo "<td>You have not selected any recipients!</td>";
				}

				else	
				{
					foreach ($recipients as $a_recipient)
					{	
						echo "<td>".$a_recipient."</td>"; 
					}

				}
			echo "</tr>";
		echo "</table>";
}
echo "<table>";
echo "</form>";
echo "<br />";
echo "<form method = 'post' action = 'send_individual_msg.php?recipients=$recipients'>";
//echo "<input type = 'hidden' name = '$recipient' value = '$recipient' />";
echo "<table>";
	echo "<tr>";
		echo "<td>";
			echo "<strong>Subject</strong>";
		echo "</td>";
		echo "<td>";
			echo "<input type = 'text' name = 'subject' size = '56' maxlength = '56' />";
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td>";
			echo "<strong>Message body</strong>";
		echo "</td>";
		echo "<td>";
			echo "<textarea name = 'message' cols = '43' rows = '6'></textarea>";
		echo "</td>";
	echo "</tr>"; 
	echo "<tr>";
		echo "<td>";
			echo "<input type = 'submit' name = 'submit' value = 'Send message' />";
		echo "</td>";
	echo "</tr>";	
echo "</table>";
echo "</form>";			
?>

 

When the message is submitted the system navigates to the following script where I am trying to retrieve the array values:

<?php

$recipients = $_GET['recipients'];
$subject = $_POST['subject'];
$message = $_POST['message'];

/*$recipient_a = $recipients[1];
$recipient_b = $recipients[2];
if (!$recipient_b) 
	{ $recipient_b = "";}

echo $recipient_a;
echo $recipient_b;	*/
$r_a = $recipients[0];
echo $r_a;
$r_b = $recipients[1];
echo $r_b;
//echo $recipients;
echo $subject;
echo $message;
?>

Any help with this will be greatly appreciated.

I think your idea is ok, but you're attempting to hold the recipients in the wrong place.

You should hold them in the $_SESSION once you've got them from the first form.

Also instead of reposting to the same script try dividing up your PHP/html and pages, as it'll give you better clarity over what you're doing.

(n.b. having partial SQL outside of an include and then RELYING on that include to use your $sql2 variable can be dangerous and very difficult to debug later).

e.g.

 

==select_teachers.php==

<?php
// SQL Query.
$sql2 = "SELECT CONCAT(ST_Code,' - ',ST_Surname,' - ',ST_Forename) as 'name' 
FROM `Teacher` 
GROUP BY ST_Code 
ORDER BY ST_Surname ASC";

// Include <=== DANGEROUS
include("../includes/php/connect2.php");

// While loop
while($row2 = mysql_fetch_assoc($rst2)){
  $teachers[] = $row2['name'];
}
?>


<form method="post1.php">
<table>
  <tr>
    <td>
     <strong>Please select recipients</strong>
    </td>
    <td>
     <select name = 'multiple_recipients[]' multiple = 'true' size = 6>
       <?foreach($teachers as $teacher):?>
        <option value="<?=$teacher?>"><?=$teacher?></option>
       <?endforeach;?>
     </select>
    </td>
  </tr>
  <tr>
    <td>
     <input type = 'submit' name = 'submit' value = 'Select recipent(s)' />
    </td>
  </tr>
</table>
</form>

 

See how the code/html division here is better?

I'll leave you to divide up the rest but you see where it's going...

i ztry on my localserver

1st file

<?php
//$sql2 = "SELECT DISTINCT * FROM `Teacher` ORDER BY ST_Surname ASC";
//	include("../includes/php/connect2.php");

echo "<form method = 'post' action = $PHP_SELF>";
echo "<table>";
	echo "<tr>";
		echo "<td>";
			echo "<strong>Please select recipients</strong>";
		echo "</td>";
		echo "<td>";
			echo "<select name = 'multiple_recipients[]' multiple = 'true' size = 6>";
			for ($individual = 1; $individual <10; $individual++)echo "<option value = $individual>$individual</option>";
//					while ($row2 = mysql_fetch_assoc($rst2)) 
//					{ 
//						$individual = $row2['ST_Code'] . " - " . $row2['ST_Surname'] . " " . $row2['ST_Forename'];
//						echo "<option value = $individual>$individual</option>";
//					}
			echo "</select>";
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td>";
			echo "<input type = 'submit' name = 'submit' value = 'Select recipent(s)' />";
		echo "</td>";
	echo "</tr>";
echo "</table>";
echo "<br />";
if (!$_POST['submit'])
{}

else 
{
	$recipients = ($_POST['multiple_recipients']); 	
		echo "<table border = '1'>";
			echo "<tr>";
				echo "<td>";
					echo "<label><strong>Recipent(s)</strong></label>";
				echo "</td>";

				if (is_null($recipients))
				{
					echo "<td>You have not selected any recipients!</td>";
				}

				else	
				{
					foreach ($recipients as $a_recipient)
					{	
						echo "<td>".$a_recipient."</td>"; 
					}

				}
			echo "</tr>";
		echo "</table>";

echo "<table>";
echo "</form>";
echo "<br />";
echo "<form method = 'post' action = 'send_individual_msg.php?recipients=".serialize($recipients)."'>";
//echo "<input type = 'hidden' name = '$recipient' value = '$recipient' />";
echo "<table>";
	echo "<tr>";
		echo "<td>";
			echo "<strong>Subject</strong>";
		echo "</td>";
		echo "<td>";
			echo "<input type = 'text' name = 'subject' size = '56' maxlength = '56' />";
		echo "</td>";
	echo "</tr>";
	echo "<tr>";
		echo "<td>";
			echo "<strong>Message body</strong>";
		echo "</td>";
		echo "<td>";
			echo "<textarea name = 'message' cols = '43' rows = '6'></textarea>";
		echo "</td>";
	echo "</tr>"; 
	echo "<tr>";
		echo "<td>";
			echo "<input type = 'submit' name = 'submit' value = 'Send message' />";
		echo "</td>";
	echo "</tr>";	
echo "</table>";
echo "</form>";	
}		
?>

and 2nd file

<?php

$recipients = unserialize($_GET['recipients']);
$subject = $_POST['subject'];
$message = $_POST['message'];

/*$recipient_a = $recipients[1];
$recipient_b = $recipients[2];
if (!$recipient_b) 
	{ $recipient_b = "";}

echo $recipient_a;
echo $recipient_b;	*/
$r_a = $recipients[0];
echo $r_a,'<br />';
$r_b = $recipients[1];
echo $r_b,'<br />';
//echo $recipients;
echo $subject,'<br />';
echo $message;
?>

uotput of 2nd file is 2 first recipients + subject and message

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.