我怎樣才能發送JavaScript變量值到PHP。我爲此使用了ajax,但是它爲我工作。請幫助,我是新的JavaScript和Ajax。這裏是我的ajax & JavaScript代碼。我怎樣才能發送JavaScript變量值到PHP
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css" />
<script type="text/javascript">
$(function()
{
$("#slider-range").slider(
{
range: true,
min: 71,
max: 109,
values: [75, 100],
slide: function (event, ui)
{
$("#size-range").html(Math.floor(ui.values[0]/12) + "'" + (ui.values[0] % 12) + '" - ' + Math.floor(ui.values[1]/12) + "'" + (ui.values[1] % 12) + '"');
$("#min_inches").val(ui.values[0]);
$("#max_inches").val(ui.values[1]);
}
});
$("#size-range").html(Math.floor($("#slider-range").slider("values", 0)/12) + "'" + ($("#slider-range").slider("values", 0) % 12) + '" - ' + Math.floor($("#slider-range").slider("values", 1)/12) + "'" + ($("#slider-range").slider("values", 1) % 12) + '"');
var a = $("#min_inches").val($("#slider-range").slider("values", 0));
var b = $("#max_inches").val($("#slider-range").slider("values", 1));
$.ajax(
{
type: "POST",
url: "searchrange.php",
data:
{
a: a,
b: b
},
success: function (option)
{
alert("voted");
}
});
});
</script>
而下面是我的php代碼(searchrange.php)。
<?php
if(isset($_POST['a']) && $_POST['a'] != '')
{
$kws = $_POST['a'];
$kws1=$_POST['b'];
echo $kws;
echo $query = "select * from newusers where Age between '".$kws."' and '".$kws1."'" ;
$res = mysql_query($query);
$count = mysql_num_rows($res);
$i = 0;
if($count > 0)
{
echo "<ul>";
while($row = mysql_fetch_array($res))
{
echo "<a href='#'><li>";
echo "<div id='rest'>";?>
<a href="searchrange.php?id=<?php echo $row['0'];?> "><?php echo $row['Religion'];?><br><?php echo $row['Name'];?></a>
<?php echo $row[0];
echo "<br />";
echo "<br />";
echo "<div style='clear:both;'></div></li></a>";
$i++;
if($i == 5) break;
}
echo "</ul>";
if($count > 5)
{
echo "<div id='view_more'><a href='#'>View more results</a></div>";
}
}
else
{
echo "<div id='no_result'>No result found !</div>";
}
}
?>
任何幫助,將不勝感激。謝謝
定義 '它不工作'。你看到錯誤信息嗎?有什麼事情發生?這是你期望的,如果不是,它有什麼不同? – 2013-12-09 04:34:47
在發送之前警告「a」變量並查看得到的結果。 –
您在執行頁面$'((){... $ .ajax({...})})'時正在執行$ .ajax({...})''。不應該在按鈕點擊/表單提交時執行此操作嗎? – Sean