Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Ajax处理PHP错误_Php_Jquery_Ajax - Fatal编程技术网

用Ajax处理PHP错误

用Ajax处理PHP错误,php,jquery,ajax,Php,Jquery,Ajax,试图通过ajax提交表单并处理PHP错误 这是我的表格 <div class="boxFocus"> <?php echo $message_text; echo $errors; ?> <form method="post" action="<?php bloginfo('template_directory'); ?>/includes/form-handler.php" id="contactfrm" role="form">

试图通过ajax提交表单并处理PHP错误

这是我的表格

<div class="boxFocus">
   <?php echo $message_text;  echo $errors;  ?>
   <form method="post" action="<?php bloginfo('template_directory'); ?>/includes/form-handler.php" id="contactfrm" role="form">
      <div class="form-group">
         <label for="name">Name</label>
         <input type="text" class="form-control" name="name" id="name" placeholder="Enter name"  title="Please enter your name (at least 2 characters)" required/>
      </div>
      <div class="form-group">
         <label for="email">Email</label>
         <input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address"/>
      </div>
      <div class="form-group">
         <label for="phone">Phone</label>
         <input name="phone" class="form-control required digits" type="tel" id="phone" size="30" value="" placeholder="Enter email phone" title="Please enter a valid phone number (at least 10 characters)" required>
      </div>
      <div class="form-group">
         <label for="comments">Comments</label>
         <textarea name="message" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)" required></textarea>
      </div>
      <div class="result"></div>
      <button name="submit" type="submit" class="btn btn-primary" id="submitform"> Submit</button>
   </form>
   <div class="result"></div>
</div>
这是表单处理程序

<?php


if(isset($_POST['submit'])) {

  //include validation class
  include 'validate.class.php';

  //assign post data to variables
  $name = @($_POST['name']);
  $email = @($_POST['email']);
  $message = @($_POST['message']);
  $phone = @$_POST["phone"];

  //echo $name, $email, $message, $phone;


  //start validating our form
  $v = new validate();
  $v->validateStr($name, "name", 3, 75);
  $v->validateEmail($email, "email");
  $v->validateStr($message, "message", 5, 1000);
  $v->validateStr($phone, "phone", 11, 13);

  if(!$v->hasErrors()) {

        $to = "lukelangfield001@googlemail.com";
        $subject = "Website contact form ";

        $mailbody = $message . "\n" . "from " . $name . "\n" . $phone;
        $headers = "From: $email";

        mail($to, $subject, $mailbody, $headers);

        echo "success";

    } else {
    //set the number of errors message
    $message_text = $v->errorNumMessage();

    //store the errors list in a variable
    $errors = $v->displayErrors();

    //get the individual error messages
    $nameErr = $v->getError("name");
    $emailErr = $v->getError("email");
    $messageErr = $v->getError("message");
    $phoneErr = $v->getError("phone");

   echo $message_text;  echo $errors;  
  }//end error check

}// end isset

您可以通过更改
成功
回调来检查返回数据的内容,如

success: function(response){
    console.log(response);
    //do stuff here like add messages in your <div>
},
成功:功能(响应){
控制台日志(响应);
//在这里做一些事情,比如在你的
},

一旦您检查了实际输出,也许您可以更新您的问题以供我们查看,并在输出方面为您提供更多帮助。

您没有预期的结果,因为您正在发送表单

您必须在“点击”功能中使用
事件
参数,并防止出现默认行为,即发送表单。
代码的简单版本可能如下所示:

   $("#submitform").click(function(e) {
     e.preventDefault();
     window.console.log('will not submit a form');
     // ajax request
   });
   $("#submitform").click(function(e){
     e.preventDefault();

     var form_data = $("#contactfrm").serialize();
     $.ajax({
        type: "POST",
        url: "/ltlcreation-new/wordpress/wp-content/themes/LTLCreation/includes/form-handler.php",
        data: form_data,
        error: function(){
            alert("failed");
        },
        success: function(){
            alert("success");
        },
        })
    });
您的代码应该如下所示:

   $("#submitform").click(function(e) {
     e.preventDefault();
     window.console.log('will not submit a form');
     // ajax request
   });
   $("#submitform").click(function(e){
     e.preventDefault();

     var form_data = $("#contactfrm").serialize();
     $.ajax({
        type: "POST",
        url: "/ltlcreation-new/wordpress/wp-content/themes/LTLCreation/includes/form-handler.php",
        data: form_data,
        error: function(){
            alert("failed");
        },
        success: function(){
            alert("success");
        },
        })
    });

我可以将PHP代码片段包装在try/catch块中,如果PHP失败,您可以将错误打包到所需的HTML中并返回到浏览器。

您必须取消注释
e.preventDefault()行以及定义
e
参数