本文介绍了AJAX + jQuery变量不会传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做一些非常简单的事情:将2个文本变量传递给php脚本,并将其插入到MySQL数据库中.但是由于某种原因,我无法传递变量(因此,我在数据库中只得到空记录).
I am trying to do something very simple: Pass 2 text variables to a php script and insert them into a MySQL db. For some reason however I can't get the variables to pass (so I just get empty records in my DB).
function ajaxCall(){
$.ajax({
type: "GET",
url: "http://www.*.be/bubblingAjax.php",
cache: false,
data: "colour="+colour+"&size="+size,
dataType: "html",
success: onSuccess
});
return false;
};
和PHP:
<?php
try
{
$connection = mysql_connect("#");
mysql_select_db("#");
$colour = mysql_real_escape_string($_GET['colour']);
$size = mysql_real_escape_string($_GET['size']);
mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')");
mysql_close($connection);
echo "SUCCESS";
echo $colour;
echo $size;
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
有人愿意快速浏览一下,指出我的-可能是显而易见的-错误吗?一天来一直让我发疯.
Anyone willing to take a quick look at it and point out my -probably obvious- mistake? It's been driving me nuts for over a day.
推荐答案
这必须有效:
<script type="text/javascript">
$(document).ready(function() {
//you can wrap the code into an event, e.g click()
var colour=...
var size=...
$.post("http://www.website.com/bubblingajax.php", { colour: colour, size: size },
function(data) {
alert("Respond: " + data);
});
});
</script>
和PHP(只有更改才能发布)
and the PHP (only changed get to post)
<?php
try
{
$connection = mysql_connect("#");
mysql_select_db("#");
$colour = mysql_real_escape_string($_POST['colour']);
$size = mysql_real_escape_string($_POST['size']);
mysql_query("INSERT INTO bubble (colour, size) VALUES ('$colour', '$size')");
mysql_close($connection);
echo "SUCCESS";
echo $colour;
echo $size;
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
也可以调试,我建议在检查工具中使用firebug或chrome的构建.
Also to debug, I would suggest using firebug or chrome's build in inspect tools.
这篇关于AJAX + jQuery变量不会传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!