Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

html - Post Self Form Validation and Submission in PHP

So I built and tested this form using an online IDE for a class. The validation and post_self submission was working great, now it appears that it doesn't work and I'm not really sure as to what I did wrong. Essentially, on submit the form self posts and echos out the information from the form beneath the form. It worked wonderfully before, but I have clearly made some form of error. The self_post action for the form wasn't working at all and I think I fixed that, but the

    <!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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check out http://www.php.net/manual/en/reserved.variables.server.php. It says "The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here." Something changed in the config of your server, and $_SERVER["REQUEST_METHOD"] is not populated anymore. An alternative way to see if the method was a post (assuming some query parameters were posted) is if(count($_POST)>0) {...}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...