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

If 'yes' autofill

Hi Experts. This is probably a fairly simple question but I havent had any luck. Once a user selects Yes from the radio buttons on my form (from Yes or No) I need the $company and $phone number fields they have filled in to (auto)fill in for each attendee after that. I am assuming it would just be an if statement in my validation but can't seem to get it working. My radio buttons are:

<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
0
brettsky07
Asked:
brettsky07
  • 4
  • 3
1 Solution
 
brettsky07Author Commented:
In case it helps my form fields look like this:
<input name="company" class="form" type="text" id="company" size="40" value="<?=$company;?>" />
0
 
m4trixCommented:
Would it be possible to post more of your code? I'm not sure I understand how it all works together
0
 
brettsky07Author Commented:
this is really rough and not really set up the way it will be but here is "step one". To use this code it would be the same as saying if the user selects YES whatever they type in the company name field will appear in the phone number and address fields. This is obviously not how it will function once complete so hopefully it gives you a bit of a better idea...
<?php 
ob_start();
session_start();
?>


<?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 ($attendees=="1Person")
{
	header("Location: register1.php");
}

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

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


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


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


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

				 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>';
                }	
				

            }

            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">
	<option value="1Person">1 Person</option>
	<option value="2People">2 People</option>
	<option value="3People">3 People</option>
	<option value="4People">4 People</option>
	<option value="5People">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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
m4trixCommented:
Ah, ok, I see what you're doing.

One way to do it is to pass that information on to the secondary registration page, so if 2 people were selected, then you could do something like:
    if ($attendees=="2people")
{
    if ($same == "Yes") {
        header("Location: register2.php?".urlencode("phone1=$phone1&phone2=$phone2&phone3-$phone3&address=$address"));
    } else {
        header("Location: register2.php");
    }
}

Open in new window


then in register2.php, you could have something at the top that handles that data:
                 $phone1     = '';
                 $phone2     = '';
                 $phone3    = '';
                 $address    = '';
if(isset($_GET['phone1']))
            {
                 $phone1    = $_GET['phone1'];
                 $phone2    = $_GET['phone2'];
                 $phone3    = $_GET['phone3'];
                 $address    = $_GET['address'];
}

Open in new window


So now on register2.php you have the phone # and address that should be used for all registrants, and you can either hide it or autofill it into the form.

Now, you might want to consider making one "registration page 2", and just pass the # of registrants to that, in the same way I did above. That way, you can make it dynamic and it the same page will produce 1, 2, 3 ... x # of registration fields. Saves you a lot of copy & pasting
0
 
brettsky07Author Commented:
Thanks, thats exactly what I am looking for. Is there any way to keep the variables out of the address bar? Also I cant seem to get them to appear on 'register2.php'.... I tried echo and it didnt seem to work. I'd love to get it to dynamically generate based on how many attendees but as I'm sure you can tell I'm fairly new at php and that kind of functionality is way over my head!
0
 
m4trixCommented:
Ok, this should help you get on the right track. PS - I've changed it to use SESSION variables to remember information between pages.

Save these files to your site and visit register.php from your browser. Give it a whirl. There's a bunch of stuff you'll have to add/change, but it at least demonstrates the idea.
register.php
register2.php

Also, you'll notice that I set the field names in register.php to stuff like "firstname[]". The brackets on the end will automatically place the results into an array when it gets posted.. so instead of
$_POST['firstname1']  // "first person"
$_POST['firstname2']  // "second person"
$_POST['firstname3']  // "third person"

you get:
$_POST['firstname'][0] // "first person"
$_POST['firstname'][1] // "second person"
$_POST['firstname'][2] // "third person"

That way you can loop through it as an array, and it can be any length you want...
0
 
brettsky07Author Commented:
Thanks so much for all your help m4trix!! That actually makes sense to me now haha I'll see what I can do as far as integration. Thanks again.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now