我是新来的PHP和JavaScript / AJAX,所以请多多包涵。
I'm new to PHP and Javascript/Ajax so please bear with me.
所有我需要做的是从阿贾克斯得到一个变量,并将其设置为在PHP变量。我试图用超全局 GET
做到这一点,但东西是不正确的。我不希望这种通过提交表单。
All I need to do is get a variable from Ajax and set it as a variable in php. I'm trying to do this with a super global GET
but something is not right. I don't want to this by submitting the form.
下面是我的JS:
function myFunction(){
var hora= document.getElementById("hora").value;
$.ajax({
type : 'GET',
url : 'reservation.php',
data : {hora: hora},
success : function(data) {
console.log(hora);//This is because I was curious as to
// what the console would say. I found
// that this sets the super global if I
// change the url to something else that
// doesn't exist. Console would say
// -GET http://localhost/bus/(somepage).php?hora=4
// 404 (Not Found)-
alert(hora);
}
})
}
下面是我的PHP:
Hora:
<select name="hora" id="hora" onchange="myFunction()">
<?php
$query = "SELECT * FROM vans";
$horas_result = mysql_query($query);
while ($horas = mysql_fetch_array($horas_result)) {
echo "<option value="{$horas["van_id"]}">{$horas["time"]}</option>";
}
?>
</select>
Asientos Disponibles:
<?php echo $_GET["hora"]; ?>
//Right now I only want to echo this variable..
正如你所看到的,现在我只想呼应这个变量,以后我会用这个来编写一个查询。
As you can see, right now I only want to echo this variable, later on I'll be using this to write a query.
推荐答案
看codeI后,AJAX是用于发布/获取,而不需要刷新页面的数据,但如果你只是想发布的数据,并给出的结果在其他页面中使用的一种形式来代替。
Look at the code i post, ajax is used to post/get data without need to refresh the page but if you just want to post the data and give the result in other page use a form instead.
<?php
if (isset($_GET["hora"]))
{
echo $_GET["hora"];
exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Page title</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function()
{
$("#hora").change(function ()
{
$.ajax(
{
type : 'GET',
url : '',
data : $('select[name='hora']'),
success : function(data)
{
$('#ajax_result').html('Asientos Disponibles: ' + data);
},
error: function(xhr, ajaxOptions, thrownError)
{
alert(thrownError + "rn" + xhr.statusText + "rn" + xhr.responseText);
}
}
)
}
)
}
)
</script>
<select name="hora" id="hora">
<?php
$query = "SELECT * FROM vans";
$horas_result = mysql_query($query);
while ($horas = mysql_fetch_array($horas_result)) {
echo "<option value="{$horas["van_id"]}">{$horas["time"]}</option>";
}
?>
</select>
<div id="ajax_result">
</div>
</body>
</html>
相关推荐
最新文章