【发布时间】:2012-08-02 04:31:25
【问题描述】:
在我的代码中我有这个回调
$('#tagList').tagit({
//Every new dag will be pushed in the list array
tagsChanged: function(tagValue,action,element){
list.push(tagValue);
$.ajax({
url: "dostuff.php",
type: "POST",
data:{ items:list.join("::")},
success: function(data){
$('#wrap').append(data);
}
});
}
});
每次我添加标签时,新添加的标签都会被推送到数组中,然后它会发出 AJAX 发布请求。
然后我在这里有这些字段
<form method = "POST" action = "demo3.php">
News Title <input id = "news_title" type = "text" name = "news_title" /><br>
<label>Insert Some tags </label>
<ul id="tagList" data-name="demo2">
</ul>
<input type = "submit" name = "submit" id = "submit" value = "Post News" />
</div>
</form>
当我单击提交(它基本上重新加载页面)时,$_POST['items'](这是在每次添加新标签时根据 AJAX 请求创建的)正在 POST 全局数组中被擦除或删除。因此将我的 $_POST 全局数组留空。
无论如何我可以合并这两个吗?或者无论如何不要让 PHP 覆盖或删除 $_POST['items'] ?因为我的查询需要项目
我也在使用一个名为 tagit 的插件
如果你们有兴趣,这里是我的完整代码
<!doctype html>
<html>
<head>
<script src="demo/js/jquery.1.7.2.min.js"></script>
<script src="demo/js/jquery-ui.1.8.20.min.js"></script>
<script src="js/tagit.js"></script>
<link rel="stylesheet" type="text/css" href="css/tagit-stylish-yellow.css">
<script>
$(document).ready(function () {
var list = new Array();
$('#tagList').tagit({
//Every new dag will be pushed in the list array
tagsChanged: function(tagValue,action,element){
list.push(tagValue);
$.ajax({
url: "dostuff.php",
type: "POST",
data:{ items:list.join("::")},
success: function(data){
$('#wrap').append(data);
}
});
}
});
});
</script>
</head>
<body>
<div id="wrap">
<div class="box">
<button class = "viewTags">View Tags</button>
<form method = "POST" action = "demo3.php">
News Title <input id = "news_title" type = "text" name = "news_title" /><br>
<label>Insert Some tags </label>
<ul id="tagList" data-name="demo2">
</ul>
<input type = "submit" name = "submit" id = "submit" value = "Post News" />
</div>
</form>
</div>
</body>
</html>
这里有一些东西。 php
<?php
$lis = $_POST['items'];
$liarray = explode("::", $lis);
print_r($liarray);
print_r($_POST);
?>
【问题讨论】:
-
PHP 不会“覆盖”$_POST。您进入 $_POST 的内容就是 PHP 收到的内容。如果 $_POST 是空的,那么你没有发送任何东西,或者它的格式不正确。
-
哦,我明白了。我将如何合并 ajax 发出的 POST 请求和 PHP 接收的 POST?因为我两个都需要。
-
这毫无意义。您是在问“我如何将奶奶送来的礼物与我从奶奶那里收到的礼物合并”?除非您的代码被冲洗,否则您的 JS 代码将发起一个单一的 http 请求,向您的服务器端 php 脚本执行一个单一的发布。
-
我的问题是 $_POST 表现得很奇怪。当我不点击提交时,$_POST['items'] 在那里,但是当我点击提交时。只有 $_POST['news_title'] 和 $_POST['submit'] 没有发生任何事情
标签: php javascript jquery tags tag-it