dounabi6295 2019-03-12 08:20
浏览 140
已采纳

Ajax调用成功但$ _POST ['value']为空

ISSUE SOLVED

The issues seemed to be browser related. Tried with a different system and it works fine.

I am coding a live search to show the details of users from database using ajax and php. The input from the form fields are getting passed to the ajax file properly. I checked with console.log() and it is printing properly. After that, the Ajax call takes place without any errors. But when trying to echo the $_POST['search'], it is printing empty space.

searchform.php

<input type="text" placeholder="Search" class="search"/>
<div class="search-result"></div>

search.js

    $(function(){
    $('.search').keyup(function(){
        var search = $(this).val();
        //console.log(search);
        $.ajax({
            url:'search.php',
            data:{search:search,},
            type:'POST',
            success:function(data){
                console.log(data);
                $('.search-result').html(data);
            },
            error:function(){
                console.log("error");
            }
        });
    });
});

search.php

 <?php

include('core/init.php');
echo $_POST['search'];

?>

EDITED

I figured out the issue, but no idea about the cause and no solution found. It is basically that the include('core/init.php'); is not getting included. I tried var_dump(include('core/init.php')); and it is returning bool(false).The path is correct. Is there something that needs to be done when file with includes is called using ajax.?

  • 写回答

3条回答 默认 最新

  • dtnnpt11795 2019-03-12 08:29
    关注

    First, you change this data:{search:search,} to this data:{search:search}

    Second, try to change your echo in your search.php become this echo json_encode($_POST['search']);

    For more complex checking, you can inspect element (press F12 from your browser), go to network tab and open in new tab your search.php file. It is show your expected value?

    If search.php shown the expected value, try to hard refresh your browser in windows (ctrl + F5) to try updated your js

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA