AJAX/PHP pass $_GET and $_POST?

Asked

Viewed 111 times

0

Good evening everyone, I have a PHP script that creates a table in which on this page is run an ajax script and you can see in real time who is in the database... in this table I have a delete button for each person and when clicking is created a variable $_GET with the process number (e. g searchstudent.php? usrdelete=5382 )

AJAX:

 $('#textbox').keyup(function(){
    $("#display_students").text('');
    var name = $('#textbox').val();
    if($.trim(name) != ''){
        $.post('../core/Query/students/student_search.php', {st_process: name}, function(data){
            $("#display_students").append(data);
        });

    }
});

PHP:

    <?php


if(isset($_POST['st_process']) === true && empty($_POST['st_process']) === false){
    include("../Query-core.php");
    include('../db.php');

        $authentication = new DBRequest($host = 'localhost',
                                $user = 'root',
                                $pass = '',
                                $db = 'contas');

        $selectedstudents = $authentication->selectionQueryLike("alunos", $authentication->e(trim($_POST['st_process'])), "student_process");

        if(mysqli_num_rows($selectedstudents) > 0){
            echo "<table><tr><th>Nome</th><th>NºProcesso</th><th>ID</th><th>Apagar</th></tr>";

            while($row =  $selectedstudents->fetch_assoc()){
               echo "<tr>";
               echo "<td><a href='#'>" . $row["student_name"] . "</a></td>";
               echo "<td>" . $row["student_process"] . "</td>";
               echo "<td>" . $row["student_ID"] . "</td>";
               echo "<td><a href=?usrdelete=" . $row["student_ID"] . ">Delete<a/></td>";

               echo "</tr>";

            }
            echo "</table>";
    }   
}

?>

how could I simultaneously spend $_GET and $_POST in ajax?

1 answer

2

In this way:

$.post('../core/Query/students/student_search.php?usrdelete=5382', {st_process: name}, function(data){
    $("#display_students").append(data);
});

If you use the value of a variable just concatenate the string using the +, for example:

var variavel = 5382;

$.post('../core/Query/students/student_search.php?usrdelete=' + variavel, {st_process: name}, function(data){
    $("#display_students").append(data);
});

GET is all that goes in querystring, ie in the URL after the ? and POST is all that goes on payload (in the body of HTTP request), then the HTTP POST instruction combined with GET would look like this:

POST /pagina.php?getfoo=getbar HTTP/1.1 <--- aqui vai o GET (após o sinal de ?)
Host: foo.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 31 <--tamanho do body

postfoo=postbar&postbaz=postboo <-- body que contém o POST

Browser other questions tagged

You are not signed in. Login or sign up in order to post.