-2
 <?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>';
    }

    }
    ?>

what is wrong in this code can anyone please explain because I already checked for the syntax and error and all . I don't know what is wrong and why this is happening. Can anyone please help me out and tell why the code is not working correctly after first if statement

Vibhor
  • 9
  • 3
  • 2
    Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – Qirel Oct 08 '17 at 09:35
  • Couple of issues I see: you are outputting 'error' twice instead of '$error'. Also, when setting $headers, you need to concatenate the two values together using a period. So that line should be "From:" . $_POST('email'); – Dan Oct 08 '17 at 09:55

2 Answers2

0

Check You Comment Style Its Html Style Comment in php code

Check This how To Give Comments in Php: https://www.w3schools.com/PhP/php_syntax.asp


Updated:

Use $error like this to print instance of 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>';
    }

    }
    ?>
Community
  • 1
  • 1
0

Your code having lots of mistake . i have just rewrite your code. used below code.main mistakes are mentioned in below.

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>';
            }
        }
    }
sheraz
  • 434
  • 4
  • 8