2017-06-21 7 views
0

私はちょうどphpの基礎を学び始め、w3schoolsクラスのコンテンツを理解したいと思っています。jqueryの検証でphpでメールを送る方法

<form role="form" method="POST" id="main-contact-form"> 
<br style="clear:both"> 
    <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3> 
    <div class="form-group"> 
     <input type="text" class="form-control" id="name" name="name" placeholder="Name" required> 
    </div> 
    <div class="form-group"> 
     <input type="text" class="form-control" id="email" name="email" placeholder="Email" required> 
    </div> 
    <div class="form-group"> 
     <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" required> 
    </div> 
    <div class="form-group"> 
     <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required> 
    </div> 
    <div class="form-group"> 
    <textarea class="form-control" type="textarea" id="message" placeholder="Message" maxlength="140" rows="7" name="message"></textarea> 
     <span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>      
    </div> 

    <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button> 
</form> 

その後、私のjqueryのファイルは、次のようになります:私のHTMLは次のようになります

私が見ることができるものから

$('#main-contact-form').submit(function(event){ 
event.preventDefault(); 
$.ajax({ 
    type:'post', 
    url:'sendememail.php', 
    data:$(this).serialize(), 
    success:function(response){ 
     if(response==1) 
     { 

      setInterval(function(){$('.review_form').html('<h5><center><div class="alert alert-success">Review Successfully Submitted......</div></center></h5>');},5); 

     } 
     else 
     { 

      setInterval(function(){$('.review_form').html('<h5><center><div class="alert alert-danger">Sorry Your Review Was Not Submitted......</div></center></h5>');},5); 

     } 
    } 

    }); 
    }); 

、これは電子メールの送信の方法について、多かれ少なかれです。

01:

<?php 
    $to = "[email protected]"; 
    $subject = "My subject"; 
    $txt = "Hello world!"; 
    $headers = "[email protected]" . "\r\n" . 
    "CC: [email protected]"; 

    mail($to,$subject,$txt,$headers); 
?> 

私sendemail.phpは、このようになります

<?php 
extract($_POST); 
$to = $email; 
$subject = $subject; 
$txt = $message; 
$headers = "[email protected]" . "\r\n" . 
"CC: [email protected]"; 

mail($to,$subject,$txt,$headers); 
?> 

私は送信ボタンをクリックすると、私はあなたが画像で見ることができますエラーが出る:

enter image description here

はあなたが助けることができると思います。事前

+0

ページを更新せずにフォームからPHPにデータを送信するには、AJAX http://api.jquery.com/jquery.ajax/ –

答えて

0

おかげで、このコードを試してみてください。

はそれでsendemail.phpと入力してください電子メールのコードのようなメールを送るために新しいページを作成します。

フォームからアクションを削除し、フォームタグに属性IDを追加します。その後

<form role="form" method="POST" id="main-contact-form"> 
<br style="clear:both"> 
     <h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3> 
     <div class="form-group"> 
      <input type="text" class="form-control" id="name" name="name" placeholder="Name" required> 
     </div> 
     <div class="form-group"> 
      <input type="text" class="form-control" id="email" name="email" placeholder="Email" required> 
     </div> 
     <div class="form-group"> 
      <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" required> 
     </div> 
     <div class="form-group"> 
      <input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required> 
     </div> 
     <div class="form-group"> 
     <textarea class="form-control" type="textarea" id="message" placeholder="Message" maxlength="140" rows="7" name="message"></textarea> 
      <span class="help-block"><p id="characterLeft" class="help-block ">You have reached the limit</p></span>      
     </div> 

     <button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button> 
</form> 

、以下に示すあなたのjQueryを変更します。ここでは

<script> 
$('#main-contact-form').submit(function(event){ 
event.preventDefault(); 
$.ajax({ 
    type:'post', 
    url:'sendememail.php', 
    data:$(this).serialize(), 
    success:function(response){ 
     if(response==1) 
     { 

      setInterval(function(){$('.review_form').html('<h5><center><div class="alert alert-success">Review Successfully Submitted......</div></center></h5>');},5); 

     } 
     else 
     { 

      setInterval(function(){$('.review_form').html('<h5><center><div class="alert alert-danger">Sorry Your Review Was Not Submitted......</div></center></h5>');},5); 

     } 
    } 

    }); 
    }); 
</script> 

あなたsendemail.phpを:

<?php 
extract($_POST); 
$to = $email; 
$subject = $subject; 
$txt = $message; 
$headers = "[email protected]" . "\r\n" . 
"CC: [email protected]"; 

mail($to,$subject,$txt,$headers); 
?> 

メールを電子メールのテキストボックスに入力された送信です。

+0

を使用する必要がありますが、電子メールを私に送信したい場合はどうすればよいのですか。私のメールアドレスが[email protected]だとします。私のPHPはどのように見えますか? – Nadj

+0

私の編集コードを見てください。 –

関連する問題