• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

Multi-Page Form

Hi Experts,

I have created a form for customers to register for an event. "Step One" (first page) will ask for contact info. etc, than "Step Two" will ask for more details for each registrant. So if the user wants to register 3 people they will be redirected to register3.php. This all works fine but I am not sure how to transfer the form variables to the next page.

Ex. The "company name" should autofill the form field "company name" on the second page if it is the same.

I have included the code below. How would I go about getting the variables to the second page?
<?php 
ob_start();
?>

XXXXXX

<?php

                 $error    = ''; // error message
                 $company     = ''; 
                 $phone1     = ''; 
                 $phone2     = ''; 
                 $phone3    = ''; 
                 $address    = ''; 
                 $same    = ''; 
                 $attendees    = ''; 
	
if(isset($_POST['send']))
            {
                 $company     = $_POST['company'];
				 $phone1    = $_POST['phone1'];				 
				 $phone2    = $_POST['phone2'];				 
				 $phone3    = $_POST['phone3'];				 
				 $address    = $_POST['address'];				 
				 $same    = $_POST['same'];				 
				 $attendees    = $_POST['attendees'];				 


				 if(trim($subject) == 'Choose One')
                {
                    $error = '<div class="errormsg">Please select a subject.</div>';
                }
				
				 if(trim($company) == '')
                {
                    $error = '<div class="errormsg">Please enter the company name.</div>';
                }				 
				
				
								else if(trim($phone1) == '')
                {
                    $error = '<div class="errormsg">Please enter your phone number!</div>';
                }

				
								else if(!preg_match("/^[0-9]{3,}$/",$phone1))
		        {
		        	$error = '<div class="errormsg">Please enter a valid phone number!</div>';
		        }


				else if(trim($phone2) == '')
                {
                    $error = '<div class="errormsg">Please enter your phone number!</div>';
                }

				
								else if(!preg_match("/^[0-9]{3,}$/",$phone2))
		        {
		        	$error = '<div class="errormsg">Please enter a valid phone number!</div>';
		        }
				
								else if(trim($phone3) == '')
                {
                    $error = '<div class="errormsg">Please enter your phone number!</div>';
                }

				
								else if(!preg_match("/^[0-9]{3,}$/",$phone3))
		        {
		        	$error = '<div class="errormsg">Please enter a valid phone number!</div>';
		        }


				 if(trim($address) == '')
                {
                    $error = '<div class="errormsg">Please enter the company address.</div>';
                }	
				
								 if(trim($same) == '')
                {
                    $error = '<div class="errormsg">Please indicate wether or not all attendees have the same phone number and address.</div>';
                }	
				
												 if(trim($attendees) == 'Choose One')
                {
                    $error = '<div class="errormsg">Please select the number of people you wish to register.</div>';
                }	
				

// CHANGE REDIRECT

	if ($attendees=="1 Person")
{
	header("Location: register1.php");
}

	if ($attendees=="2 People")
{
	header("Location: register2.php");
}

	if ($attendees=="3 People")
{
	header("Location: register3.php");
}

	if ($attendees=="4 People")
{
	header("Location: register4.php");
}

	if ($attendees=="5 People")
{
	header("Location: register5.php");
}
				
            }

            if(!isset($_POST['send']) || $error != '')
            {
				}
            ?>
            
            
            <!--Error Message-->
           <?=$error;?>
                  
            <form method="post" action="" enctype="multipart/form-data">
				<table cellpadding="2">

                      <tr><td valign="top"><label>Company Name:<span class="required">*</span>&nbsp;</label></td>
            			<td width="auto"><input name="company" class="form" type="text" id="company" size="40" value="<?=$company;?>" /></td></tr>
                        
                        <tr><td><label>Phone:<span class="required">*</span>&nbsp </label></td>
 				<td>
			<input id="phone1" name="phone1" size="3" maxlength="3" type="text" value="<?=$phone1;?>" /> -		
		
			<input id="phone2" name="phone2" size="3" maxlength="3" type="text" value="<?=$phone2;?>"/> -		

			<input id="phone3" name="phone3" size="4" maxlength="4" type="text" value="<?=$phone3;?>"/>		

	</td></tr>      
                        
            			 <tr><td valign="top"><label>Address:<span class="required">*</span>&nbsp; </label></td>
            			<td><input name="address" type="text" class="form" id="address" size="40" value="<?=$address;?>" /></td></tr>
                        
                         <tr><td valign="top"><label>Is the phone number and address the same for all attendees?<span class="required">*</span>&nbsp; </label></td>
            			<td><input type="radio" name="same" value="Yes" <?php echo (has_error && $_POST['same'] == "Yes")?("checked"):("");?>/>Yes
<input type="radio" name="same" value="No" <?php echo (has_error && $_POST['same'] == "No")?("checked"):("");?>/>No </td></tr>

                        
                        
        <tr><td><label>How many people do you want to register? <span class="required">*</span> </label></td>
            			<td>                      
	<select name="attendees" id="attendees">
  <option>Choose One</option>
  <option>1 Person</option>
  <option>2 People</option>
  <option>3 People</option>
  <option>4 People</option>
  <option>5 People</option>
  
	</select>
						</td></tr>
                        

                        	<!-- Submit Button. Do not edit-->
                <tr><td>&nbsp;</td><td align="right"> <input name="send" type="submit" class="button" id="send" value="Next" /></td></tr>
            </table>

            </form>

Open in new window

0
brettsky07
Asked:
brettsky07
  • 7
  • 6
  • 3
  • +1
1 Solution
 
jason987Commented:
Create a hidden input field like this:

<input type="hidden" name="num_attendees" value="1">

And pass that to the submit to form then you can handle the creation of the form dynamically based on the number of attendees.

That way you don't need register1,2,3,4,etc.php  You just use the same form and make an IF/Else evaluation of how many fields to output.
0
 
brettsky07Author Commented:
That still puts me in the position of not knowing how to get the variable carried over to the second page though... and it is going to be a really long form so for simplicity I was thinking it would be best to keep them on separate pages. I'm fairly new to php form handling so the simpler the better...
0
 
russianryebreadCommented:
You can use $_SESSION vars to persist across different pages/refreshes, though Jason987 is right, better coding practice would be to put your form in the logic of this page, and then your additional details would be available within its own ($_POST) context.  So, basically, your different registration pages could be put within the parent page, and when you click submit, you would have all the POST var info to work with, where putting a header redirect clears all that info unless it is stored in the session.  Not sure if that makes sense, so if you want me to clarify, just ask!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jason987Commented:
You need to change your action to post to the new pages:

<form method="post" action="" enctype="multipart/form-data">

To

<form method="post" action="register5.php" enctype="multipart/form-data">

Etc.

And trust me in the long run yuo'll be much better of knowing how to get redundant things like that done on a single php page and functions than spreading it around.  (more places for things to break)


0
 
brettsky07Author Commented:
I can defiantly see the benefit to having it done that way I just don't understand how to go about it. Are there any tutorials or online resources that might help explain? I would rather stay away from sessions as they info. is a bit sensitive. How would I have this:

<form method="post" action="register5.php" enctype="multipart/form-data">

Sent to multiple pages? Or how would:

<input type="hidden" name="num_attendees" value="1">

be altered if they need to register 3 people?
0
 
russianryebreadCommented:
I'm assuming the information for each attendee will be the same, so the form structure for each person only has to be programmed once, then you can loop through the number of attendees and print the extra fields out as many times as needed.  It's a hard concept to grasp when coming from completely static HTML coding, but it's part of the joy of a dynamic language like PHP.  Repetitive tasks can all be automated.

Here's a really crude example of what you can do:
<?php

for($i=0;$i<$numberOfAttendees;$i++){
    echo 'Attendee #'.$i.' <input type="text" name="attendee'.$i.'" id="attendee'.$i.'" />';
}

?>

Open in new window

0
 
brettsky07Author Commented:
The form info. for each attendee is actually different. Each person is given the same list of sessions to choose from but their choices may all be different. The form needs to email us something like:

Attendee 1
Name:
Address:
Thursday Session:
Friday Session:

Attendee 2
Name:
Address:
Thursday Session:
Friday Session:
0
 
jason987Commented:
The data is not being sent to "resgister5.php" etc because you are sendnig it to the same page but the redirecting away from that page.  That's kind of what we were saying about trying to keep it compact.  Instead of just dealing with the submitted data on that page you are now faced with a delime you shouldn't need to..how to auto "post" the data while redirecting without using sessions....

...instead you could just remove the "// CHANGE REDIRECT" portion of your code to create a another form based on how many attendees they selected using something like russian posted above:
0
 
brettsky07Author Commented:
That makes total sense I am just stuck on the actual implementation. I agree that way would be much more effective I am just not sure how to code it as I have not done it that way before and each attendee could have different information...
0
 
jason987Commented:
Yes the options presented will always be the same just the number of items changes so a singular function is perfect for that.  Just expand the for loop he posted above to include all of that and wrap it up into another form liek this:

            <form method="post" action="" enctype="multipart/form-data">
				<table cellpadding="2">

                 <?php

                for($i=0;$i<$numberOfAttendees;$i++){
                    echo 'Attendee #'.$i.' <input type="text" name="attendee'.$i.'" id="attendee'.$i.'" />';
                    echo 'Address #'.$i.' <input type="text" name="address'.$i.'" id="address'.$i.'" />';
                }

                ?>


                        	<!-- Submit Button. Do not edit-->
                <tr><td>&nbsp;</td><td align="right"> <input name="send" type="submit" class="button" id="send" value="Next" /></td></tr>
            </table>
            

Open in new window


Then have another IF ISSET check for attendee or set an hidden post option and then deal with the attendees portion.
0
 
jason987Commented:
I'll try to mock something up if russian doesn't beet me to it  ;)
0
 
brettsky07Author Commented:
Thanks for your help - I am beyond over my head on this one!
0
 
jason987Commented:
Here you go, quick and dirty and definitely not the best way to do it but much better than creating different pages for every attendee:

<?php 
ob_start();
?>




            <form method="post" action="" enctype="multipart/form-data">
				<table cellpadding="2">

                      <tr><td valign="top"><label>Company Name:<span class="required">*</span>&nbsp;</label></td>
            			<td width="auto"><input name="company" class="form" type="text" id="company" size="40" value="<?=$company;?>" /></td></tr>

                        <tr><td><label>Phone:<span class="required">*</span>&nbsp </label></td>
 				<td>
			<input id="phone1" name="phone1" size="3" maxlength="3" type="text" value="<?=$phone1;?>" /> -

			<input id="phone2" name="phone2" size="3" maxlength="3" type="text" value="<?=$phone2;?>"/> -

			<input id="phone3" name="phone3" size="4" maxlength="4" type="text" value="<?=$phone3;?>"/>

	</td></tr>

            			 <tr><td valign="top"><label>Address:<span class="required">*</span>&nbsp; </label></td>
            			<td><input name="address" type="text" class="form" id="address" size="40" value="<?=$address;?>" /></td></tr>

                         <tr><td valign="top"><label>Is the phone number and address the same for all attendees?<span class="required">*</span>&nbsp; </label></td>
            			<td><input type="radio" name="same" value="Yes" <?php echo (has_error && $_POST['same'] == "Yes")?("checked"):("");?>/>Yes
<input type="radio" name="same" value="No" <?php echo (has_error && $_POST['same'] == "No")?("checked"):("");?>/>No </td></tr>



        <tr><td><label>How many people do you want to register? <span class="required">*</span> </label></td>
            			<td>
	<select name="attendees" id="attendees">
  <option>Choose One</option>
  <option>1 Person</option>
  <option>2 People</option>
  <option>3 People</option>
  <option>4 People</option>
  <option>5 People</option>

	</select>
						</td></tr>


                        	<!-- Submit Button. Do not edit-->
                <tr><td>&nbsp;</td><td align="right"> <input name="send" type="submit" class="button" id="send" value="Next" /></td></tr>
            </table>

            </form>



<?php

                 $error    = ''; // error message
                 $company     = '';
                 $phone1     = '';
                 $phone2     = '';
                 $phone3    = '';
                 $address    = '';
                 $same    = '';
                 $attendees    = '';

if(isset($_POST['attendee1']))
{
    //deal with attendee data here
}

if(isset($_POST['send']))
            {
                 $company     = $_POST['company'];
				 $phone1    = $_POST['phone1'];
				 $phone2    = $_POST['phone2'];
				 $phone3    = $_POST['phone3'];
				 $address    = $_POST['address'];
				 $same    = $_POST['same'];
				 $attendees    = $_POST['attendees'];


				 if(trim($subject) == 'Choose One')
                {
                    $error = '<div class="errormsg">Please select a subject.</div>';
                }

				 if(trim($company) == '')
                {
                    $error = '<div class="errormsg">Please enter the company name.</div>';
                }


								else if(trim($phone1) == '')
                {
                    $error = '<div class="errormsg">Please enter your phone number!</div>';
                }


								else if(!preg_match("/^[0-9]{3,}$/",$phone1))
		        {
		        	$error = '<div class="errormsg">Please enter a valid phone number!</div>';
		        }


				else if(trim($phone2) == '')
                {
                    $error = '<div class="errormsg">Please enter your phone number!</div>';
                }


								else if(!preg_match("/^[0-9]{3,}$/",$phone2))
		        {
		        	$error = '<div class="errormsg">Please enter a valid phone number!</div>';
		        }

								else if(trim($phone3) == '')
                {
                    $error = '<div class="errormsg">Please enter your phone number!</div>';
                }


								else if(!preg_match("/^[0-9]{3,}$/",$phone3))
		        {
		        	$error = '<div class="errormsg">Please enter a valid phone number!</div>';
		        }


				 if(trim($address) == '')
                {
                    $error = '<div class="errormsg">Please enter the company address.</div>';
                }

								 if(trim($same) == '')
                {
                    $error = '<div class="errormsg">Please indicate wether or not all attendees have the same phone number and address.</div>';
                }

												 if(trim($attendees) == 'Choose One')
                {
                    $error = '<div class="errormsg">Please select the number of people you wish to register.</div>';
                }



        echo'<form method="post" action="" enctype="multipart/form-data">
				<table cellpadding="2">';


        for($i=0;$i<$attendees;$i++){
            echo '<tr><td>Attendee #'.$i.' </td><td><input type="text" name="attendee'.$i.'" id="attendee'.$i.'" /></td></tr>';
            echo '<tr><td>Address'.$i.' </td><td><input type="text" name="address'.$i.'" id="address'.$i.'" /></td></tr>';


        }
        echo '                       	<!-- Submit Button. Do not edit-->
                <tr><td>&nbsp;</td><td align="right"> <input name="send" type="submit" class="button" id="send" value="Submit" /></td></tr>
            </table>

            </form>';



// CHANGE REDIRECT
/*
	if ($attendees=="1 Person")
{
	header("Location: register1.php");
}

	if ($attendees=="2 People")
{
	header("Location: register2.php");
}

	if ($attendees=="3 People")
{
	header("Location: register3.php");
}

	if ($attendees=="4 People")
{
	header("Location: register4.php");
}

	if ($attendees=="5 People")
{
	header("Location: register5.php");
}*/

 }

            if(!isset($_POST['send']) || $error != '')
            {
				}



?>


            <!--Error Message-->
           <?=$error;?>

Open in new window

0
 
brettsky07Author Commented:
This seems to be working but I dont understand how the information would be emailed to our account if the variables are eventually the same for attendee 1-5.... If I use something like

      $msg     .= "\r\nFri. 2010: " . $fri;
                    mail($to, $subject, $msg, xxxx);
            ?>

How will each attendees information appear differently? I cant have #1 ect. after each feild. This is getting to be a pretty complex form with alot of error checking and validation... I really need a very basic 'how to'. Any suggestions?
0
 
Ray PaseurCommented:
I think you might want to step back from this particular problem and familiarize yourself with how PHP can be used to work with HTML forms.  This tutorial can help.
http://us3.php.net/manual/en/tutorial.forms.php

After you have come to grips with that, please consider getting this book. Give yourself a month to work through the examples and you will be light years ahead in your understanding of PHP.  In particular, you will understand not only the problems you faced, but also the many good suggestions you are getting here at EE.
http://www.sitepoint.com/books/phpmysql4/

It's not an instant solution, but it's the right thing for you to do if you are determined to try to write PHP code.  Best of luck, ~Ray
0
 
russianryebreadCommented:
Ray is very right, and I completely second his words of wisdom.  However, if you have to have this done in a very quick and dirty way, printing the POST array out into your browser window might give you a conceptual grasp on where your data is going, and how you can get ahold of it.

Your POST data is just stored in an array, called $_POST.  So if you put this line (print_r($_POST);) in the top of the page right below your ob_start(), and then fill your form out, you will see the contents of POST, and you can deal with the data in whatever way you want.  You can statically grab your data by calling the individual elements of the array ( ex: echo($_POST['attendee1']) ) will print out the contents of the 2nd attendee's name.  Once you grasp that concept, then automating it by looping through the array is much more scalable.

Again, Ray's advice is the best, but if you HAVE to get this project done, then printing out the POST array so that you can see your data as the server sees it might help you understand what the form is really doing behind the scenes.

Hope that helps!
0
 
brettsky07Author Commented:
Thanks Ray_Paseur i will definatly check out that tutorial - russianryebread is right though. This is a deadline of "yesterday" so I might have to resort to the easiest / not best way of doing it. I will work on it a bit tonight and see what I come up with. Thanks for all the suggestions.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now