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

报告相同问题?