Jump to content

Drop Down Menu


tapupartforpres

Recommended Posts

Hello.  I didn't know if this is a .php question.  But I appreciate your comments.

 

I have a form that people will fill out for an event.  The person registering for the event has the option to have 8 guests.  So depending on how many guests there are, it will give them that many fields to fill out for each guest.  Hope that make sense.

 

Is there a way to do that, or will I have to create a seperate page for each quantity of guests.

 

This info is compiled and sent to paypal (and sent to the SQL file).

Link to comment
Share on other sites

I would recommend a little bit of nifty JavaScript. I would suggest having a form that displays only one text field, have a link or a button next to that text field so when users clicked it, you would append another text field and so on.

 

For the PHP side of things, I would suggest making these into an array. I.e.

 

<input type="text" name="guests[]" id="guests" />

 

Note the []! This will make "guests" an array.

 

For example:

 

echo $_POST['guests'][0]; // Guest 1
echo $_POST['guests'][1]; // Guest 2
// And so on..

Link to comment
Share on other sites

If I am understanding you correctly, you mean that you want to display the data from the form on the next page (if I am wrong please correct me as I may have misunderstood). This is extremely easy due to PHP's built in $_POST array. Each element of the forum has a 'name' attribute. (<input type="text" name="guests[]" />). This can be accessed by PHP like this (just as mentioned in a previous post):

 

<?php
echo $_POST['guests'][0]; 
echo $_POST['guests'][1]; 
?>

 

On the next page you can then make your form like this:

 

<input type="text" name="guests[]" id="guests" value="<?php echo $_POST['guests']['0'];?>" />

 

If the user inputs a guest called Phil then this will output a text box with Phil written in it.

 

A bit extra for you!

You also mentioned before that you would have multiple guests, and would therefore want the same number of input boxes. To do this you could use PHP's foreach control structure. E.g.:

 

<?php
foreach ($_POST['guests'] as $value) {

   echo '<input type="text" name="guests[]" id="guests" value="'.$value.'" /><br />';

}
?>

 

Be careful with this - you might choose to change the single and double quotes here - I have done this with singles for the echo statement and doubles for the html attributes.

 

value=" '.$value.' "

 

Take a look at that snippet of beofre. The red double quotes show the html attributes, the green single quotes end and start the string for the echo statement and the blue dots add on the next variable or string.

 

If as the previous poster suggested, you want to have the information available on multiple pages as they move around, you should put this data into a PHP session.

Link to comment
Share on other sites

Almost. 

 

What I wanted to do was to post the registration info to the next page.  Then depending on how many guests they had it would go to a page with that number of guests on it.  So if they selected 5 guests from the drop down menu, then their info would post to the next page and then there would be 5 places for the guest names.

 

Hope that clears it up.

Link to comment
Share on other sites

Maybe this will clear it up.  When the form is posted I want the name of the registration person filed out on the next page, then depending on the number of guests it will go to that coorisponding page.  (Guests 1 - 8).

 

So I have the standard info like "first_name", "last_name", etc.

 

It is proceeding to the next page, but it is filling out the "<?php echo $_POST['first_name']['0'];?>" /> in the value field.

 

I know I got this wrong, so if someone could help I would apprecite it.  Thanks

Link to comment
Share on other sites

  • 4 weeks later...

OK - best way to do this is probobly as follows

 

<?php

if(is_numeric($_POST['noofguests'])){

  $number = intval($_POST['noofguests']);
  if ($number<1){die('Number cannot be less than zero');}

  for ($k=1; $k<$number; $k++){
    echo '<input type="text" name="guests[]" /><br />';
  }

} else {die('Invalid Number of guests');}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.