duanhong8839 2019-03-14 08:21
浏览 87

PHP表单未正确验证表单字段

I'm learning some more PHP and after PHP itself to work, I can't seem to get it to validate any form field correctly. My goal is to check if the firstname field is empty, and if it is, it will give a message in a red color. The message in the red works, but only because the echo script is being called by the form submission, not because it has detected any empty field, because when I made an else statement to say "wassup" if its not empty, I got the same message for when the field is empty. Also, is there a way to check off multiple input fields at once like you could with JavaScript? For example if input1 == '' || input2 == '' and so on. Here is my HTML:

<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="welcome.php" method="post">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text"> 
Middle name <input name="middlename" type="text"> 
Surname <input name="lastname" type="text">  
Age <input name="age" type="number"> 
Date of birth <input name="dob"  type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent 
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input name="country" type="text"> State <input type="text"> 
City <input name="city" type="text">
Street number <input name="streetno" type="number"> 
Street name <input name="streetname" type="text"> <br><br>
Suburb <input name="suburb" type="text"> Postcode <input name="postcode" type="number"> 
If none of these apply to your accommodations, enter a typed location here <input  type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text"> 
First   time job seeker <input type="checkbox" name="check1" value="ftjb"> 
I'm a student <input type="checkbox" name="check2" value="ias"> 
Previous &/or most recent acedemic title <input name="school" type="text"> 
First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq"> 
I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival <input name="reason" type="text"> 
Date of arrival <input name="arrival" type="date"> 
Amount of stay expectancy 
<input type="checkbox" name="check3">Temporary 
<input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input name='signiture' type="text"> 
</fieldset>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html> 

Here is my PHP code:

<?php
$firstname = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$dob = $_POST['dob'];
$country = $_POST['country'];
$city = $_POST['city'];
$suburb = $_POST['suburb'];
$postcode = $_POST['postcode'];
$streetno = $_POST['streetno'];
$streetname = $_POST['streetname'];
$suburb = $_POST['suburb'];
$job = $_POST['job'];
$school = $_POST['school'];
$reason = $_POST['reason'];
$arrival = $_POST['arrival'];
$signiture = $_POST['signiture'];
if (isset($_POST['submit'])) {
    if (empty($_POST[$firstname])) {
        echo '<p style="color: red; text-align: center">Your first name is required</p>';
    } else {
        echo "wassaup";
    }
}





?>
  • 写回答

6条回答 默认 最新

  • doufu9521 2019-03-14 08:23
    关注

    change this if (empty($_POST[$name])) { to if (empty($_POST['firstname'])) { and check again. your syntax is wrong that's why it is not working.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀