Jump to content


Photo

Retrieving values from form items for form validation when adding another line thru URL call


  • Please log in to reply
2 replies to this topic

#1 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 27 February 2006 - 09:32 AM

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.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 February 2006 - 06:54 PM

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:
<?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">&nbsp;<input type="submit" value="Submit Data" name="submit">';
$tmp[] = '</form>';
echo implode("\n",$tmp);
?>

Ken

#3 fohanlon

fohanlon
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 07 March 2006 - 01:45 PM

Ken

Many 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) View Post[/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:
<?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">&nbsp;<input type="submit" value="Submit Data" name="submit">';
$tmp[] = '</form>';
echo implode("\n",$tmp);
?>

Ken
[/quote]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users