Post Self表单验证和提交在PHP


Post Self Form Validation and Submission in PHP

所以我使用一个类的在线IDE构建并测试了这个表单。验证和postrongelf提交工作得很好,现在看起来它不起作用,我不确定我做错了什么。基本上,在提交表单时,表单自己发布并回显表单下面的表单中的信息。它以前工作得很好,但我显然犯了一些错误。表单的self_post动作根本不起作用,我想我已经解决了这个问题,但是

    <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'
  'http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd'>
<html>
  <head>
    <title>Registration Form</title>
    <meta charset = "utf-8" />
  </head>
  <body>
  <?php
  $nameErr = $phoneErr = $addressErr = $cityErr = $stateErr = $zipErr = "";
  $name = $phone = $address = $city = $zip = $state = "";
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
  $nameErr = "Name is required";
  } else {
   $name = test_input($_POST["name"]);
  // check if name only contains letters and whitespace
  if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed";
  $name = "";
  }
 }
  if (empty($_POST["phone"])) {
  $phoneErr = "Phone number is required";
 } 
  else {
  $phone = test_input($_POST["phone"]);
 }
 if (empty($_POST["address"])) {
 $addressErr = "Street address is required";
 }
 else {
 $address = test_input($_POST["address"]);
 }
if (empty($_POST["city"])) {
$cityErr = "City is required";
} 
else {
$city = test_input($_POST["city"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
  $cityErr = "Only letters and white space allowed";
  $city = "";
 }
}
if (empty($_POST["state"])) {
$stateErr = "State is required";
} 
else {
$state = test_input($_POST["state"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$state)) {
  $stateErr = "Only letters and white space allowed";
  $state = "";
 }
}
if (empty($_POST["zip"])) {
$zipErr = "Zip code is required";
} 
else {
$zip = test_input($_POST["zip"]);
}
}
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"]);?>""> 
<div id = "table" style = "margin: 200px 0px 0px 0px;">
<h2 align = "center">Please register to enjoy our services</h2>
<table align = "center" border = "1">
  <tr>
    <td> Name: </td>
    <td> <input type = "text" name = "name" size = "30" value = ""<?php echo $name;?>""/><span class="error">* <?php echo $nameErr;?></span></td>
  </tr>
  <tr>
    <td> Street Address: </td>
    <td> <input type = "text" name = "address" size = "30" value=""<?php echo $address;?>"" /><span class="error">* <?php echo $addressErr;?></span></td>
  </tr>
  <tr>
    <td> City: </td>
    <td> <input type = "text" name = "city" size = "30" value=""<?php echo $city;?>""/><span class="error">* <?php echo $cityErr;?></span></td>
  </tr>
  <tr>
    <td> State: </td>
    <td> <input type = "text" name = "state" size = "30" value=""<?php echo $state;?>""/><span class="error">* <?php echo $stateErr;?></span></td>
  </tr>
  <tr>
    <td> Zip Code: </td>
    <td> <input type = "text" name = "zip" size = "30" value=""<?php echo $zip;?>""/><span class="error">* <?php echo $zipErr;?></span></td>
  </tr>
  <tr>
    <td> Phone: </td>
    <td> <input type = "text" name = "phone" size = "30" value=""<?php echo $phone;?>""/><span class="error">* <?php echo $phoneErr;?></span></td>
  </tr>
 </table> 
 </div>
 <br />
 <div id = "button" align = "center">
 <input type = "Submit" name = "register" value = "Register"/>
 <input type = "Reset" name = "clear" value = "Clear Form"/>
 </div>
 </form>
 <div id = "results" align = "center">
 <?php
  echo "<h2>Registration Information:</h2>";
  echo $name;
  echo "<br>";
  echo $address;
  echo "<br>";
  echo $city;
  echo "<br>";
  echo $state;
  echo "<br>";
  echo $zip;
  echo "<br />";
  echo $phone;
  ?>
</div>
</body>
</html>

查看http://www.php.net/manual/en/reserved.variables.server.php。它说"这个数组中的条目是由web服务器创建的。不能保证每个web服务器都会提供这些功能;服务器可能会省略一些,或者提供此处未列出的其他内容。"服务器的配置发生了变化,$_SERVER["REQUEST_METHOD"]不再被填充。查看该方法是否为post(假设发布了一些查询参数)的另一种方法是if(count($_POST)>0) {...}