我如何将PHP
变量传递给AJAX
我尝试使用var sStr = "<?php echo $game ?>.";
它会将其打印出来,但我无法将其POST
传递给它PHP
文件。这是我的代码。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<?php
$game=$_GET['id'];
?>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.post("php.php",
{
game:"<?php echo $game; ?>",
city:"Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button>Add to Favourites</button>
答案 0 :(得分:0)
尝试按ID调用该按钮并使用addslashes
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<?php
$game = $_GET['id'];
$game = addslashes($game);
?>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
$.post("php.php",
{
game:"<?=$game?>",
city:"Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button id="button">Add to Favourites</button>