简体   繁体   English

PHP + jQuery:ajax回调不起作用

[英]PHP + jQuery: ajax callback not working

I need your help to try to sort out an issue with ajax callback. 我需要您的帮助,以尝试解决ajax回调的问题。 What happen is that the php script is called without issues, the query works fine and it generate the json output, but the callback doesn't works. 发生的情况是调用了php脚本没有问题,查询工作正常并且生成了json输出,但是回调不起作用。 No div is displayed on success and no changes on class happens. 成功时不会显示div,也不会对类进行任何更改。

This is my form 这是我的表格

<form class="form-inline">
<input type="hidden" id="id_pro" value="1">
<input type="hidden" id="status" value="1">
<button id="submit" type="button" class="btn btn-link">
<span id="check" class="glyphicon glyphicon-check" title="my tytle"></span>
</button>
</form>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Form Submitted Success</span>

This is the js part 这是js部分

$(function() {
 $("#submit").click(function() {
   var id_pro = $("#id_pro").val();
   var status = $("#status").val();
   var dataString = 'id_pro='+ id_pro + '&status=' + status;

   if(id_pro=='' || status=='')
   {
      $('.success').fadeOut(200).hide();
      $('.error').fadeOut(200).show();
   }
   else
   {
      $.ajax({
      type: "POST",
      url: "myphppage.php",
      data: dataString,
      datatype: 'json',
      success: function(data)
      {
          if(data.result=='1')
          {
            $('.success').fadeIn(200).show();
            $('.error').fadeOut(200).hide();
            $("#check").attr('class', data.check);
          }
       }
      });
    }
return false;
});
});

And this is the php part 这是PHP部分

<? 

    if($_POST)
    {
        $id_pro=$_POST['id_pro'];
        $status=$_POST['status'];
        if($status==0){$status=1;}else{$status=0;}
        if ($mysqli->query("UPDATE mytable SET online=".$status." WHERE id=".$id_pro." ") === TRUE) {
            header("Content-type: application/json");
            $data = array('check'=>'new_class','check_text'=>'new text','result'=>'1');
            print json_encode($data);   
        }
        else
        {
            header("Content-type: application/json");
            $data = array('result'=>'0');
            print json_encode($data);   
        }
        $result->close();
    }else { }
?>

Any idea? 任何想法? Thank you in advance 先感谢您

error 500 means error in php and in your php don't see defined $mysqli and $result i think here is your problem. 错误500表示php中的错误,并且您的php中没有看到已定义的$ mysqli和$ result,我认为这是您的问题。

better PHP looks like this but must define connect to DB 更好的PHP如下所示,但必须定义连接到数据库的连接

<?php
header("Content-type: application/json");
$data = array('result'=>'0');

if ($_SERVER['REQUEST_METHOD'] == 'post' )
{
    $id_pro = $_POST['id_pro'];
    $status = ($_POST['status'] == 0) ? 1 : 0; // if($status==0){$status=1;}else{$status=0;}

    // define $mysqli
    if ($mysqli->query("UPDATE mytable SET online=".$status." WHERE id=".$id_pro." ") === TRUE) {
        $data = array('check'=>'new_class','check_text'=>'new text','result'=>'1');
    }
    // $result->close(); // ????
}

print json_encode($data);

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

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