stackacc
stackacc

Reputation: 81

Get input value and show it in new page after submit

I tried to show user input after they submit their form.

Here is the form looks like:

<form class="form-horizontal col-sm-12" name="enq" method="post" action="email/" >
    <div class="form-group"><label>Name</label><input class="form-control required" name="name" placeholder="Your name" data-placement="top" data-trigger="manual" data-content="Must be at least 3 characters long, and must only contain letters." type="text"></div>
    <div class="form-group"><label>Message</label><textarea class="form-control" name="message" placeholder="Your message here.." data-placement="top" data-trigger="manual"></textarea></div>
    <div class="form-group"><label>E-Mail</label><input class="form-control email" name="email" placeholder="[email protected] (so that we can contact you)" data-placement="top" data-trigger="manual" data-content="Must be a valid e-mail address ([email protected])" type="text"></div>
    <div class="form-group"><label>Phone</label><input class="form-control phone" placeholder="999-999-9999" data-placement="top" data-trigger="manual" data-content="Must be a valid phone number (999-999-9999)" type="text"></div>
    <div class="form-group"><input type="submit" class="btn btn-success pull-right" id="submit" name="submit"></input> <p class="help-block pull-left text-danger hide" id="form-error">&nbsp; The form is not valid. </p></div>
</form>

Here is the PHP after the user submit:

if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';
    $to="[email protected]";
    $subject="Enquiry!";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: ".$email_from."\r\n";
    $message="    
        Name:
        $name      
        <br>
        Email-Id:
        $email     
        <br>
        Message:
        $query     
    ";
    if(mail($to,$subject,$message,$headers))
        header("Location:../success.php");
    else
        header("Location:../test.php?msg=Error To send Email !");
        //contact:[email protected]
}

And here is success.php:

$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
echo "<strong>Name:</strong> ".$name."<br>";    
echo "<strong>Email:</strong> ".$email."<br>";  
echo "<strong>Message:</strong> ".$query."<br>";

I could receive what the user sent in my email, but the problem is with success.php, I tried to use the above code, but what I got is undefined index.

Is there a way to show the input values in the new page after submit?

Upvotes: 1

Views: 220

Answers (2)

Matt
Matt

Reputation: 2869

Use session variables or you could alternatively send the data through the URL and use $_GET on the success page (I probably don't suggest this though).

PHP after submit:

<?php
 if(isset($_POST['submit']))
 {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';

 $to="[email protected]";
 $subject="Enquiry!";
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "From: ".$email_from."\r\n";
 $message="   

         Name:
         $name     
         <br>
         Email-Id:
         $email        
         <br>
         Message:
         $query        

   ";
    if(mail($to,$subject,$message,$headers))
    {
        session_start();//Start the session
        $_SESSION['name'] = $name; //Set name session
        $_SESSION['email'] = $email; //Set email session
        $_SESSION['query'] = $query;  // Set query session
        header("Location:../success.php");
    }
    else
    {
        header("Location:../test.php?msg=Error To send Email !");
    }
 }
?>

success.php

<?php
    session_start();
    echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
    echo "<strong>Name:</strong> ".$_SESSION['name']."<br>";//Use the session variables    
    echo "<strong>Email:</strong> ".$_SESSION['email']."<br>";  
    echo "<strong>Message:</strong> ".$_SESSION['query']."<br>";
?>

It essentially saves the data into variables that will work whenever sessions are started on alternative pages and can be called using $_SESSION.

Upvotes: 1

Keyboard ninja
Keyboard ninja

Reputation: 755

You are redirecting to another page so the post values are not passed to the success page. What you could do it put them temporairy in a session

Upvotes: 1

Related Questions