大家好,尝试做一些简单的事情:将2个文本变量传递给php脚本,然后将它们插入MySQL数据库。但是由于某种原因,我无法传递变量(因此,我在数据库中只得到空记录)。
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(); } ?>
任何人愿意快速浏览一下并指出我(可能是显而易见的)错误吗?一天来一直让我发疯!
谢谢!
这必须工作:
<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(只有更改才能发布)
<?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的构建。