doudiao2335 2016-04-22 19:26
浏览 80

HTML PHP自我表单验证错误

I can't seem to get this form to work properly. My web scripting knowledge is pretty limited as I'm still a student. I searched and found this Post Self Form Validation and Submission in PHP which is the same assignment but this person had a different problem.

For some reason when I try to test out my page "> Name: shows up as my first line but the "> is the closing for the html form opening tag.

I also have this a .html because I want to submit it as one document and not have the PHP code in a separate .php document.

edit: @ Quentin - This is not related to the duplicate article that this was marked as. I'm not running an Apache server. That person solved his problem by restarting the XAMPP service I'm Windows 10 and cannot possibly have that service running.

I want it to run off one document, thus why I was using PHP_SELF. When I save the file as a .php it doesn't do anything but display the code.

I did inspect the code in Chrome, the only way I know how to debug it. And I couldn't find anything wrong with it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Assignment 6.2 PHP Validation</title>
</head>

<body>

<?php
// define variables and set to empty values
$nameErr = $addressErr = $phoneErr = $zipErr = "";
$name = $address = $phone = $zip = "";

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"; 
  }
 }
 //check name field
if (empty($_POST["address"])) {
$emailErr = "Address is required";
} 

// check phone number field
 if (empty($_POST['phone'])) {
 $phoneErr = "Phone number is required";
 } else {
 $phone = test_input($_POST['phone']);
if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $phone)) {
$phoneErr = "must be in ddd-ddd-dddd format";
}
}
//check zip code field
if (empty($_POST['zip'])) {
$zipErr = "Zip code is required";
} else {
$zip = test_input($_POST['zip']);
if(!preg_match("/^[0-9]{5}-[0-9]{4}$/", $)) {
 $zipErr = "Zip code must be in ddddd-dddd format";
}
}
?>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

Name: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Address:
<input type="text" name="address">
<span class="error">* <?php echo $addressErr;?></span>
<br><br>
 Phone Number:
 <input type="text" name="phone">
 <span class="error"><?php echo $phoneErr;?></span>
 <br><br>
 Zip Code:
 <input type="text" name="zip">
 <span class="error"><?php echo $zipErr;?></span>

 <br><br>
 <input type="submit" name="submit" value="Submit"> 

 </form>


 </body>

</html>
  • 写回答

2条回答 默认 最新

  • doulong2782 2016-04-22 19:31
    关注

    If you have php anywhere in the code, it can't be saved as an .html file. Save it as a .php file and then Apache will call the PHP interpreter.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。