最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I pass form data to the next form page after sending to a php mailto page? - Stack Overflow

programmeradmin2浏览0评论

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:&nbsp;<input type="text" id="userName" name="userName" value="">
   Email:&nbsp;<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:&nbsp;<input type="text" id="userName" name="userName" value="">
   Email:&nbsp;<input type="email" id="userEmail" name="userEmail" value="">
   Other Data:&nbsp;<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:&nbsp;<input type="text" id="userName" name="userName" value="">
   Email:&nbsp;<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:&nbsp;<input type="text" id="userName" name="userName" value="">
   Email:&nbsp;<input type="email" id="userEmail" name="userEmail" value="">
   Other Data:&nbsp;<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
  • 6 You can use $_SESSION to store the data and access them in the next form. – shirshak007 Commented Mar 17 at 6:25
  • Welcome to SO. Please try searching before posting a new question - there is so much knowledge here already. A simple search for the keywords in your title, eg "php pass data between pages" turns up many answers stackoverflow/questions/871858/…, stackoverflow/questions/5678567/…, stackoverflow/questions/16778425/…, stackoverflow/q/20294915/6089612, ... – Don't Panic Commented Mar 17 at 8:50
  • 1 This question is similar to: PHP Pass variable to next page. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Don't Panic Commented Mar 17 at 8:50
Add a comment  | 

1 Answer 1

Reset to default 0

simply 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:&nbsp;<input type="text" id="userName" name="userName" value="<?php echo htmlspecialchars($userName); ?>">
    Email:&nbsp;<input type="email" id="userEmail" name="userEmail" value="<?php echo htmlspecialchars($userEmail); ?>">
    Other Data:&nbsp;<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.

发布评论

评论列表(0)

  1. 暂无评论