javascript - 使用 jquery $.post 从服务器加载数据

标签 javascript php jquery

我要做的是通过表单和 php 脚本将数据发送到服务器。然后我想将其取回并使用 Jquery 将其放置在正确的位置。我不知道为什么,但这个解决方案似乎不起作用,因为我只是得到“文本框中没有文本!”。有人有什么建议吗?

<html>
<head>
    <script type="text/javascript" src="jquery-3.1.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#send").click(function(event) {           
                $.post("target.php", {
                    melding: $("#input").val().
                }, function(data){
                    $("#meldinger").after("<div>" + data + "</div>");
                });
                event.preventDefault();
            });
        });
        document.getElementById('demo').innerHTML = date();
    </script>
    <title>Meldingssystem</title>
</head>
<body>
    <h1>Meldingsutveksling</h1>
    <form action="target.php" method="post">
    <textarea id="input" name="input" cols="40" rows="2"></textarea>
    <br/><input type="submit" name="submit" value="Send">
    </form>
    <h2>Meldinger</h2>
    <div id="meldinger">
    </div>
</body>
</html>

PHP

<?php
if (!empty($_POST['input'])) {
     $melding = $_POST['input'];
    echo $melding;
}
else {
    echo "No text in the textbox!";
}
?>

最佳答案

这个

if (!empty($_POST['input'])) {
     $melding = $_POST['input'];
    echo $melding;
}
else {
    echo "No text in the textbox!";
}

应该是

if (!empty($_POST['melding'])) {
     $melding = $_POST['melding'];
    echo $melding;
}
else {
    echo "No text in the textbox!";
}

因为没有发送输入POST参数

关于javascript - 使用 jquery $.post 从服务器加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951622/

相关文章:

php - 如何使用 shell_exec() 触发 gulp 任务?

javascript - jQuery 可调整大小同步调整大小

javascript - JQuery 添加/删除类问题

jquery - 如何在添加到现有表之前对表行进行动画处理

javascript - 两个 Node.JS 脚本读取/写入单个 .JSON 文件,但第一个脚本无法修改该文件

javascript - 如何对齐 3 个不同的图表?

javascript - 使用 Express 在 node.js 中需要 jQuery-ui 时出现 "TypeError: undefined is not a function"

javascript - Vue 中的 componentWillReceiveProps

php - Codeception SOAP 命名空间

php - MYSQL从内网和外网读取文件为什么数据包乱序?