在PHP中发布自我表单验证和提交

时间:2014-05-29 13:01:42

标签: php html

所以我使用在线IDE为一个类构建并测试了这个表单。验证和post_self提交工作很好,现在似乎它没有工作,我不确定我做错了什么。从本质上讲,提交表单自我发布和回声表格下面的表格中的信息。之前它运作得非常好,但我已经明确地犯了一些错误。表单的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>

1 个答案:

答案 0 :(得分:0)

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