snailguy Posted May 27, 2010 Share Posted May 27, 2010 I have a working php page with an hmtl form where five names can be entered into five different text boxes. Like so: echo "<form name=\"formname\" action=\"form_review.php\" method=\"post\" enctype=\"multipart/form-data\"> <p>"; for ($i=0;$i<=4;$i++) { echo "First Name<input type=\"text\" name=\"firstnameT[".$i."]\" value=\"".($firstnameT[$i])."\" maxlength=\"15\"> "; echo "Last Name<input type=\"text\" name=\"lastnameT[".$i."]\" value=\"".($lastnameT[$i])."\" maxlength=\"15\"><br>"; } On the page form_review.php: //Convert POST variables to PHP if (isset($_POST['firstnameT[]'])) {$firstnameT[]=$_POST['firstnameT[]'];} else { $firstnameT[] = '';} if (isset($_POST['lastnameT[]'])) {$lastnameT[]=$_POST['lastnameT[]'];} else { $lastnameT[] = '';} //Display the submitted data for ($i=0;$i<=4;$i++) { echo "<p> $firstnameT[$i]." ".$lastnameT[$i]."<br>"; } echo "</p>"; So the problem I'm having is that I want to use <select><option> instead of <input type="text"> so users can select five names from five separate pull-down selectors, and display the selections on the next page, like so: $query = "SELECT users.user_id, users.first_name, users.last_name, users.city FROM users"; $result = mysql_query($query) OR DIE("Your query failed: " .mysql_errno() . " " . mysql_error()); echo "<form name=\"formname\" action=\"form_review.php\" method=\"post\" enctype=\"multipart/form-data\"> for ($i=0;$i<=4;$i++) { echo "<p><select name=\"volunteer[".$i."]\">"; echo "<option value=\"\">Choose a Volunteer</option>"; while($row = mysql_fetch_array($result)) { $vol_id = $row['user_id']; $first_name = $row['first_name']; $last_name = $row['last_name']; $city = $row['city']; echo "<option value=\"".$vol_id."\">".$last_name.", ".$first_name." (".$city.")</option>"; } //close WHILE echo "</select></p>"; mysql_data_seek($result,0); //reset the pointer in the result array for each FOR loop } //close FOR The above section of code works fine = the form displays properly, each pulldown has the full list of users, etc. I have even confirmed that the underlying html has unique values for each pulldown choice, and that each pulldown choice has a unique name like: <select name=volunteer[0]><option value="1">Last, First (city)</option><option value='2'>Last2, First2 (City2)</option></select> <select name=volunteer[1]><option value="1">Last, First (city)</option><option value='2'>Last2, First2 (City2)</option></select> But when I submit to review page, I can't get the selected names to display. Here's the pertinent code from "form_review.php": if (isset($_POST['volunteer[]'])) {$volunteer[]=$_POST['volunteer[]'];} else { $volunteer[] = '';} for ($i=0;$i<=4;$i++) { echo "Volunteer #".$i.": ".$volunteer[$i]."<br>"; } echo "</p>"; Unfortunately, nothing appears. I'm expecting to get the selected option value following "Volunteer #:", but I'm not. I've counted rows in the array passed to the second page and I consistently get numrows=1, and the value in that resulting array is obviously "". After five hours of trying a mind-numbing number of variations, I can't wrap my head around the solution. Is there something special/different about passing arrays generated from <select><option> versus arrays generated from <input type=text>??? TIA! snailguy Link to comment https://forums.phpfreaks.com/topic/203154-trouble-passing-html-form-selection-to-php-array-on-second-page/ Share on other sites More sharing options...
Pikachu2000 Posted May 27, 2010 Share Posted May 27, 2010 What does print_r($_POST) show you? Link to comment https://forums.phpfreaks.com/topic/203154-trouble-passing-html-form-selection-to-php-array-on-second-page/#findComment-1064424 Share on other sites More sharing options...
snailguy Posted May 28, 2010 Author Share Posted May 28, 2010 [volunteer] => Array ( [0] => 1 [1] => 12 [2] => 2 [3] => 11 [4] => 3 That's good news. The data is arriving at the second page intact. Perhaps a problem with the POST to PHP variable conversion? Link to comment https://forums.phpfreaks.com/topic/203154-trouble-passing-html-form-selection-to-php-array-on-second-page/#findComment-1064426 Share on other sites More sharing options...
snailguy Posted May 28, 2010 Author Share Posted May 28, 2010 This code: echo "<p>ID's of trained volunteers:</p> <p>"; echo "volunteer array: "; print_r($_POST[volunteer]); echo "<br>trained = ".$trained."<br>"; $j = $trained-1; for ($i=0;$i<=$j;$i++) { echo "Volunteer #".$i.": ".$volunteer[$i]."<br>"; } echo "</p>"; Results in the following display: ID's of trained volunteers: volunteer array: Array ( [0] => 1 [1] => 12 [2] => 2 [3] => 11 [4] => 3 ) trained = 5 Volunteer #0: Volunteer #1: Volunteer #2: Volunteer #3: Volunteer #4: Link to comment https://forums.phpfreaks.com/topic/203154-trouble-passing-html-form-selection-to-php-array-on-second-page/#findComment-1064431 Share on other sites More sharing options...
greatstar00 Posted May 28, 2010 Share Posted May 28, 2010 $volunteer[]=$_POST['volunteer[]']; should be $volunteer=&$_POST['volunteer[]']; Link to comment https://forums.phpfreaks.com/topic/203154-trouble-passing-html-form-selection-to-php-array-on-second-page/#findComment-1064466 Share on other sites More sharing options...
kenrbnsn Posted May 28, 2010 Share Posted May 28, 2010 These two lines are wrong: <?php if (isset($_POST['volunteer[]'])) {$volunteer[]=$_POST['volunteer[]'];} else { $volunteer[] = '';} ?> Change them to <?php $volunteer = (isset($_POST['volunteer']))?$_POST['volunteer']:array(); ?> Ken Link to comment https://forums.phpfreaks.com/topic/203154-trouble-passing-html-form-selection-to-php-array-on-second-page/#findComment-1064472 Share on other sites More sharing options...
snailguy Posted May 28, 2010 Author Share Posted May 28, 2010 Thanks! After a nights sleep and a consult with a friend, I figured it out over breakfast. Here is my solution: if (isset($_POST['volunteer'])) {$volunteer = $_POST['volunteer'];} else {$volunteer = '';} Jeff Link to comment https://forums.phpfreaks.com/topic/203154-trouble-passing-html-form-selection-to-php-array-on-second-page/#findComment-1064614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.