Jump to content

Multiple Selections in a List


Nitestarz78

Recommended Posts

I have a PHP validator that checks all of my fields. It loops through until everything is correct and then the form submits. If something is not filled out it alerts you and you can fix the problem. This script also remembers the fields so the user doesnt have to re-type anything. 

 

I am having issues with trying to get it to save multiple selections from my list.Here is my current code which works for 1 selection only:

 

<?php

$Interests = !isset($_POST['Interests'])? NULL : $_POST['Interests'];

?>

<select name="Interests[]" multiple="multiple" size="4">

<option value="<?php echo $Interests;?>" selected><?php echo $Interests;?></option>

<option>1</option>

<option>2</option>

<option>3</option>

<option>4</option>

</select>

Link to comment
https://forums.phpfreaks.com/topic/137191-multiple-selections-in-a-list/
Share on other sites

<?php
$Interests = is_array($_POST['Interests']) ? $_POST['Interests'] : array();
?>
<select name="Interests[]" multiple="multiple" size="4">
<option value="1" <?php if(in_array('1',$Interests)) echo " SELECTED"; ?>>1</option>
<option value="2" <?php if(in_array('2',$Interests)) echo " SELECTED"; ?>>2</option>
<option value="3" <?php if(in_array('3',$Interests)) echo " SELECTED"; ?>>3</option>
<option value="4" <?php if(in_array('4',$Interests)) echo " SELECTED"; ?>>4</option>
</select>

 

or with a loop:

<?php
$Interests = is_array($_POST['Interests']) ? $_POST['Interests'] : array();
echo '<select name="Interests[]" multiple="multiple" size="4">';
for($n=1;$n < 5;$n++){
  printf('<option value="%d"%s>%d</option>',$n,(in_array($n,$Interests) ? " SELECTED" : ""),$n);
}
echo '</select>';
?>

 

Thank you! That works perfectly.

 

When I submit the form, however it doesnt want to print out the results on my thank you page. Currently I have:

 

<?
       print   "
    <strong>Interests:</strong> $Interests";
?>

 

How can it print out on the thank you page?

It doesnt seem to want to work. It doesnt print anything.

 

My form sends to a thank you page, and I pass the following:

 

if($valid == true) {
   mail_form("[email protected]", $Start_Date." Information Request ".rand(1,10000), Format_Form_Data_For_Mail($HTTP_POST_VARS), "-f $from");
 header( "Location: http://www.traininghott.com/onsite_submit.php?Contact_First_Name=$Contact_First_Name&Contact_Last_Name=$Contact_Last_Name&Contact_Title=$Contact_Title&Contact_Organization=$Contact_Organization&Contact_StreetAddress=$Contact_StreetAddress&Contact_City=$Contact_City&Contact_State=$Contact_State&Contact_ZipCode=$Contact_ZipCode&Country=$Country&Contact_WorkPhone=$Contact_WorkPhone&Contact_Email=$Contact_Email&where_found=$where_found&Interests=$Interests&time_frame=$time_frame&Number_Students=$Number_Students&Comments=$Comments" );
  } 

 

On the thank you page, I grab the variables:

 

<?php 
$Contact_First_Name = $_GET['Contact_First_Name'];
$Contact_Last_Name = $_GET['Contact_Last_Name'];
$Contact_Title = $_GET['Contact_Title'];
$Contact_Organization = $_GET['Contact_Organization'];
$Contact_StreetAddress = $_GET['Contact_StreetAddress'];
$Contact_City = $_GET['Contact_City'];
$Contact_State = $_GET['Contact_State'];
$Contact_ZipCode = $_GET['Contact_ZipCode'];
$Contact_WorkPhone = $_GET['Contact_WorkPhone'];
$Contact_Email = $_GET['Contact_Email'];
$Interests = $_GET['Interests'];
$Number_Students = $_GET['Number_Students'];
$time_frame = $_GET['time_frame'];
$Comments = $_GET['Comments'];
$Country = $_GET['Country'];
$date = $_GET['date'];
$where_found = $_GET['where_found'];
?>

 

And finally, print them:

 

   <?
       print   "
    <strong>Name:</strong> $Contact_First_Name $Contact_Last_Name<br>";
    
    if (!empty($Contact_Title)) {
       print   "<strong>Title:</strong> $Contact_Title<br>";
    }    
       print   "<strong>Company Name:</strong> $Contact_Organization<br>";
    
       if (!empty($Contact_StreetAddress)) {
       print   "<strong>Address:</strong> $Contact_StreetAddress<br>";
    }
       if (!empty($Contact_City) || !empty($Contact_State)) {    
       print   "<strong>City/State:</strong> $Contact_City, $Contact_State<br>";
    }
       if (!empty($Contact_ZipCode)) {    
       print   "<strong>Zip:</strong> $Contact_ZipCode<br>";
    }
       print   "<strong>Country:</strong> $Country<br>
    <strong>E-mail:</strong> $Contact_Email<br>";


     $Interests = is_array($_POST['Interests']) ? $_POST['Interests'] : array();
if(count($Interests)){
  print "<strong>Interests:</strong> ".implode(',',$Interests);
}
print  "
    <strong>Expected Number of Students:</strong> $Number_Students<br>
    <strong>Time Frame:</strong> $time_frame<br>
   <strong>Comments/Questions:</strong> $Comments";
?>

It is probably easier to store all that info a SESSION variable instead of GET variables like that.

 

But, since Interests is an array, you need to IMPLODE it to a string, then EXPLODE it again:

    header( "Location: http://www.traininghott.com/onsite_submit.php?Contact_First_Name=$Contact_First_Name&Contact_Last_Name=$Contact_Last_Name&Contact_Title=$Contact_Title&Contact_Organization=$Contact_Organization&Contact_StreetAddress=$Contact_StreetAddress&Contact_City=$Contact_City&Contact_State=$Contact_State&Contact_ZipCode=$Contact_ZipCode&Country=$Country&Contact_WorkPhone=$Contact_WorkPhone&Contact_Email=$Contact_Email&where_found=$where_found&Interests=".implode(',',$Interests)."&time_frame=$time_frame&Number_Students=$Number_Students&Comments=$Comments" );

 

$Interests = explode(',',$_GET['Interests']);

 

 //    $Interests = is_array($_POST['Interests']) ? $_POST['Interests'] : array(); //take out this line
if(count($Interests)){
  print "<strong>Interests:</strong> ".implode(',',$Interests);
}

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.