karl_murphy Posted February 6, 2008 Share Posted February 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/ Share on other sites More sharing options...
sasa Posted February 6, 2008 Share Posted February 6, 2008 use echo "<form method = 'post' action = 'send_individual_msg.php?recipients=".serialize($recipient)."'>"; and on submit page $recipients = unserialize($_GET['recipients']); Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459678 Share on other sites More sharing options...
haku Posted February 6, 2008 Share Posted February 6, 2008 Put your code in [code ] tags. Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459679 Share on other sites More sharing options...
karl_murphy Posted February 6, 2008 Author Share Posted February 6, 2008 I added in serialize and unserialize, but how do I print out each element of the array? Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459688 Share on other sites More sharing options...
aschk Posted February 6, 2008 Share Posted February 6, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459689 Share on other sites More sharing options...
sasa Posted February 6, 2008 Share Posted February 6, 2008 print_r($recipients); Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459702 Share on other sites More sharing options...
karl_murphy Posted February 6, 2008 Author Share Posted February 6, 2008 this doesn't seem to work, I just get a blank screen. Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459728 Share on other sites More sharing options...
sasa Posted February 6, 2008 Share Posted February 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459759 Share on other sites More sharing options...
karl_murphy Posted February 6, 2008 Author Share Posted February 6, 2008 Copied your scripts and only the subject and message print out ??? Quote Link to comment https://forums.phpfreaks.com/topic/89706-populating-an-array-with-values-selected-from-a-multi-list/#findComment-459766 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.