将PHP变量消息回显到php表单

时间:2013-04-01 05:47:52

标签: php forms validation echo

我在new.php中有一个基本的帖子表格,我想知道如何在它上面回显(或显示)来自new1.php的php脚本变量消息。

new.php:

 <body>
<div id="contact_form">
  <form name="contact" method="post" action="new1.php" id="contact">
    <fieldset>
      <label for="firstName" id="firstName_label">First Name*:</label>
      <input type="text" name="firstName" id="firstName" size="36" value="" class="text-input" />
      <label class="error" for="firstName" id="firstName_error">This field is required.</label>
      <br />
            <label for="lastName" id="lastName_label">Last Name:</label>
      <input type="text" name="lastName" id="lastName" size="36" value="" class="text-input" />
      <label class="error" for="lastName" id="lastName_error">This field is required.</label>
      <br />
      <label for="email" id="email_label">Email*:</label>
      <input type="text" name="email" id="email" size="36" value="" class="text-input" />
      <label class="error" for="email" id="email_error">This field is required.</label>
      <label class="error" for="email" id="email_error2">This is not a valid email address.</label>
      <br />
      <label for="postcode" id="postcode_label">Postcode:</label>
      <input type="text" name="postcode" id="postcode" size="12" value="" class="text-input" />
      <label class="error" for="postcode" id="postcode_error">This field is required.</label>

        <br />
      <input type="submit" name="submit" id="submit_btn" value="Send" />
    </fieldset>
  </form>
</div>
</body>

new1.php - 返回$ msg_to_user中的消息。你可以看到我在这个页面上回显它们,但我怎样才能将它们回显到表单所在的new.php?

<?php
// set variables to null
$firstName   = "";
$lastName    = "";
$email       = "";
$postcode    = "";
$msg_to_user = "";


include_once "connect_to_mysql.php";

// set variables from form
// be sure to filter this data to deter SQL injection, filter before querying database
$firstName = $_POST['firstName'];
$lastName  = $_POST['lastName'];
$email     = $_POST['email'];
$postcode  = $_POST['postcode'];

$sql     = mysql_query("SELECT * FROM newsletter WHERE email='$email'");
$numRows = mysql_num_rows($sql);

// first name is mandatory
if (!$firstName) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Please enter your name.</font></h4>';

    // email is mandatory
} else if (!$email) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Please type an email address ' . $firstName . '.</font></h4>';

    // check email is valid
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is not a valid email address.               </font></h4>';

    // check postcode is a number
} else if (!is_numeric($postcode)) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Postcode must be a numeric value.</font></h4>';

    // check postcode is greater than 4 chars
} else if (strlen ($postcode) < 4) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Postcode must be at least 4 characters.</font></h4>';

    // check postcode is less than 12 chars
} else if (strlen ($postcode) > 12) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">Postcode must be less than 12 characters.</font></h4>';

    // check email doesn't exist    
} else if ($numRows > 0) {
    $msg_to_user = '<br /><br /><h4><font color="FF0000">' . $email . ' is already in the system.</font></h4>';

    // if all test passed, insert details into database
} else {
    $sql_insert = mysql_query("INSERT INTO newsletter (firstName, lastName, email, postcode, dateTime) 
                                                    VALUES('$firstName','$lastName','$email','$postcode',now() )") or die(mysql_error());

    $msg_to_user = '<br /><br /><h4><font color="0066FF">Thanks ' . $firstName . $lastName . ' , you have been added successfully.</font></h4>';




    $firstName = "";
    $lastName  = "";
    $email     = "";
    $postcode  = "";
}
?>

<html>
<body>

<p>First Name: <?php
echo $firstName;
?></p>
<p>Last Name: <?php
echo $lastName;
?></p>
<p>Email: <?php
echo $email;
?></p>
<p>Postcode: <?php
echo $postcode;
?></p>
<p>Message: <?php
echo $msg_to_user;
?></p>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

使用ajaxphp sessions执行此操作。

这是使用php sessions

的方法

在new1.php

session_start();
$_SESSION['name']=$variable;

在new.php中

session_start();
echo $_SESSION['name'];

答案 1 :(得分:1)

执行此操作的简单方法是使用标头功能从new1.php重定向到new.php。在重定向时,通过url传递$ msg_to_user。

为此,在为$ msg_to_user分配适当的值后,在new1.php中添加以下代码

header("location: new.php?msg_to_user=".$msg_to_user);
exit;

并在new.php中添加以下代码:

echo $_GET['msg_to_user']; //this will display the message

另一种方法是使用session来访问页面中的值。

在new1.php中插入以下代码以在会话变量中设置消息:

session_start();
$_SESSION['msg_to_user'] = $msg_to_user;
header("location: new.php);
exit;

在new.php中添加以下代码以检索在new1.php中设置的消息:

session_start();
if (isset($_SESSION['msg_to_user'])) {
    echo $_SESSION['msg_to_user'];
    unset($_SESSION['msg_to_user']);//After displaying the message unset it, to make sure that the message is displayed only once.
}

答案 2 :(得分:0)

通过会话传递验证消息。 而不是使用$_SESSION['name']='';删除会话值 所以消息只显示一次。