[英]Automatically change PHP variable based on select option (AJAX?)
現在,我有默認為當前d / m / y的變量,然后將其彈出到mySQL查詢中,該查詢顯示表WHERE date ='$ dday'AND month ='$ dmonth'AND year =' $ dyear”。
$ddate = date("d");
$dmonth = date("m");
$dyear = date("Y");
相反,我希望有一個選擇框,它將根據所選選項更改變量。 因此,“日期選擇”的默認選項將是當前日期,但是例如,如果我將其更改為第12天,則我希望變量在選擇選項更改時更改,然后自動重新查詢數據庫。 我假設這將使用AJAX完成。
我在說什么甚至可能嗎? 如果查詢的自動化增加了很大的復雜性,那么只需更改變量並根據提交按鈕進行更新就可以了。
如果您提出的問題低於我的簡單水平,我承諾會從提問開始休息一下,並開始回答一些問題。
是的,這是可能的。 jQuery甚至使它變得容易。
創建您的<select>
元素並為其指定一個ID,以便可以在jQuery中使用它。
在jQuery中添加一個事件偵聽器,該事件偵聽器會在<select>
更改時觸發,例如$("#date").change()
。
在change事件的事件處理程序中,獲取<select>
的當前值,然后使用jQuery的AJAX $.post()
函數將該數據發送到PHP文件。
在該PHP文件中,清理數據以防止MySQL Injections ,然后在數據庫中查詢新數據。
使用PHP的echo
函數發送回數據。
在jQuery $.post()
回調函數(第三個參數)中,接收回顯的數據並將其放入變量中。
使用jQuery使用數據更新HTML。
您建議的兩種解決方案都可以。 您可以使用AJAX將選擇框中的值發送到php腳本,也可以提交表單並通過$_POST
或$_GET
以這種方式訪問它們,具體取決於您的表單方法。
這是一個例子,請您執行查詢:
<?php
//Some pseudo data kinda as your receive it from your query
$datafromSql = array(
array('id'=>1,'date'=>1,'month'=>1,'year'=>2012,'theData'=>'This is some data when the user select 1/1/2012'),
array('id'=>2,'date'=>2,'month'=>2,'year'=>2012,'theData'=>'This is some data when the user select 2/2/2012'),
array('id'=>3,'date'=>3,'month'=>3,'year'=>2012,'theData'=>'This is some data when the user select 3/3/2012'),
);
//Super simple API to access the data
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
header('Content-Type: text/html');
//pseudo code, really you would just format your query result
$return=array();
foreach($datafromSql as $row){
//Select all from array which match select choice
if($row['date']==$_POST['day'] || $row['month']==$_POST['month'] || $row['year']==$_POST['year']){
$return[]=$row['theData'].'<br />';
}
}
//output, with a fancy horizontal rule
echo implode('<hr />',$return);
die;
}?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js" charset="utf-8"></script>
<script type="text/javascript">
function update(){
$.post('./<?php echo basename(__FILE__)?>',
{
day: $("#day").val(),
month: $("#month").val(),
year: $("#year").val()
},
function(data) {
$('#result').replaceWith('<div id="result"><h1>The Result:</h1>'+ data +'</div>');
});
}
$(document).ready(function(){
update();
});
</script>
</head>
<body>
<form id="dateform" >
<p>Select Date:
<select size="1" name="day" id="day" onChange="update()">
<?php foreach(range(1,31) as $i){echo '<option value="'.$i.'">'.$i.'</option>';} ?>
</select>
<select size="1" name="month" id="month" onChange="update()">
<?php foreach(range(1,12) as $i){echo '<option value="'.$i.'">'.$i.'</option>';} ?>
</select>
<select size="1" name="year" id="year" onChange="update()">
<?php foreach(range(2008,2012) as $i){echo '<option value="'.$i.'">'.$i.'</option>';} ?>
</select>
</p>
</form>
<p id='result'></p>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.