Search code examples
javascriptphpajaxpostresponse

How to POST ajax and get response?


I have a problem with a form, the post and the response. In my form I call a function (javascript) with the ajax post:

var vars = "test="+test;
$.ajax({
    type: "POST",
    url: "index.php",
    data: vars
}).done(function(data) {
    alert(data);
}).fail(function(data) {
    alert(data);
});

In index.php I receive all the data:

<?php
    $test = $_POST['test'];
    //do something
?>

After I have to give back a value to the previous php. How can I do?? Thanks


Solution

  • var vars = "test="+test;
    $.ajax({
        type: "POST",
        url: "index.php",
        data: vars
        success: function(html){
             alert(html)
        }
    });
    

    in index.php, enter follow code and check

    <?php
        $test = $_POST['test'];
        //do something
        echo $test
    ?>