2017-10-08 6 views
-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="[email protected]"; 

    $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

可能な重複[PHPコードが実行され、代わりにコードがページに表示されていません]( https://stackoverflow.com/質問/ 5121495/php-code-is-being-being-with-pageの代わりにコード) – Qirel

+0

私が見る問題のカップル: 'エラー'の代わりに 'エラー'を2回出力しています。また、$ headersを設定する場合は、ピリオドを使用して2つの値を連結する必要があります。その行は「From:」でなければなりません。 $ _POST( 'email'); – Dan

答えて

0

はあなたがPHPでのコメントを与える方法これを確認するPHPコード

でスタイルそのHTMLスタイルのコメントをコメントチェックした場合、誰もが私を助け、コードは最初の後に正常に動作していない理由を教えてくださいことができます: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="[email protected]"; 

    $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>'; 
    } 

    } 
    ?> 
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 

場合(!空($ _ 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="[email protected]"; 

      $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>'; 
      } 
     } 
    } 
関連する問題