我有一个我需要在PHP中创建的表单,需要验证输入的数据,如果一切正常,则将其发送出去。我需要一段时间才能完成它,但如果所有表格都填写正确,它会重定向。但是,接收页面不会从表单页面获取数据。我可以对表单应用两个操作吗?我怎样才能将数据发送给自己($ _SERVER [" PHP_SELF"])和另一页?我在接收页面中的所有变量定义上收到未定义的索引错误。
以下是表单页面的代码:
<!DOCTYPE html>
<!-- Jonathan DeMars
4/20/2017
http://chelan.highline.edu/~jon_demars3/116/magazine.html
-->
<html lang="en">
<head>
<meta charset="utf-8">
<title>GQ - Subscription</title>
<meta name="description" content="Register to GQ">
<meta name="author" content="CSCI 116 Student: Jonathan DeMars">
<link rel="stylesheet"
type="text/css"
href="styles.css">
</head>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<body>
<h1>Register to GQ</h1>
<p>GQ or "Gentleman's Quarterly" is an international monthly men's magazine based in New York City. The publication focuses on fashion, style,
and culture for men; though articles on food, movies, fitness, sex, music, travel, sports, technology,
and books are also featured.</p>
<hr>
<br>
<p><strong>Please complete the following form:</strong></p>
<?php
$firstnameErr = $lastnameErr = $addressErr = $cityErr = $zipcodeErr = $monthsErr = "";
$first_name = $last_name = $address = $city = $zipcode = $months = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name = test_input($_POST["first_name"]);
$last_name = test_input($_POST["last_name"]);
$address = test_input($_POST["address"]);
$city = test_input($_POST["city"]);
$zipcode = test_input($_POST["zipcode"]);
$months = test_input($_POST["months"]);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["first_name"])) {
$firstnameErr = "First name is required";
} else {
$first_name = test_input($_POST["first_name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$firstnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["last_name"])) {
$lastnameErr = "Last name is required";
} else {
$last_name = test_input($_POST["last_name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$lastnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["address"])) {
$addressErr = "Address is required";
} else {
$address = test_input($_POST["address"]);
}
if (empty($_POST["city"])) {
$cityErr = "City is required";
} else {
$city = test_input($_POST["city"]);
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "Only letters and white space allowed";
}
}
if (empty($_POST["zipcode"])) {
$zipcodeErr = "Zipcode is required";
} else {
$zipcode = test_input($_POST["zipcode"]);
if (!preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {
$zipcodeErr = "Please enter a valid zipcode.";
}
}
if (empty($_POST["months"])) {
$monthsErr = "You must subscribe for 1 or more months.";
} else {
$months = test_input($_POST["months"]);
if (!preg_match("/^[1-9][0-9]*$/",$months)) {
$monthsErr = "Must enter a valid number.";
}
}
}
if($firstnameErr == "" && $lastnameErr == "" && $addressErr == "" && $cityErr == "" && $zipcodeErr == "" && $monthsErr == ""){
header('Location: magazinevalidation_post.php');
exit();
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>First Name: <input type="text" name="first_name" size="15" value="<?php echo $first_name;?>" <span class="error">* <?php echo $firstnameErr;?></span>
Last Name: <input type="text" name="last_name" size="15" value="<?php echo $last_name;?>" <span class="error">* <?php echo $lastnameErr;?></span></p>
<br>
<p>Address: <input type="text" name="address" size="20" value="<?php echo $address;?>" <span class="error">* <?php echo $addressErr;?></span></p>
<p>City: <input type="text" name="city" size="15" value="<?php echo $city;?>" <span class="error">* <?php echo $cityErr;?></span> </p>
<p>State: <select name="state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
<p>Zip Code: <input type="text" name="zipcode" size="4" value="<?php echo $zipcode;?>" <span class="error">* <?php echo $zipcodeErr;?></p>
<br>
<br>
<p>How many months would you like to subscribe? <input type="text" name="months" size="1" value="<?php echo $months;?>" <span class="error">* <?php echo $monthsErr;?></span></p>
<br>
<br>
<input type="submit" name="submit" value="Continue" />
</form>
<p><a href="index.html">Return to index page</a></p>
</body>
</html>
以下是接收页面的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GQ - Thank you!</title>
<meta name="description" content="Thank you for Registering to GQ">
<meta name="author" content="CSCI 116 Student: Jonathan DeMars">
<link rel="stylesheet"
type="text/css"
href="styles.css">
</head>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<body>
<?php
echo "<h1>Order Summary</h1><hr>";
define("TAX", "0.10");
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$months = $_POST['months'];
$name = $first_name . ' ' . $last_name;
$monthlyrate = 9.99;
$subtotal = $months * $monthlyrate;
$taxtotal = $subtotal * TAX;
$grandtotal = $subtotal + $taxtotal;
print "<p><strong>$name</strong></p>";
print "<strong>$address</strong><br>";
print "<strong>$city, $state $zipcode</strong><br>";
print "<p>Your Subscription: <strong>$months months</strong></p>";
print "Monthly Payments: <strong>$$subtotal</strong><br>";
print "Tax:";
echo "<strong> $";
echo round($taxtotal, 2, PHP_ROUND_HALF_UP);
echo "</strong>";
print "<br>Total:";
echo "<strong> $";
echo round($grandtotal, 2, PHP_ROUND_HALF_UP);
echo "</strong>";
print "<div><p>Thank you, $name, for your subscription. You will recieve your first copy of GQ within the week!
</p></div>";
?>
<p><a href="index.html">Return to index page</a></p>
</body>
</html>
答案 0 :(得分:0)
您可以使用会话将数据传递到另一个表单。在使用会话变量可以在form2中访问这些数据之后,重定向之前设置会话中的值。 $ _ SESSION - 可能是最好的方式
<?php
session_start();
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['address'] = $address;
$_SESSION['city'] = $city;
$_SESSION['address'] = $address;
$_SESSION['zipcode'] = $zipcode;
$_SESSION['months'] = $months;
header('Location: magazinevalidation_post.php');
exit();
在magazinevalidation_post.php文件中,您可以使用
访问该值<?php
session_start();
echo $_SESSION['first_name'];
echo $_SESSION['last_name']
echo $_SESSION['address'];
echo $_SESSION['city'];
echo $_SESSION['address'];
echo $_SESSION['zipcode'];
echo $_SESSION['months'];
?>