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条)

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算