我在 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>