PHP-将javascript变量发送到.PHP脚本
所以,不完全确定这是否正确,但是,这是我的php文档:connect.phpPHP-将javascript变量发送到.PHP脚本,php,javascript,Php,Javascript,所以,不完全确定这是否正确,但是,这是我的php文档:connect.php <?php mysql_connect("timereliefusers.db.8956371.hostedresource.com", "timereliefusers", "User1!db") or die(mysql_error()); mysql_select_db("timereliefusers") or die (mysql_error()); $choice =(int) $_G
<?php
mysql_connect("timereliefusers.db.8956371.hostedresource.com", "timereliefusers", "User1!db") or die(mysql_error());
mysql_select_db("timereliefusers") or die (mysql_error());
$choice =(int) $_GET["choice"];
switch ($choice) {
case 1:
mysql_query("UPDATE poll1 set choice1 = choice1 + 1");
break;
case 2:
mysql_query("UPDATE poll1 set choice2 = choice2 + 1");
break;
case 3:
mysql_query("UPDATE poll3 set choice3 = choice3 + 1");
break;
}
}
?>
请告诉我关于这两个文件的任何错误,谢谢你-克里斯
注意:这不会超过警报(“传递的变量”);出于某种奇怪的原因,有人能解释一下为什么会发生这种情况吗?AJAX调用一定有问题。看起来您从未向脚本发送选择……您应该这样做: PHP:
您可能希望从此数据库中删除您的用户名和密码,以便其他人看不到它。假设您在js脚本中使用的是
jQuery
,为什么不使用$。get
或$。ajax
而不是xmlhttp
?xmlhttp做了完全相同的事情,如果我使用$.get,我将如何传递变量?@ChrisCates从未通过警报是什么意思?这是你剧本中的最后一件事。。。它从不发出警报吗?请注意,$choice=(int)$choice
这将防止有人偷偷溜过字符串,我通常会使用mysql\u real\u escape\u stiring()
,但由于它只使用数字(int)就可以了。另外,请注意javascript,“connect.php?choice=“+choice
你能编辑他的连接信息吗?他现在已经在主帖子中完成了,所以只在这里显示:)非常感谢,现在在我的服务器上测试它。我知道我在传递变量时遗漏了一些东西,这应该可以解决问题。我做了一些变通方法,查看原始帖子以检查是否有错误。非常感谢。
$(document).ready(function(){
var choice = 0;
$("#s1main div").click(function(){
$("#s1main div").removeClass("buttonclicked");
$(this).addClass("buttonclicked");
if ($(this).attr("id") == "choice1"){
choice = 1;
}
if ($(this).attr("id") == "choice2"){
choice = 2;
}
if ($(this).attr("id") == "choice3"){
choice = 3;
}
xmlhttp.open("GET", "connect.php?choice=" + choice, true);
xmlhttp.send();
alert("Variables passed");
});
<?php
mysql_connect("db.host", "username", "password") or die(mysql_error());
mysql_select_db("timereliefusers") or die (mysql_error());
$choice = (int) $_GET["choice"];
switch ($choice) {
case 1:
mysql_query("UPDATE poll1 set choice1 = choice1 + 1");
break;
case 2:
mysql_query("UPDATE poll1 set choice2 = choice2 + 1");
break;
case 3:
mysql_query("UPDATE poll3 set choice3 = choice3 + 1");
break;
}
?>
$("#next").click(function(){
if (choice != 0) {
xmlhttp.open("GET", "connect.php?choice=" + choice, true);
xmlhttp.send();
}
});