simple jquery .post not returning data

152 Views Asked by At

jquery statement:

$.post ('test.php', {f1: 'abc', f2: 'def'}, function (data) {console.log (data)})

test.php:

 <?php
        $f1 = $_REQUEST ['f1'];
        $f2 = $_REQUEST ['f2'];

        echo $f1 . $f2;
?>

After invoking the jquery statement, nothing gets returned which should be seen in the console area.

Running test.html, however, the browser shows 'f1f2' as expected. test.html:

<form action='test.php' method="post">
    <label>f1</label>
    <input name="f1" type="text">
    <label>f2</label>
    <input name="f2" type="text">
    <input type="submit">
</form>

Why is there no return data to the jquery post request?

1

There are 1 best solutions below

7
On BEST ANSWER

This should work:

<?php
        $f1 = $_POST ['f1'];
        $f2 = $_POST ['f2'];

        echo $f1 . $f2;
    ?>

and if you want to post using jquery , dont submit your form , preventDefault on submit , and trigger the post submit.

$("#yourFormId").submit(function(e){e.preventDefault();$.post ('test.php', {f1: 'abc', f2: 'def'}, function (data) {console.log (data)})})