我的代码工作正常,直到我的代码打印在屏幕

时间:2017-10-08 09:29:24

标签: php

 <?php

     $error="";$successMessage="";

    if($_POST) {

    $error="";

    if(!$_POST("email"))
    {
    $error .="An email address is required<br>";
    }//code works fine till here 

   //from here on the code gets printed on screen and is not working 
    if(!$_POST("content")){
    $error.="The content field is required<br>";
    }

    if(!$_POST("subject")){
    $error.="The subject is required<br>";
    }

    if ($_Post['email'] && filter_var($_POST("email"), FILTER_VALIDATE_EMAIL)===false) {
    $error .="$email is not a valid email address. <br>";
    }


    if($error !==""){

    $error='<div class="alert alert-danger" role="alert"><p><strong>There were error(s) in your form</strong></p>'. error . '</div>';
    }

    else
    {
    $emailTo="vibhorvimal5598@gmail.com";

    $subject=$_POST("subject");

    $content=$_POST("content");

    $headers="From:"$_POST('email');

    if(mail($emailTo,$subject,$content,$headers)){
    $successMessage='<div class="alert alert-danger" role="alert"><p><strong>Your message was sent</strong></p>'. error . '</div>';
    }

    }
    ?>

这个代码有什么问题可以请任何人解释,因为我已经检查了语法和错误等等。我不知道出了什么问题以及为什么会这样。 任何人都可以帮助我,并告诉我为什么代码在第一个if语句之后无法正常工作

2 个答案:

答案 0 :(得分:0)

检查你的评论样式它的HTML代码的HTML样式评论

在Php中查看此如何发表评论:https://www.w3schools.com/PhP/php_syntax.asp

更新

像这样使用$error来打印error的实例。

// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
 <?php

     $error="";
     $successMessage="";

    if($_POST) {

    $error="";

    if(!$_POST("email"))
    {
    $error .="An email address is required<br>";
    } //code works fine till here 

   //from here on the code gets printed on screen and is not working 

    if(!$_POST("content")){
    $error.="The content field is required<br>";
    }

    if(!$_POST("subject")){
    $error.="The subject is required<br>";
    }

    if ($_Post['email'] && filter_var($_POST("email"), FILTER_VALIDATE_EMAIL)===false) {
    $error .="$email is not a valid email address. <br>";
    }


    if($error !==""){

    $error='<div class="alert alert-danger" role="alert"><p><strong>There were error(s) in your form</strong></p>'. $error . '</div>';
    }

    else
    {
    $emailTo="vibhorvimal5598@gmail.com";

    $subject=$_POST("subject");

    $content=$_POST("content");

    $headers="From:"$_POST('email');

    if(mail($emailTo,$subject,$content,$headers)){
    $successMessage='<div class="alert alert-danger" role="alert"><p><strong>Your message was sent</strong></p>'. $error . '</div>';
    }

    }
    ?>

答案 1 :(得分:0)

你的代码有很多错误。我刚刚重写了你的代码。在代码下面使用。主要错误在下面提到。

should used  $_POST['email'] instead of $_POST('email')
should used  $_POST['content'] instead of $_POST('content')
should used  $_POST['subject'] instead of $_POST('subject')
should used $error instead of error

if(!empty($ _ POST)){

        if(!$_POST['email'])
        {
        $error .="An email address is required<br>";
        }//code works fine till here 

       //from here on the code gets printed on screen and is not working 
        if(!$_POST['content']){
        $error .="The content field is required<br>";
        }

        if(!$_POST["subject"]){
        $error .="The subject is required<br>";
        }

        if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)===false) {
        $error .="$email is not a valid email address. <br>";
        }

        if($error !==""){
        $error = '<div class="alert alert-danger" role="alert"><p><strong>There were error(s) in your form</strong></p>'. $error . '</div>';
        }

        else{
            $emailTo="vibhorvimal5598@gmail.com";

            $subject=$_POST["subject"];

            $content=$_POST["content"];

            $headers="From:".$_POST['email']."";

            if(mail($emailTo,$subject,$content,$headers)){
                $successMessage='<div class="alert alert-success" role="alert"><p><strong>Your message was sent</strong></p></div>';
            }
        }
    }