简体   繁体   English

Ajax成功消息不起作用

[英]Ajax success message is not working

I want to submit form without refreshing page and display success/error message under form, also in case of success reset form. 我想提交表单而不刷新页面,并且在成功重置表单的情况下也要在表单下显示成功/错误消息。 But I guess, I am doing something wrong in ajax code, php works fine. 但是我想我在ajax代码中做错了事,php可以正常工作。 Ajax: 阿贾克斯:

$("#usignupform").validate({
    submitHandler:function() {
        var $form = $(form),
        $messageSuccess = $('#mesSuccess'),
        $messageError = $('#mesError');
        $.ajax({
            url: "investor-invite.php",
            type: "POST",
            data: $(this).serialize(),
            complete: function(data) {
                if (data.status == 'success') {
                    $messageSuccess.removeClass('hidden');
                    $messageError.addClass('hidden');

                    // Reset Form
                    $form.reset();

                    return;

                } else {
                    $messageError.removeClass('hidden');
                    $messageSuccess.addClass('hidden');
                }
            }
        });
        return false;
    }
});

PHP: PHP:

function NewUser(){ 
    $data = mysql_query($query)or die(mysql_error()); 

    if($data){
        $response_array['status'] = 'success';  
    } else {
        $response_array['status'] = 'error';  
    }
}
if(isset($_POST['submit'])){ 
    NewUser();
    }

HTML: HTML:

<form>
    --all fields--

    <button type="submit" name="submit" id="submitButton">
    Request Invite</button>
    <div class="alert alert-success hidden" id="mesSuccess">
        <strong>Success!</strong> Your message has been sent to us.
    </div>
    <div class="alert alert-danger hidden" id="mesError">
        <strong>Error!</strong> There was an error sending your message.
    </div>      
</form>

you need to echo content on the php page then this will return success message .Ajax call will get the response if there is html or some printed data in the php file or any target file. 您需要在php页面上回显内容,这将返回成功消息。如果php文件或任何目标文件中包含html或某些打印数据,则Ajax调用将获得响应。

try like this 这样尝试

    function NewUser(){ 
         $data = mysqli_query($query)or die(mysql_error()); 

         if($data){
            $response_array['status'] = 'success';  
               }else {
            $response_array['status'] = 'error';  
          }
return $response_array;
        }
    $data=NewUser();
    echo json_encode($data);

and update the ajax function 并更新ajax功能

$("#usignupform").validate({
                  submitHandler:function() {
                  var $form = $(form),
                   $messageSuccess = $('#mesSuccess'),
                   $messageError = $('#mesError');
             $.ajax({
                   url: "investor-invite.php",
                   type: "POST",
                   data: $(this).serialize(),
                    dataType: 'json',                  //add this to get the json response
                   complete: function(data) {
                        if (data.status == 'success') {

                        $messageSuccess.removeClass('hidden');
                        $messageError.addClass('hidden');

                        // Reset Form
                        $form.reset();


                    return;

                } else {

                    $messageError.removeClass('hidden');
                    $messageSuccess.addClass('hidden');
                }
            }
  });
  return false;
}
});

Update please remove Depricated mysql_* from your code and use mysqli_* or pdo queries 更新,请从您的代码中删除Depricated mysql_ *并使用mysqli_ *pdo查询

where is the print statement and JSON Header 打印语句和JSON标头在哪里

<?PHP
header('Content-Type: application/json');
echo json_encode($data);

Try This, 尝试这个,

 $.ajax({
                type: "post",
                url : "ajax.php",
                dataType: 'json',
                data:{ name : name,claass : claass,mobile : mobile},
                success : function(data){
                    alert("success");
                }
            },"json");

in php, 在php中

$arr= ['fname'=>$name,'classs'=>$class,'mobile'=>$mobile];
echo json_encode($arr);

NB : Try With Your post variables. 注意:尝试使用post变量。 and array variables, I just coppy pasted my code for quick answer !!!!!! 和数组变量,我只是coppy粘贴了我的代码以便快速回答!!!!!!

Rather than using complete, use success. 与其使用完整,不如使用成功。 and if you want to be able to directly reference the name value pair then you will need to parse the data returned into json as your php currently only sends back the data as a string. 并且,如果您希望能够直接引用名称值对,那么您将需要解析返回到json的数据,因为您的php当前仅以字符串形式发送回数据。

$.ajax({url:'investor-invite.php',
 type:'POST',
 success: function checkData(data){
  dataObj = JSON.parse(data);
  if(dataObj.status == success){
   ...
  }
  else{
   ...
  }
 }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM