PHP结果如何通过ajax成功函数

时间:2019-08-10 05:13:57

标签: php ajax

我没有从ajax获得成功消息,我在成功函数中调用了该消息。 请检查我的 index.php contact-form-script.js form-process.php

的代码
 $("#contactForm").validator().on("submit", function (event) {
    if (event.isDefaultPrevented()) {
        // handle the invalid form...
        formError();
        submitMSG(false, "Did you fill in the form properly?");
    } else {
        // everything looks good!
        event.preventDefault();
        submitForm();
    }
});

function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#sugname").val()
    var email = $("#sugemail").val();
    var mobile = $("#sugmobile").val();
    var message = $("#sugmessage").val();


    $.ajax({
        type: "POST",
        url: "form-process.php",

        data: "name=" + name + "&email=" + email + "&mobile=" + mobile + "&message=" + message,
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else {
                formError();
                submitMSG(false,text);
            }
        }
    });
}

1 个答案:

答案 0 :(得分:0)

进行如下更改:

 //if errormsg not empty 
 if(!empty($errorMSG)){
  //print 
  echo $errorMSG;
 }else{
  //execute query
  include('dbconfig.php');
  $sql="insert into suggestions(name,contactno,mailid,jfsuggestion) values 
  ('".$name."','".$mobile."','".$email."','".$message."')";
  $success = mysqli_query($conn, $sql);
  if($success){
  echo "success";
  }
 }