简体   繁体   English

PHP 不返回 Ajax 调用

[英]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.我正在尝试使用对我的 PHP 文件的 ajax 调用从我的 textarea 发送数据,但我们没有从 PHP 文件中得到任何响应。

<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.我以为我已经在这段代码中尝试了 echo,但我想我只是错过了网页上的输出并认为它不起作用。

<?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 :你的 ajax 帖子:

$('#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 响应会像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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