如果字段为空,我想在联系表单的必填字段下面给出错误消息。我按照这个链接
http://stackoverflow.com/questions/11237655/display-form-validation-errors-next-to-each-field
我的代码如下:
send.php
$error = array(
"name" => "",
"email" => "",
"message" => ""
);
if(empty()$_POST["email"])
$error["email"] = "Email is required!";
if(empty()$_POST["name"])
$error["name"] = "Name is required!";
if(empty()$_POST["message"])
$error["message"] = "Message is required!";
但我得到错误:
Parse error: syntax error, unexpected ')', expecting T_STRING or T_VARIABLE or '$' in send.php on line 20
第20行是if(empty()$_POST["email"])
我的代码中有什么错误?
答案 0 :(得分:1)
这
if(empty()$_POST["email"])
到
if(!isset($_POST["email"]) || empty($_POST["email"]))
答案 1 :(得分:0)
尝试使用此代码,然后将字符串传递给empty
,别忘了trim
,并使用isset
<?php
$error = array(
"name" => "",
"email" => "",
"message" => ""
);
$email = ( isset( $_POST["email"] ) ) ? trim( $_POST["email"] ) : false;
$name = ( isset( $_POST["name"] ) ) ? trim( $_POST["name"] ) : false;
$message = ( isset( $_POST["message"] ) ) ? trim( $_POST["message"] ) : false;
if ( !$emai ) {
$error["email"] = "Email is required!";
}
if ( !$name ) {
$error["name"] = "Name is required!";
}
if ( !$message ) {
$error["message"] = "Message is required!";
}
?>