Ad Reactor
Ad Reactor

Reputation: 85

Passing form data from one web page to another with PHP

i found few similar questions here but from answers i didn't get the whole picture of how should work.

I have a subscription form in a page:

<form method="post" action="index.php/register"> 
<fieldset> 
<input type="text" id="first_name" name="first_name" /> 
<input type="text" id="last_name" name="last_name" />
<input type="text" id="email" name="email" />
<input type="text" id="address" name="address" /> 
<input id="submit" type="submit" value="&gt;&gt;" /> 
</fieldset> 
</form>

when a user a user click the submit button is lead to a page with the full registering form, where i need to have few fields populated with the data sent from previous page form. this is a preview of few fields from the form of the second page:

<form id="register" name="form1" method="post" action="send_contact.php">
<fieldset>  
<li><label>*First Name</label>
<input type="text" id="first_name" name="first_name" />
</li>

<li>
<label>*Last Name</label>
<input type="text" id="last_name" name="last_name" />
</li>

<li>
<label>*Email</label>
<input type="text" id="email" name="email" />
</li>

<li>
<label>*Confirm Email</label>
<input type="text" id="confirm-email" name="confirm_email" />
</li>

<li>
<label>Street Address</label>
<input type="text" id="address" name="address" />

<li class="full-width">
<input id="submit" type="submit" value="Register" />                    
</li>

</fieldset>
</form> 

the php is not my strong point, so if you can be more detailed in answer is great for me.

thanks!

Upvotes: 2

Views: 16438

Answers (4)

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

<form method="post" action="register.php"> 
<fieldset> 
<input type="text" id="first_name" name="first_name" /> 
<input type="text" id="last_name" name="last_name" />
<input type="text" id="email" name="email" />
<input type="text" id="address" name="address" /> 
<input id="submit" type="submit" value="&gt;&gt;" /> 
</fieldset> 
</form>

register.php

<form id="register" name="form1" method="post" action="send_contact.php">
<fieldset>  
<li><label>*First Name</label>
<input type="text" value="<?php echo $_POST['first_name'];?>" id="first_name" name="first_name" />
</li>

<li>
<label>*Last Name</label>
<input type="text" value="<?php echo $_POST['last_name'];?>" id="last_name" name="last_name" />
</li>

<li>
<label>*Email</label>
<input type="text" value="<?php echo $_POST['email'];?>" id="email" name="email" />
</li>

<li>
<label>*Confirm Email</label>
<input type="text" id="confirm-email" name="confirm_email" />
</li>

<li>
<label>Street Address</label>
<input type="text" value="<?php echo $_POST['address'];?>" id="address" name="address" />

<li class="full-width">
<input id="submit" type="submit" value="Register" />                    
</li>

</fieldset>
</form>

Finally service the post in send_contact.php as you wish

Upvotes: 0

Tumharyyaaden
Tumharyyaaden

Reputation: 2893

I would say for security reasons, do not use Get method "$_GET[]" as people described, keep POST as you have it.

All you need to do on register/ page is get all the values passed on using the POST method and populate them into your HTML. So the second form should look like:

    <form id="register" name="form1" method="post" action="send_contact.php">
    <fieldset>  
    <li><label>First Name</label>
    <input type="text" id="first_name" name="first_name" value="<?=$_POST[first_name]?>" />
    </li>

    <li>
    <label>*Last Name</label>
    <input type="text" id="last_name" name="last_name" value="<?=$_POST[last_name]?>" />
    </li>

    <li>
    <label>*Email</label>
    <input type="text" id="email" name="email" value="<?=$_POST[email]?>" />
    </li>

    <li>
    <label>*Confirm Email</label>
    <input type="text" id="confirm-email" name="confirm_email" />
    </li>

    <li>
    <label>Street Address</label>
    <input type="text" id="address" name="address" value="<?=$_POST[address]?>" />

    <li class="full-width">
    <input id="submit" type="submit" value="Register" />                    
    </li>

    </fieldset>
    </form> 

Above, I am using shorthand version of "echo" and php tags, if you do not have that enabled under php.ini, please change "" to "; ?>. Also, script will not populate "confirm" email as I assume you would like the user to retype that.

That should do it.

Upvotes: 2

PtPazuzu
PtPazuzu

Reputation: 2537

You can use the $_POST values from the first form in the page handling the submit of the first form and print them in the new form as:

<?php
  echo '<input type="text" id="email" name="email" value="' . htmlentities($_POST['email']) . '"/>
?>

Upvotes: 0

Jonah Katz
Jonah Katz

Reputation: 5288

There are basically two methods

  1. Store the values of the first form in a cookie, and the process code can retrieve the values from the cookie

  2. make the form action 'Get' so that the data is passed on to the next page.

Upvotes: 0

Related Questions