I have a few forms on my website which I need site visitors to submit in sequence.
'form1' sends the user data to the (php mailto) form mailer.
The header(location) of the form mailer then directs the site visitor to a page with 'form2'.
I need a php solution to pre-populate form2 with the previously submitted data from form1.
I would like a slicker alternative to passing the data through the URL.
Form 1:
<form id="form1" action="formMailer.php" method="post">
Name: <input type="text" id="userName" name="userName" value="">
Email: <input type="email" id="userEmail" name="userEmail" value="">
<input type="submit" id="submitButton" name="submitButton" value="Submit">
PHP Form Mailer:
<?php
$name = $_POST['userName'];
$email = $_POST['userEmail'];
$message =
"\r\nYour Name: " .$name .
"\r\nYour Email: " .$email;
$subject ="Subject of email";
$mailto = "[email protected]";
$separator = md5(uniqid(time()));
$eol = "\r\n";
$headers = "From: ".$name." <".$email.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed;". $eol. " boundary=\"" . $separator . "\"" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
mail($mailto, $subject, $body, $headers);
header('location: /formFolder/form2.php');
?>
Form 2:
<form id="form2" action="nextAction" method="post">
Name: <input type="text" id="userName" name="userName" value="">
Email: <input type="email" id="userEmail" name="userEmail" value="">
Other Data: <input type="text" id="other_userData" name="other_userData" value="">
<input type="submit" id="submitButton" name="submitButton" value="Submit">
I tried some php 'get' and 'echo' script, but it seems the data gets lost somewhere between the mailto and the second form.
I have a few forms on my website which I need site visitors to submit in sequence.
'form1' sends the user data to the (php mailto) form mailer.
The header(location) of the form mailer then directs the site visitor to a page with 'form2'.
I need a php solution to pre-populate form2 with the previously submitted data from form1.
I would like a slicker alternative to passing the data through the URL.
Form 1:
<form id="form1" action="formMailer.php" method="post">
Name: <input type="text" id="userName" name="userName" value="">
Email: <input type="email" id="userEmail" name="userEmail" value="">
<input type="submit" id="submitButton" name="submitButton" value="Submit">
PHP Form Mailer:
<?php
$name = $_POST['userName'];
$email = $_POST['userEmail'];
$message =
"\r\nYour Name: " .$name .
"\r\nYour Email: " .$email;
$subject ="Subject of email";
$mailto = "[email protected]";
$separator = md5(uniqid(time()));
$eol = "\r\n";
$headers = "From: ".$name." <".$email.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed;". $eol. " boundary=\"" . $separator . "\"" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
mail($mailto, $subject, $body, $headers);
header('location: /formFolder/form2.php');
?>
Form 2:
<form id="form2" action="nextAction" method="post">
Name: <input type="text" id="userName" name="userName" value="">
Email: <input type="email" id="userEmail" name="userEmail" value="">
Other Data: <input type="text" id="other_userData" name="other_userData" value="">
<input type="submit" id="submitButton" name="submitButton" value="Submit">
I tried some php 'get' and 'echo' script, but it seems the data gets lost somewhere between the mailto and the second form.
Share Improve this question asked Mar 17 at 6:04 Vera de MiloVera de Milo 92 bronze badges 3 |1 Answer
Reset to default 0simply using header('location: /formFolder/form2.php');
loses the data. You need a way to persist the data between requests.
1. Modify formMailer.php
to use sessions
<?php
session_start(); // Start the session
$name = $_POST['userName'];
$email = $_POST['userEmail'];
// Store data in session variables
$_SESSION['userName'] = $name;
$_SESSION['userEmail'] = $email;
$message =
"\r\nYour Name: " . $name .
"\r\nYour Email: " . $email;
$subject = "Subject of email";
$mailto = "[email protected]";
$separator = md5(uniqid(time()));
$eol = "\r\n";
$headers = "From: " . $name . " <" . $email . ">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed;" . $eol . " boundary=\"" . $separator . "\"" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $message . $eol;
mail($mailto, $subject, $body, $headers);
header('location: /formFolder/form2.php');
?>
2. Modify form2.php
to retrieve and populate the form using session data
<?php
session_start(); // Start the session
$userName = isset($_SESSION['userName']) ? $_SESSION['userName'] : '';
$userEmail = isset($_SESSION['userEmail']) ? $_SESSION['userEmail'] : '';
// Optionally, clear the session data after using it if you don't need it later
// unset($_SESSION['userName']);
// unset($_SESSION['userEmail']);
?>
<form id="form2" action="nextAction" method="post">
Name: <input type="text" id="userName" name="userName" value="<?php echo htmlspecialchars($userName); ?>">
Email: <input type="email" id="userEmail" name="userEmail" value="<?php echo htmlspecialchars($userEmail); ?>">
Other Data: <input type="text" id="other_userData" name="other_userData" value="">
<input type="submit" id="submitButton" name="submitButton" value="Submit">
</form>
Important Considerations:
Sessions rely on cookies or URL parameters to track users. Ensure your website is configured to handle sessions securely.
Add more robust error handling to your code, such as checking if $_POST variables are set before accessing them.
Sessions have a default expiration time. You can configure this in your PHP settings.
After form2 is submitted, you can unset or destroy the session, so a user cannot reload form2 and see the data.
$_SESSION
to store the data and access them in the next form. – shirshak007 Commented Mar 17 at 6:25