提交给PHP脚本的HTML表单不起作用

时间:2014-05-22 00:59:15

标签: php html forms

我有一个HTML表单,它将联系人信息发布到镜像同一页面并验证输入的PHP脚本。 以下是第一页上的HTML代码:

<p><span class="error">* required field.</span></p>
<form action="http://academic1.bellevue.edu/users/CIS33715/validate.php" method="POST">
Full Name: 
<input type="text" name="name"/>
<span class="error">* </span></br>
Phone #: 
<input type="text" name="phone"/>
<span class="error">* </span></br>
Street Address: 
<input type="text" name="street"/>
<span class="error">* </span></br>
City: 
<input type="text" name="city"/>
<span class="error">* </span></br>
State: 
<input type="text" name="state"/>
<span class="error">* </span></br>
Zip Code: 
<input type="text" name="zip"/>
<span class="error">* </span></br>
    <input type="submit" value="Submit"/>
</form>

下一页是一个php脚本,它基本上在HTML中打印同一页面,同时运行PHP代码以验证表单输入并在输入无效时显示错误消息。

这是PHP脚本:

<?php
// define variables and set to empty values
$nameErr = $phoneErr = $streetErr = $cityErr = $stateErr = $zipErr = "";
$name = $phone = $street = $city = $state = $zip = "";

// start script if the form is submitted
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 whitespaces
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $nameErr = "Only letters and white space allowed"; 
    }
}

if (empty($_POST["phone"])) {
    $phoneErr = "Phone # is required";
} 
else {$email = test_input($_POST["phone"]);
    // check if phone only contains numbers and dashes in correct format
    if (!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/",$phone)) {
        $phoneErr = "Please resubmit Phone # in the following format: 555-    555-5555"; 
    }
}

    if (empty($_POST["street"])) {
    $streetErr = "Address is required";
} 
else {$street = test_input($_POST["street"]);
     // check if address syntax is valid
     if (!preg_match("/^[0-9a-zA-Z. ]+$/",$street)) {
   $streetErr = "Address appears to be invalid.";
     }
   }

        if (empty($_POST["city"])) {
     $cityErr = "City is required";
}
else {$city = test_input($_POST["city"]);
 // check if city syntax is valid
     if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
      $cityErr = "City appears to be invalid." ; 
 }
}

    if (empty($_POST["state"])) {
 $stateErr = "State is required";
} 
    else {$state = test_input($_POST["state"]);
 // check if state is valid uppercase abbv. and two letters
     if (!preg_match("/^(A[LKSZRAP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADL N]
     |K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD] 
     |T[NX]|UT|V[AIT]|W[AIVY])$/",$state)) {
         $stateErr = "State appears to be invalid. (Hint...Use Upper-case!)" ; 
       }
   }

    if (empty($_POST["zip"])) {
    $zipErr = "Zip Code field is required";
}   
    else {$zip = test_input($_POST["zip"]);
    // check if zip only contains numbers and dashes in correct format
    if (!preg_match("/^[0-9]{5}-[0-9]{4}$/",$phone)) {
        $phoneErr = "Please resubmit Zip Code in the following format: 55555-5555"; 
    }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
 }
 ?>

<p><span class="error">* required field.</span></p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
Full Name: 
<input type="text" name="name" value="<?php echo $name;?>"/>
<span class="error">* <?php echo $nameErr;?></span></br>
Phone #: 
<input type="text" name="phone" value="<?php echo $phone;?>"/>
<span class="error">* <?php echo $phoneErr;?></span></br>
Street Address: 
<input type="text" name="street" value="<?php echo $street;?>"/>
<span class="error">* <?php echo $streetErr;?></span></br>
City: 
<input type="text" name="city" value="<?php echo $city;?>"/>
<span class="error">* <?php echo $cityErr;?></span></br>
State: 
<input type="text" name="state" value="<?php echo $state;?>"/>
<span class="error">* <?php echo $stateErr;?></span></br>
Zip Code: 
<input type="text" name="zip" value="<?php echo $zip;?>"/>
<span class="error">* <?php echo $zipErr;?></span></br>
<input type="submit" value="Submit"/>
</form>

<?php
echo "<h2>Your Input:</h2>;"
echo $name;
echo "<br>";
echo $street;
echo "<br>";
echo $city;
echo "<br>";
echo $state;
echo "<br>";
echo $zip;
echo "<br>";
echo $phone;
?>

感谢您提供的任何帮助。当我在第一页上提交表单时,它会将我带到一个没有错误或任何错误的空白HTML页面。

我已经确认其他学生的PHP页面正在运行,因此服务器没有任何问题。

2 个答案:

答案 0 :(得分:0)

您遇到语法错误。

echo "<h2>Your Input:</h2>;"
echo $name;

应阅读

echo "<h2>Your Input:</h2>";
echo $name;

启用error_reporting进行开发,并使用带有错误突出显示的IDE。

此外,在功能if ($_SERVER["REQUEST_METHOD"] == "POST") {

之前,你有一个缺失的大括号应该关闭test_input($data)

也许也适当缩进。纪律可以防止头痛。

答案 1 :(得分:0)

第二页代码的第90行缺少} ,需要关闭整个 if 语句。在 test_input()函数之前添加它。这是Popnoodles提到的分号错误放置的补充。

良好的编码,

c0p