fohanlon Posted February 27, 2006 Share Posted February 27, 2006 Hi I wonder could you help.I have a form on a page that has a number of drop downs (gender, date of birth with a drop down for day, month and year) being some.On the previous page the user selects the number of persons and this value determines the number of times a for loop executes to display the correct number of gender, date of birth options etc. This variable is called $num_persons.Snippets of Code showing gender form select statement (the alert is only for testing to see the valus of $gender php variable):<?php for($np = 0; $np < $num_persons; $np++) { ?><select name="<?php echo "gender".$np; ?>" class="formitems" id="gender" value="<?php echo $gender = "gender".$np; ?>"> <?php echo "<script>alert('" . $gender . "')</script>"; if($gender == "male") { ?> <option value="male" selected>Male</option> <option value="female">Female</option> <?php } else { ?> <option value="female" selected>Female</option> <option value="male">Male</option> <?php } ?> </select><?php } ?>I also have a link on the bottom of the page called Add More People that refreshes the page and updates the value of $num_persons by 1. <a href="health2.php?c=1&num_persons=<?php echo $num_persons; ?>">Add More People</a>However, my request is as follows. I need to be able to keep the values entered in the drop downs for gender, DOB etc when the page refreshes after a user has clicked the Add More People link.You will see from the select code that I name each dropdown using the word "gender2 appended with the values of the variable $np i.e. gender0, gender1, etc.My problem is I odnlt know how to assign the value of this select i.e. male or female to php varibale called $gender so that I can use this php variable to check and hold previous chosen value after page has been submitted.Any comments or help would be greatly appreciated.Many thanks. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 27, 2006 Share Posted February 27, 2006 It's much easier to use arrays to keep track of multiple form input values. Here's a modification of your script that does what you wanted:[code]<?php$num_persons = (isset($_POST['num_persons']))?$_POST['num_persons']:5;if (isset($_POST['submit']) && $_POST['submit'] == "Add More People") $num_persons++;$tmp = array();$tmp[] = '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';$tmp[] = '<input type="hidden" name="num_persons" value="' . $num_persons . '">';for($np = 0; $np < $num_persons; $np++) { $male_selected = ''; $female_selected = ''; $tmp[] = '<select name="gender[' . $np . ']" class="formitems" id="gender">'; if(isset($_POST['gender'][$np]) && $_POST['gender'][$np] == 'male') $male_selected = 'selected'; if(isset($_POST['gender'][$np]) && $_POST['gender'][$np] == 'female') $female_selected = 'selected'; $tmp[] = '<option value="male" '. $male_selected . '>Male</option>'; $tmp[] = '<option value="female" '. $female_selected . '>Female</option>'; $tmp[] = '</select><br>';}$tmp[] = '<br><input type="submit" value="Add More People" name="submit"> <input type="submit" value="Submit Data" name="submit">';$tmp[] = '</form>';echo implode("\n",$tmp);?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
fohanlon Posted March 7, 2006 Author Share Posted March 7, 2006 KenMany thanks for this section of code.I wonder could I ask you, how I can retrieve these values when the page in submitted to another php page. I notice you have the page going to self but what if I want it to got to another page called test.php and retrieve then values from the select choices on this test.php page. Will the tmp array be avialable globaly?Many thanks,Fergal.[!--quoteo(post=349928:date=Feb 27 2006, 01:54 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Feb 27 2006, 01:54 PM) [snapback]349928[/snapback][/div][div class=\'quotemain\'][!--quotec--]It's much easier to use arrays to keep track of multiple form input values. Here's a modification of your script that does what you wanted:[code]<?php$num_persons = (isset($_POST['num_persons']))?$_POST['num_persons']:5;if (isset($_POST['submit']) && $_POST['submit'] == "Add More People") $num_persons++;$tmp = array();$tmp[] = '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';$tmp[] = '<input type="hidden" name="num_persons" value="' . $num_persons . '">';for($np = 0; $np < $num_persons; $np++) { $male_selected = ''; $female_selected = ''; $tmp[] = '<select name="gender[' . $np . ']" class="formitems" id="gender">'; if(isset($_POST['gender'][$np]) && $_POST['gender'][$np] == 'male') $male_selected = 'selected'; if(isset($_POST['gender'][$np]) && $_POST['gender'][$np] == 'female') $female_selected = 'selected'; $tmp[] = '<option value="male" '. $male_selected . '>Male</option>'; $tmp[] = '<option value="female" '. $female_selected . '>Female</option>'; $tmp[] = '</select><br>';}$tmp[] = '<br><input type="submit" value="Add More People" name="submit"> <input type="submit" value="Submit Data" name="submit">';$tmp[] = '</form>';echo implode("\n",$tmp);?>[/code]Ken[/quote] Quote Link to comment 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.