将POST数据从Javascript(jquery)传递到php问题? [英] Passing POST data from Javascript(jquery) to php problems?
问题描述
我一直在尝试传递此值:
I been trying to passing this value:
// Content to submit to php
Others string here. And this link:
http://www.youtube.com/watch?v=CUUgKj2i1Xc&feature=rec-LGOUT-exp_fresh+div-1r-2-HM
到php页面,并将其插入数据库.这是我当前的代码:
to a php page, and insert it to database. here is my current code:
... // javascript
var content = $("#mcontent").val();
$.ajax({
url : '<?php echo PATH; ?>functions/save.php',
type: 'POST',
data: 'id=<?php echo $_GET['id']; ?>&content=' + content + '&action=save&val=<?php echo md5("secr3t" . $_SESSION['userid_id']); ?>',
dataType: 'json',
success: function(response) {
if (response.status == 'success') {
alert(response.message);
} else {
alert(response.message);
}
}
});
实际上没有错误,但是在数据库中保存的是:
No errors actually, but in database, what it saved is:
Others string here. And this link:
http://www.youtube.com/watch?v=CUUgKj2i1Xc
我想我知道出什么问题了,问题是:
I guess i know whats the problem, the problem is the:
http://www.youtube.com/watch?v=CUUgKj2i1Xc &功能 = rec-LGOUT-exp_fresh + div-1r-2-HM
http://www.youtube.com/watch?v=CUUgKj2i1Xc&feature=rec-LGOUT-exp_fresh+div-1r-2-HM
我认为它将& feature =作为另一个POST数据.我尝试过的:
I think it takes the "&feature=" as another POST data. What I have tried:
-
在&符之前添加斜杠( http://phpjs.org/functions/addslashes:303 )
使用Javascript HTML编码/解码功能(也可以在互联网上找到)
Using Javascript HTML encode/decode function (found somewhere on internet also)
但是两者都不起作用.你还有其他办法吗?
But both does not work. Do you have any others way?
您是否预见到可能发生的其他问题?内容由用户键入/写入.这意味着,用户可以键入/写入任何内容.反过来说,我还是做了其他检查,包括"mysql_real_escape_string"
Do you foresee any others problem that might occurs? The content are type/write by user. Meaning that, the user can type/write anything. On backhand, I did others checking though, including the "mysql_real_escape_string"
推荐答案
关于jQuery的一件好事是data参数可以使用JS对象,因此您无需尝试手动构建查询字符串.>
A nice thing about jQuery is that the data parameter can take a JS object, so you don't need to try to build a query string manually.
<?php
$data = array("id" => $_GET['id'],
"action" => "save",
"val" => md5("secr3t",$_SESSION['userid_id'])
);
$json_data = encode_json($data);
$json_data = str_ireplace($json_data, '</script>', '<\/script>');
echo "var data = $json_data;";
?>
data.content = content;
$.ajax({
url : '<?php echo PATH; ?>functions/save.php',
type: 'POST',
data: data,
dataType: 'json',
这篇关于将POST数据从Javascript(jquery)传递到php问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!