dousi7579 2019-02-23 02:21
浏览 91
已采纳

PHP文件不接收来自AJAX帖子的数据

jQuery code:

$(document).ready(function(){
    $('.tablePend tbody').on('click','.btn',function(){
        // var id=$('#test-pend').val();
        var currow=$(this).closest("tr");
        var colId = currow.find('td:eq(3)').text();
        var id = colId;
        alert(id);
        $.post("../admin/detail.php", {
            pid:id
        });
    });
});

So the code above works perfectly, the alert shows the result that I want

<?php
include('dbh.inc.php');
include_once('header.php');

if(isset($_POST['pid'])) {
    $id = mysqli_real_escape_string($conn, $_POST['pid']);
    $sql= "SELECT event_name, event_desc, event_date, event_id FROM events WHERE event_id='$id'";
    $result= mysqli_query($conn, $sql);
    echo $id;
} else {
    echo 'nothing';
}

include_once 'footer.php';
?>

So right here, the PHP automatically goes to the else statement which shows nothing, I don't know what went wrong, I've tried all possible ways, for days, this is really making me frustrated, and I also believe that I made a simple mistake but I can't find it.

  • 写回答

2条回答 默认 最新

  • down100009 2019-02-23 07:28
    关注

    I had to do some sleuthing to come up with this answer.

    Your $.post() jQuery code does not include a success (or error) handler. That means no matter what happens, you won't see the results of what your PHP file says. But in your question, you say you can see nothing. So, this makes me suspect that you are not actually submitting via AJAX at all.

    You have shown us jQuery code where you have an event handler attached to your button. My guess is your button is inside a a <form>. When you click a button in a form, it will submit it. If you want your jQuery to handle it instead, you have to explicitly specify that the normal form submission should not happen. And your your jQuery code does not do that.

    So what all this means is that when you click the button, the form is submitted normally. Your event handler maybe fires, and that code starts, but meanwhile the browser has moved on to your <form> target, and the jQuery never gets far. Your <form> does not actually include an id, you set that dynamically with jQuery. So you wind up on the <form> target, with no id POSTed, and you see nothing on the page.

    The fix is to stop the normal form submission:

    $(document).ready(function(){
        $('.tablePend tbody').on('click','.btn',function(event){
            event.preventDefault();
            // rest of your code
    

    Note I have added event as a parameter passed to the event handler, and then used its preventDefault() method. There is an example of this shown in the .post() documentation.

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

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端