【问题标题】:Jquery Ajax Acting weirdJquery Ajax 行为怪异
【发布时间】:2014-07-31 07:34:56
【问题描述】:

您好,我已经在这几天了,我只是想不通为什么这不起作用,请有人看看。

index.php

<form id = "update_status" method = "POST">
                        <textarea id="shadow" name ="user_status" placeholder = "Share what is on your mind!" cols="97" rows="1" title="Share what's on your mind"></textarea>

                        <input style="float:right; margin-top: 0.3%;margin-right : 0% !important;" id = "btnStatus_update" name = "btnStatus_udate" type="button" value="Add Post" title="Your posts will be made public"></input></form>

swift.php

$(document).ready(function(){ 
    $("#btnStatus_update").click(function(e) { 
    e.preventDefault();
    //alert("Confirming function works");
        $.ajax({
        cache: false,
        type: 'POST',
        url: './datacenter.php',
        data: $("#user_status"),
        success: function(d) {
            $("#successMesg").html(d);
        }
        });
    }); 
});

datacenter.php

if (isset($_POST['user_status'])) {
    var_dump($_POST['user_status']);
        foreach ($_POST as $ak => $av) {
            if (empty(trim($_POST['user_status']))) {
                echo 'Say what is on your mind'.' ';
            } else {
                $userstatus = $_POST['user_status'];
                add_user_status($user_data['id'], $userstatus);
            }
        }
    }

使用 var_dump 进行测试我希望它确实返回了数据,但是我得到了 NULL,我需要数据,所以我可以将它传递给 add_user_status 函数以添加到数据库中,但似乎缺少一些东西或关闭关于否认我满意的代码。请帮忙

【问题讨论】:

  • 你的数据被搞砸了,你需要提供一个键值对让你的 php 工作:data: {user_status: $("#user_status").val()},

标签: php jquery ajax


【解决方案1】:

似乎缺少一些东西:

索引.php:

您需要在表单标签中添加action="something" 属性,这会告诉表单您提交时要做什么。 (除非您在其他地方手动处理这是 JS?)

<form id = "update_status" action ="data.php" method = "POST">

此外,除非您在索引页面上使用 JavaScript 来处理表单的实际提交,否则您的 &lt;input&gt; 应该包含 type="submit" 属性。 (这也将使它成为一个按钮),单击它会自动将表单提交到上面的action 位置。

Swift.php:

发布的代码是 JS,并且执行上一段第一行提到的操作(处理提交按钮)。您是否将此文件包含在index.php 中?如果index.php 看不到它,那么它就不会运行。它还必须在适当的&lt;script&gt; 块中的某个位置的html 中。

我相信使用 ajax 发送表单数据的正确方法是序列化数据: $('#update_status').serialize() 而不是只发送一个输入字段。

您还需要引用 jQuery 库,最好在索引中,但也可以在 swift.php 中。我还假设发布的代码出现在必要的&lt;script&gt; 块中。

数据.php:

应该是datacenter.php 吗?您的 Swift.php 正在向 ./datacenter.php 发送 ajax 请求

附带说明,如果您需要它来使用 Ajax,那么您实际上不需要表单中的 action ="data.php" method = "POST"(Ajax 会为您完成所有这些)

可能的方式是这样的:

索引.php:

// HTML beginning stuff
<head>
// Either reference the script in its own JS file or:
// Need to also include jquery library
<script type="text/javascript">
$(document).ready(function(){ 
    $("#btnStatus_update").click(function(e) { 
    e.preventDefault();
    //alert("Confirming function works");
        $.ajax({
        cache: false,
        type: 'POST',
        url: './datacenter.php',
        data: $('#update_status').serialize(),
        success: function(d) {
            $("#successMesg").html(d);
        }
        });
    }); 
});
</script>
</head>
<body>
<form id = "update_status">
<textarea id="shadow" name ="user_status" placeholder = "Share what is on your mind!" cols="97" rows="1" title="Share what's on your mind"></textarea>
<input style="float:right; margin-top: 0.3%;margin-right : 0% !important;" id = "btnStatus_update" name = "btnStatus_udate" type="button" value="Add Post" title="Your posts will be made public"></input>
</form>
</body>

datacenter.php:

<?php
var UserStatus = user_status
if (!empty(UserStatus)) {
    var_dump($_POST['user_status']);
// process as necessary
}

?>

但是,包括 &lt;form id = "update_status" action ="datacenter.php" method = "POST"&gt; 并将按钮更改为

&lt;input style="float:right; margin-top: 0.3%;margin-right : 0% !important;" id = "btnStatus_update" name = "btnStatus_udate" type="submit" value="Add Post" title="Your posts will be made public"&gt;

如果用户禁用了 JavaScript,仍允许用户发布信息。

【讨论】:

    【解决方案2】:

    textarea 的选择器不正确,id 不是“user_status”,而是它的“shadow”

    data: $("#user_status"),
    

    应该是:

    data: $("textarea[name=user_status]") 
    

    data: $("#shadow") 
    

    【讨论】:

    • 您好,感谢您的回复,在上一个解决方案之后我没有尝试这个解决方案,我相信这也可以工作
    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多