joseph young
joseph young

Reputation: 27

send data to php with jquery AJAX and retrieve the response

I'm trying to do this: When a user click on the briefinfo div which has rows from mysqli, send the student id to id.php where the php file will then get all data by the student id and echo it out so i can use ajax to put it in my original 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>

Please help. This is the 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;
        }

    ?>

Upvotes: 1

Views: 3966

Answers (1)

Mfoo
Mfoo

Reputation: 3755

Actually... the var was inside the ajax, just add it as the value in the data.

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

Upvotes: 1

Related Questions