使用AJAX单击php中的ID行时检索更多数据

时间:2015-03-01 15:44:51

标签: php html ajax mysqli

我希望在点击$ row-> student id行时显示更多的php数据。当点击row-> studentid时,它应该在showmore div中将数据显示为ajax。

基本上这是index.php代码的一部分。

<div class="studentsinfo">
   <div class="studentpicture">
      <img src="images/chrissy.jpg" style="width:100%;height:30%;padding-bottom:none;margin-bottom:none;"/>
   </div>
   <div id="briefinfo">
        <?php require_once("db.php");
        if ($result = $mysqli->query("SELECT * FROM requests WHERE status = 1 ORDER BY id"))
        {
            if ($result->num_rows > 0)
            {
                while ($row = $result->fetch_object())
                {
                    echo "document id:" . $row->id;
                    echo "<br>";
                    $studentid=$row->student_id;
                    echo "student id:" . $studentid;
                    echo "<br>";
                    echo "requested: " . $row->document;
                    echo "<br>";

                    if ($row->paidstatus != 1){
                        echo "payment status: not paid";
                    }
                    else if ($row->paidstatus = 1){
                        echo "payment status: paid";
                    }
                    /*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><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>";*/
                }
            }
            else
            {
                echo "No results to display!";
            }
        }
        else
        {
            echo "Error: " . $mysqli->error;
        }
        ?>
    </div>
</div>
<div id="showmore> <!-- show more data here -->

因此,当点击studentid按钮时,它将从id.php获取信息并在此处返回。 Id.php是这样的:

<?php

// connect to the database
include('connect-db.php');

// confirm that the 'id' variable has been set
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
    // get the 'id' variable from the URL
    $id = $_GET['id'];

require_once("db.php");

    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;
    }}

?>
</div>

所以基本上,我希望id.php中的所有行在showmore的div中显示为ajax。请帮忙。 我如何编写AJAX代码以便在点击时发布studentID并通过studentid返回PHP数据。

我认为AJAX jquery代码看起来像这样:

$.(#studentid).click(){
something here
}
$.ajax({
      type : 'POST',
      url : 'id.php'
and soemthing else
});

请帮忙。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用jQuery的ajax?给一个<tr>个id并用

执行ajax请求
//... in your HTML put
<script src="jquery.js"></script>
//...
<tr id="rowId"...> ...
//...

//in your Javascript, put
$(function (){
  $('#rowId').click(function(){ //bind row click to ajax
    //send request for student data
    $.ajax({
      url:"getMoreStudentData.php",
      type:"POST",
      data:'rowId',
      dataType:"json",
      success: onStudentDataReturned
    });
  });    
});

//do something with your ajaxed info
function onStudentDataReturned(data){
    console.log(data);
}

一旦你开始工作,你只需要选择一种方法让你的所有行都有适当的rowId,而不是占位符rowId