doubu8643 2015-06-03 11:16 采纳率: 100%
浏览 24
已采纳

ajax发布请求到没有响应的PHP(每次都是假设)

I'm using jquery multiselect plugin and I want to perform an ajax request on a select/deselect event.

My problem: When I send the request to the php file, ISSET(VAR) returns every time false so that I can't pass variable to the php file.

But Firebug extension for Chrome/Firefox shows me that the POST value is set right POST -> "Response myvar" but GET is empty.

How do I pass the variable to the php file? (I've searched arround the web but found nothing.)

My script, where this pointer is from the multiselect plugin and afterSelect returns if a object is selected

afterSelect: function()
    {
        this.qs1.cache();
        this.qs2.cache();
        count++;     

        var dataString = "count=" + count;

        if ( count > 0 )
        {
            $.ajax
            ({
                type: 'POST',
                url: 'page-to-send-request.php',
                data: dataString,
                success: function()
                {
                    $("#div-to-load").load("page.php #div-to-load").fadeIn('slow');
                },
                error: function (xhr, ajaxOptions, thrownError)
                {
                    alert(xhr.status);
                    alert(thrownError);
                }
             });
        }
    },

The php page to load has for test only

if($_POST['count'])
{
  $count = $_POST['count'];
  echo "Count " .$count;
}
else{ echo "FALSE"; }

Expected result should be

Count 5

But real output is

FALSE

  • 写回答

1条回答 默认 最新

  • duanmoen784988 2015-06-03 11:23
    关注

    1st try

    success: function(data)
           {
             console.log(data);
           },
    

    it should output "Count " .$count; if not

    try to use

    var dataString = {count: count};
    

    and then you can use

    $("#div-to-load").html(data).fadeIn('slow');
    

    the reason of return false .. you used the .load to load the php page .. when you load it .. javascript know it as a separated page .. so $_POST['count'] is undefined in this case . and It will return False always

    your success function should be like this

    success: function(data)
               {
                  $("#div-to-load").html(data).fadeIn('slow');
               },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿