获取输入值并在提交后在新页面中显示

时间:2015-12-17 09:57:14

标签: javascript php jquery html forms

我尝试在提交表单后显示用户输入。

表单如下:

<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@you.com (so that we can contact you)" data-placement="top" data-trigger="manual" data-content="Must be a valid e-mail address (user@gmail.com)" 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>

以下是用户提交后的PHP

if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';
    $to="test@ymail.com";
    $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:-your-email@your-domain.com
}

这是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>";

我可以收到用户在我的电子邮件中发送的内容,但问题出在success.php,我尝试使用上面的代码,但我得到的是未定义的索引。

提交后有没有办法在新页面中显示输入值?

2 个答案:

答案 0 :(得分:1)

您正在重定向到另一个页面,因此帖子值不会传递到成功页面。你能做些什么让他们在会议中获得临时性

答案 1 :(得分:1)

使用会话变量,或者您也可以通过网址发送数据,并在成功页面上使用$_GET(我可能不会建议这样做)。

提交后

PHP:

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

 $to="test@ymail.com";
 $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>";
?>

它实际上将数据保存到变量中,这些变量可以在备用页面上启动会话时使用,并且可以使用$ _SESSION进行调用。