1

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

新的.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>
4

3 回答 3

1

最简单的方法是使用 header 函数从 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.
}
于 2013-04-01T06:04:03.727 回答
1

使用ajaxphp sessions来执行此操作。

这是如何使用php sessions.

在 new1.php

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

在新的.php

session_start();
echo $_SESSION['name'];
于 2013-04-01T05:55:02.643 回答
0

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

于 2013-04-01T05:58:17.017 回答