0

如果该字段为空,我想在我的联系表的必填字段下方给出一条错误消息。我跟着这个链接

http://stackoverflow.com/questions/11237655/display-form-validation-errors-next-to-each-field

我的代码如下:

发送.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"])

我的代码有什么错误?

4

2 回答 2

1

修复支票。并且添加isset可能会更好

if(empty()$_POST["email"])

if(!isset($_POST["email"]) || empty($_POST["email"]))
于 2013-07-22T21:40:08.540 回答
0

试试这段代码,在传递字符串之前empty不要忘记做trimisset也可以使用

<?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!";
  }
?>
于 2013-07-22T21:49:55.480 回答