Is there any way to pass values between two forms. Let's say, I have two html form.
At Form 1, there's one field called name and submit button.
At Form 2, there's two field called name and email and submit button.
What I want to get is, At form 1, when i fill Form 1 name field and click submit, it will carry me to form 2 and name values that I filled will be inserted at names field of Form 2.
Is that possible to do that ?
If that so, please leave idea or snippets for me.
Thanks.
if the forms are in separated pages, you can populate values in form two using the $_POST arrays:
page 1
<form action="page2.php" method="post">
name <input type="text" name="name" /><br/>
email <input type="email" name="email" />
<input type="submit" value="submit"/>
</form>
At page2.php, grab values comming from $_POST and populate the form with these values:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
?>
<form action="pageX.php" method="post">
name <input type="text" name="name" value="<?php echo $name;?>"/><br/>
email <input type="email" name="email" value="<?php echo $email;?>"/>
<input type="submit" value="submit"/>
</form>
Obviously you'll need to validate $_POST data in page2.php, but that's another issue...
Didn't validated the code, probably some checks missing, but you can do all in one page with two functions, something like:
function renderForm1($name $error)
{
<?PHP
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
name <input type="text" name="name" /><br/>
<input type="submit" value="getname"/>
</form>
}
function renderForm2($name, $email, $error)
{
<?PHP
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
name <input type="text" name="name" /><br/>
email <input type="email" name="email" />
<input type="submit" value="submit"/>
</form>
}
if (isset($_POST['getname']))
{
if(empty($_POST['name'])
{
$error='bla bla';
renderForm1($name $error)
}
else
{
$name=$_POST['name'];
renderForm2($name, $email, $error)
}
}
elseif (isset($_POST['submit']))
{
do whathever with fomr2
}
else
{
renderForm1($name $error);
}