我刚刚开始学习并整理了以下表格。我的困惑是当我使用空来验证用户是否选择或输入了任何信息时,代码会生成以下错误。
我注意到我是否有以下代码行
if(empty($gender)) {
$errormessage[2] = "Please select your gender";
}
作为
if(empty($_POST["gender"])) {
$errormessage[2] = "Please select your gender";
}
和
if(empty($gender)) {
$errormessage[2] = "Please select your gender";
}
作为
if(empty($_POST["gender"])) {
$errormessage[2] = "Please select your gender";
}
我没有看到错误消息。我认为正在生成错误消息是因为性别和媒体表单元素的代码行$ _POST分别是单选按钮和复选框。但是,如果我想在顶部初始化所有变量,那么最好的方法是什么?
/* <?php
if(isset($_POST["submit"])) {
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$gender = $_POST["gender"];
$age = $_POST["age"];
$address = $_POST["address"];
$media = $_POST['media'];
$errormessage = array();
if(empty($fname)) {
$errormessage[0] = "Please enter your first name";
}
if(empty($lname)) {
$errormessage[1] = "Please enter your last name";
}
if(empty($gender)) {
$errormessage[2] = "Please select your gender";
}
if(empty($age)) {
$errormessage[3] = "Please select your age";
}
if(empty($address)) {
$errormessage[4] = "Please enter your address";
}
if(empty($media)) {
$errormessage = "Please select the type of media";
}
}
?>
<html>
<head>
<title>Sample Registration</title>
</head>
<body>
<h2>Sample registration</h4>
<form name="registration" method="post" action="registration.php">
<div>
First Name: <br />
<input type="text" name="fname" value="">
</div>
<div>
Last Name: <br />
<input type="text" name="lname" value="">
</div>
<div>
Gender: <br />
male<input type="radio" name="gender" value="male">
female<input type="radio" name="gender" value="female">
</div>
<div>
Age: <br />
<select name="age">
<option value="">Please select your age</option>
<option value="18-25">18-25</option>
<option value="26-33">26-33</option>
</select>
</div>
<div>
Address: <br />
<textarea name="address" cols="10" rows="10"></textarea>
</div>
<div>
Sign-me up: <br />
<input type="checkbox" name="media['newsletter']" value="newsletter"> newsletter
<input type="checkbox" name="media['specials']" value="specials"> specials
<input type="checkbox" name="media['events']" value="events"> events
<div>
<input type="submit" name="submit" value="submit">
</div>
</form>
</body>
</html>
*/
/ *错误信息* /
! ) Notice: Undefined index: gender in C:\Program Files\EasyPHP-5.3.5.0\www\registration.php on line 7
Call Stack
# Time Memory Function Location
1 0.0004 341792 {main}( ) ..\registration.php:0
Dump $_SERVER
$_SERVER['REMOTE_ADDR'] =
string '127.0.0.1' (length=9)
$_SERVER['REQUEST_METHOD'] =
string 'POST' (length=4)
$_SERVER['REQUEST_URI'] =
string '/registration.php' (length=17)
Variables in local scope (#1)
$address =
Undefined
$age =
Undefined
$errormessage =
Undefined
$errormsg =
Undefined
$fname =
string '' (length=0)
$gender =
Undefined
$lname =
string '' (length=0)
$media =
Undefined
( ! ) Notice: Undefined index: media in C:\Program Files\EasyPHP-5.3.5.0\www\registration.php on line 10
Call Stack
# Time Memory Function Location
1 0.0004 341792 {main}( ) ..\registration.php:0
Variables in local scope (#1)
$address =
string '' (length=0)
$age =
string '' (length=0)
$errormessage =
Undefined
$errormsg =
Undefined
$fname =
string '' (length=0)
$gender =
null
$lname =
string '' (length=0)
$media =
Undefined
答案 0 :(得分:4)
在访问之前,您需要检查HTTP POST请求中是否实际设置了$_POST['gender']
和$_POST['media']
个变量;初始化每个$_POST
var的更好的解决方案可能是这样的:
$fname = isset( $_POST['fname'] ) ? $_POST['fname'] : '';
上面的三元赋值相当于为您感兴趣的每个$_POST
变量运行以下逻辑表达式:
if( isset( $_POST['fname'] ) ) {
$fname = $_POST['fname'];
} else {
$fname = '';
}
如果您实施其中任何一项,如果您在empty()
上运行$gender
,则不会收到通知;此外,如果未设置$_POST['gender']
,empty()
仍会按照您的预期行事。它增加了一些冗长,但要重写你的例子你可能会尝试:
if( isset( $_POST["submit"] ) ) {
$fname = isset( $_POST['fname'] ) ? $_POST["fname"] : '';
$lname = isset( $_POST['lname'] ) ? $_POST["lname"] : '';
$gender = isset( $_POST['gender'] ) ? $_POST["gender"] : '';
$age = isset( $_POST['age'] ) ? $_POST["age"] : '';
$address = isset( $_POST['address'] ) ? $_POST["address"] : '';
$media = isset( $_POST['media'] ) ? $_POST['media'] : '';
$errormessage = array();
if( empty( $fname ) )
$errormessage[] = "Please enter your first name";
if( empty( $lname ) )
$errormessage[] = "Please enter your last name";
if( empty( $gender ) )
$errormessage[] = "Please select your gender";
if( empty( $age ) )
$errormessage[] = "Please select your age";
if( empty( $address ) )
$errormessage[] = "Please enter your address";
if( empty( $media ) )
$errormessage[] = "Please select the type of media";
}
当然,如果您打算在SQL上下文中使用它,您需要清理数据,但这至少可以解决您所面临的错误。
如果你有很多这些变量 - 或者经常重复这个任务 - 你可以考虑将它变成一个函数!
答案 1 :(得分:2)
尝试使用!isset()
代替empty()
。
作为旁注,如果warning
值不存在,您分配变量的方式将创建$_POST
通知。如果您想删除这些通知,请使用@ Ryan的解决方案。