Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 尝试在字段下方显示消息时出错_Php_Html - Fatal编程技术网

Php 尝试在字段下方显示消息时出错

Php 尝试在字段下方显示消息时出错,php,html,Php,Html,如果我的联系人表单的必填字段为空,我想在该字段下方显示一条错误消息。我跟踪了这个链接 http://stackoverflow.com/questions/11237655/display-form-validation-errors-next-to-each-field 我的代码如下: send.php $error = array( "name" => "", "email" => "", "message" => ""

如果我的联系人表单的必填字段为空,我想在该字段下方显示一条错误消息。我跟踪了这个链接

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()$\u POST[“email”])

我的代码中有什么错误?

修复检查。也许最好加上


在将字符串传递到
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!";
  }
?>


if(空($\u POST[“email”])
其他问题同上,我按照您的说明更正了,我甚至按照上面链接中的教程以html格式进行了编辑。但是我没有收到消息你说没有收到消息是什么意思?你是在给自己发送邮件吗?你是如何输出消息的?
isset
如果发送了一个post字段
email
,即使它是空的,没有值,也会返回true谢谢,但即使我添加了,我也不会收到错误消息为什么使用
empty
?由于上面已经设置了一个值,如果($email){//has email value}也这样做,则不会显示该值吗?@PuneethP设置错误时不会显示该值。之后必须打印错误。添加
print\r($error)
并查看它是否在there@PuneethP,正如他所说,要显示错误消息,您需要使用
echo
print\r
@bystwn22。我想您的意思是,如果(!$email)感叹号丢失,则将其更新为

if(!isset($_POST["email"]) || empty($_POST["email"]))
<?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!";
  }
?>