-1

Is there an efficient way to pass variables from one page to another after form submission? I'm struggling to maintain the variables that are submitting on the form page to display them on the confirmation page after being redirected on submission.

Could it be the way i'm 'storing' the data with $_POST? Should I be using sessions? If I should how would I go about storing the $_POST in $_SESSION and being able to call it in the email template as a $variable-name format? Is using header(); to redirect inefficient in this manner and maybe redirecting via ajax? Not sure how I would approach that if so.

Form:

<form id="pricing-form-inquiry" action="<?php echo get_stylesheet_directory_uri(); ?>/pricing-form/form-handler.php" method="POST" role="form">
    <div class="pricing-modal" id="modal1" data-animation="slideInOutLeft">
        <div class="modal-dial">
            <header class="modal-head">
                <a class="close-modal" aria-label="close modal" data-close></a>
            </header>
            <section class="modal-body">
                <div class="row">
                    <div class="col">
                        <input type="text" class="" placeholder="First Name" name="first-name" required data-error="First Name is required."> 
                    </div>
                    <div class="col">
                        <input type="text" class="" placeholder="Last Name" name="last-name" required data-error="Last Name is required."> 
                    </div>
                </div>
                <input type="email" class="" placeholder="Email Address" name="email" required data-error="Email Address is required.">
                <input type="text" class="" placeholder="Company" name="company" id="company">
                <input type="text" class="" placeholder="Phone Number" name="phone" id="phone">
                <div class="row">
                    <div class="col text-center"></div>
                </div>
            </section>
            <footer class="modal-foot">
                <input type="submit" class="pricing-form-submit" value="Calculate" name="submit">
            </footer>
        </div>
    </div>
</form>

form-handler.php

if(isset($_POST['submit'])) {
    $first_name                     = filter_var($_POST['first-name'], FILTER_SANITIZE_STRING);
    $last_name                      = filter_var($_POST['last-name'], FILTER_SANITIZE_STRING);
    $email                          = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $company                        = filter_var($_POST['company'], FILTER_SANITIZE_STRING);
    $phone                          = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);

    $to = "email@email.com";             // Email Address to send lead to
    $subject = "Subject Goes Here!";     // Subject of generated lead email

    // HTML Message Starts here
    $message = "<html><body><table style='width:600px;'><tbody>";
    $message = "<tr><td style='width:150px'><strong>Name: </strong></td><td style='width:400px'>$first_name $last_name </td></tr>";
    $message = "<tr><td style='width:150px'><strong>Email Address: </strong></td><td style='width:400px'>$email</td></tr>";
    $message = "<tr><td style='width:150px'><strong>Company Name: </strong></td><td style='width:400px'>$company</td></tr>";
    $message = "<tr><td style='width:150px'><strong>Phone Number: </strong></td><td style='width:400px'>$phone</td></tr>";
    $message = "</tbody></table></body></html>";
    // HTML Message Ends here

    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

    // More headers
    $headers .= 'From: Company <from@email.com>' . "\r\n"; // User will get an email from this email address
    // $headers .= 'Cc: from@email.com' . "\r\n"; // If you want add cc

    if(mail($to,$subject,$message,$headers)){
        // Message if mail has been sent
        echo "<script>
                alert('Mail has been sent Successfully.');
             </script>";
             header("Location: /pricing-confirm/");
    } else {
        // Message if mail has been not sent
        echo "<script>
                alert('EMAIL FAILED');
              </script>";
    }
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Nick
  • 1,471
  • 2
  • 21
  • 35

1 Answers1

-1

To pass your variables onto the pricing-confirm page, you could pass the variables in your header() function like so

header("Location: /pricing-confirm.php?name=" . $first_name);

Once on your pricing-confirm.php page, you can grab the variables from the query string

if(isset($_GET['name']) {
 $name = $_GET['name'];
}

If you want to pass multiple variables at once, you can either use & in the query string, or use urldecode with an array like this

$arr = [
  "firstname" => $first_name,
  "lasttname" => $last_name,
]

header("Location: /pricing-confirm.php?userdetails=" . urlencode(serialize($arr)));

If you have used serialize, you can get the values in the array like this

$queryArr = unserialize(urldecode($_GET['userdetails']));

you can then access them with their array key, like so

if(isset($_GET['userdetails']) {
  $arr = $_GET['userdetails'];
  if(isset($arr['firstname']) {
    $firstName = $arr['firstname'];
  }
}
IndustryDesigns
  • 1,938
  • 2
  • 12
  • 22