weixin_33720078 2014-07-09 14:09 采纳率: 0%
浏览 13

AJAX通话未执行

I just come arround the corner with AJAX calls and trying to learn it. I have a form i am trying to push through an AJAX call, but getting a constant error it can't send, and I cant wrap my head arround it why it doesn't work.

Form:

<form class="form-horizontal" id="contact-form"  method="post">
      <div class="control-group">
            <div class="controls">
                <input type="text" name="name" id="name" placeholder="Naam" class="form-control input-lg">                                
             </div>
      </div>
      <div class="control-group">
             <div class="controls">
                <input type="text" name="email" id="email" placeholder="Email adres" class="form-control input-lg">
             </div>
      </div>
      <div class="control-group">
             <div class="controls">
                 <textarea name="message" id="message" rows="8" class="form-control input-lg" placeholder="Bericht"></textarea>
             </div>
       </div>
       <div class="form-actions">
              <button type="submit" id="submit" class="btn btn-default btn-lg btn-block">Verstuur Bericht</button>
       </div>
       <div id="succes">
       </div>
</form>

The Ajax Call

$('form').bind('submit',function(){
    $.ajax({
        type    : 'POST',
        url     : 'inc/actions/sendmail.php',
        data    : $(this).serialize(),
        cache   : false,
        dataType: 'text',
        success : function (serverResponse) { alert('mail sent successfully.'); },
        error   : function (jqXHR, textStatus, errorThrown) {alert('error sending mail');}
    });
})

And the sendmail.php

$name = mysqli_real_escape_string($con,$_POST["naam"]);
$from = mysqli_real_escape_string($con,$_POST["email"]);                                    
$message = mysqli_real_escape_string($con,$_POST["bericht"]);

$message = wordwrap($message, 70);  
$to = "example@myemail.nl";
$subject = "Een gebruiker heeft een vraag of opmerking";

$body = "Some messages and colloring, to much to paste cause it's inline-markup";

$headers = "MIME-Version: 1.0" . "
";
$headers .= "Content-type:text/html;charset=UTF-8" . "
";
$headers .= "From: noreply@ajaxfailure.nl";

if ($name == ''){
    echo '<p>U heeft geen naam ingevuld</p>';    
}else if($from == ''){
    echo '<p>U heeft geen emailadres achter gelaten</p>';
}else if($body == ''){
    echo '<p>U heeft geen bericht ingevuld niet ingevuld';
}else {
    if (mail ($to, $subject, $body, $headers)) {
        echo '<p>Uw bericht is verzonden.</p>';
    } else {
        echo '<p>Er is iets mis gegaan bij het verzenden van de data, probeer het opnieuw.</p>';
    }
} 

Everytime it returns me the error alert from the AJAX call and doesnt execute anything. My console doesn't spot an error either and remains competely blank.

I checked the console as per suggestion on the network tab and it doesnt even get on the page itself. There is no 404 error on that part. It seems the request gets canceled.

Screendump of console / network

  • 写回答

4条回答 默认 最新

  • weixin_33739646 2014-07-09 14:14
    关注

    Your input name attributes dont match with your php:

    $name = mysqli_real_escape_string($con,$_POST["name"]); //<-- not 'naam'
    $from = mysqli_real_escape_string($con,$_POST["email"]);                                    
    $message = mysqli_real_escape_string($con,$_POST["message"]); //<-- not 'bericht'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败