简体   繁体   中英

PHP not returning Ajax Call

I'm trying to send over the data from my textarea using an ajax call to my PHP file, but we aren't getting any response back from the PHP file.

<textarea id="area" cols="70" rows="30"></textarea>
<button id="submit">Submit</button>

<script>
$('#submit').click(function (e) {
    e.preventDefault();

    var info = $('#area').val();
    $.ajax({
        type: "POST",
        url: 'pages/assignments/response.php',
        data: {area: info}
    });
});
</script>
<?php
    if (!empty($_POST['area'])) {
        $success = json_encode('succes');
        return $succes;
    };
?>

-- # Answer # --

I thought I had already tried an echo in this piece of code, but I think I just missed the output on the webpage and thought it wasn't working.

<?php
    if (!empty($_POST['area'])) {
        echo "success";
    };
?>

Thanks Mickael for the answer!

I completely forgot to add an echo to my code.

<?php
    if (!empty($_POST['area'])) {
        $succes = json_encode('succes');
        echo $succes();
    };
?>

Your ajax post :

$('#submit').click(function (e) {
    e.preventDefault();

    // information to be sent to the server
    var info = $('#area').val();
    $.ajax({
        type: "POST",
        url: 'pages/assignments/response.php?return=1',
        data: {area: info},
        dataType:'json',
        success:function(r){
         console.log(r);
        }
    });
});

and your php response will be like

Php file:
<?php
if ( $_GET['return'] == 1 && isset($_GET['return']) ) {
    echo json_encode('succes');
    exit;
};
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM