使用jquery AJAX将数据发送到php并检索响应

时间:2015-03-01 18:32:42

标签: php jquery mysql ajax mysqli

我试图这样做: 当用户点击具有mysqli行的briefinfo div时,将学生ID发送到id.php,然后php文件将通过学生ID获取所有数据并将其回显,这样我就可以使用ajax将其放入我的原始文件中的index.php

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>    
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    //in your Javascript, put
    $(document).ready ( function () {
    $("#briefinfo").click (function(){
    $.ajax({  
        type: 'POST',  
        url: 'id.php', 
        var studentid = "<?php echo $studentid; ?>";
        data: { id: studentid },
        success: function(response) {
                $("#briefinfo").html(response);
            }
    });
    });
    });
        </script>

请帮忙。 这是id.php

<?php

    // connect to the database
    include('connect-db.php');
    // confirm that the 'id' variable has been set
    require_once("db.php");
        $id = $_POST['id'];
        if ($result = $mysqli->query("SELECT * FROM requests WHERE student_id=$id"))
        {
                if ($result->num_rows > 0)
                {
                    echo "<table border='1' cellpadding='10'>";
                    echo "<tr><th>Document #</th><th>Student #</th>
                    <th>Documents needed</th><th>Edit</th><th>Delete</th>
                    <th>recorded comment</th><th>unverify</th><th>comment</th><th>payment amount</th><th>set payment</th>
                    </tr>";


                        while ($row = $result->fetch_object())
                        {
                            echo "<tr>";
                            echo "<td>" . $row->id . "</td>";
                            $studentid=$row->student_id;
                            echo "<td><a href='id.php?id=" . $row->student_id . "'>$studentid</a></td>";
                            echo "<td>" . $row->document . "</td>";
                            echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
                            echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
                            echo "<td>" . $row->comment . "</td>";
                            echo "<td><a href='unverify.php?id=" . $row->id . "'>unverify</a></td>";
                            echo "<td><a href='comments.php?id=" . $row->id . "'>comment</a></td>";
                            echo "<td>" . $row->paymentamount . " pesos";"</td>";
                            echo "<td><a href='paymentamount.php?id=" . $row->id . "'>set amount</a></td>";
                            echo "</tr>";
                        }
                        echo"<br><br>";
                        echo "</table>";
                }
                else
                {
                    echo "No results to display!";
                }
        }
        else
        {
                echo "Error: " . $mysqli->error;
        }

    ?>

1 个答案:

答案 0 :(得分:1)

实际上...... var在ajax中,只需将其添加为数据中的值。

    $.ajax({
    type: 'POST',
    url: 'id.php',
    data: {
        id: "<?php echo $studentid; ?>"
    },
    success: function(response) {
        $("#briefinfo").html(response);
    }
});