![](/img/trans.png)
[英]how I can get unselected an option in a select multiple when I click in another option of that same select?
[英]How can get the refreshed option values from database only when i click on select box?
我只想在單擊選擇框時從數據庫中獲取刷新的選項值。
假設兩名服務員同時打開同一個訂單面板頁面。 然后表格no:2在兩個面板中都顯示為空閑。
現在服務員預訂了表號:2。 然后另一位服務員點擊選擇框時,他不會在選項中得到表:2。
<select name="table_id" class="form-control tablename">
<option disabled="disabled">Select Table</option>
<?php $result = mysql_query("select * from rtable r
inner join table_status as ts
on ts.status_id=r.status_id
where ts.status!='Booked'
order by r.table_id desc")or die(mysql_error());
while ($row=mysql_fetch_array($result)){ ?>
<option value="<?php echo $row['table_id'];?>"><?php echo $row['table_name']; ?></option>
<?php } ?>
</select>
在php中創建函數來生成選項(發送html不是很好的做法,但我正在調整這個例子)。 在這個特定的例子中,我建議創建functions.php文件並在那里添加printSelectOptions函數聲明:
function printSelectOptions(){
$result = mysql_query("select * from rtable r
inner join table_status as ts
on ts.status_id=r.status_id
where ts.status!='Booked'
order by r.table_id desc")or die(mysql_error());
echo "<option disabled='disabled'>Select Table</option>";
while ($row=mysql_fetch_array($result)){
echo "<option value=".$row['table_id'].">".$row['table_name']."</option>";
}
}
上面的函數打印所有選擇的html選項。
在生成選擇時使用它函數(記住,使用printSelectOptions時,functions.php應該包含在任何文件中) :
<?php
//db connection code
require_once("functions.php");//here we add our function to be available in this file
?>
<select name="table_id" class="form-control tablename">
<?php printSelectOptions() ?>
</select>
在前端綁定您的選擇(javascript代碼):
document.addEventListener("DOMContentLoaded", function(event) {
var select=document.querySelector("select"); //this is pure selector gets first select on page
//function sends ajax and refresh options of select
function refreshOptions(){
//send ajax request
select.innerHTML="<option>Loading..</option>"; //loading info
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", 'yourSecondPHPScript.php');//here example url where we get updated options
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if(xmlhttp.status == 200){
select.innerHTML = xmlhttp.responseText;//set new options
}else{
console.log('Error: ' + xmlhttp.statusText )
select.innerHTML="<option>Connection problem</option>";
}
}
}
xmlhttp.send();
};
//bind our select
select.addEventListener("focus",function(){
refreshOptions();
});
});
最后創建示例yourSecondPHPScript.php並在其中使用函數:
<?php
//db connection code
require_once("functions.php");//here we add our function to be available in this file
printSelectOptions();//outputs options
為了確保用戶除了檢查焦點之外不會使用相同的表,請在一些訂單提交中再次檢查它。 因此,如果表被刷新選擇(通過ajax使用refreshOptions())並顯示該表已被采取的信息。
最后一件事是在服務器端保護它,在php(PHP CODE)中創建一些檢查功能:
function tableCanBeTaken($optionId){
//this code adds **and** to query with id to check but optionId should be validate before using in query
$result = mysql_query("select * from rtable r
inner join table_status as ts
on ts.status_id=r.status_id
where ts.status!='Booked'
and ts.table_id=$optionId ")or die(mysql_error());
return mysql_fetch_array($result); //if row exists - will be false if not exists row with table_id==$optionId and not booked
}
}
然后使用它(PHP代碼):
if (tableCanBeTaken($youOptionId)){
//here code for taking option
}else{
//here option is taken
}
在選擇框的焦點事件中調用ajax。在調用成功時,將數據(可用表)附加到選擇輸入。然后,將選擇框選項保留為“正在加載”。 希望這可以幫助!
@Maciej Sikora
問題是固定的。 無法從另一個文件(如yourSecondPHPScript)調用printSelectOptions()函數。 並且還需要從url中刪除反斜杠。
xmlhttp.open("GET", 'yourSecondPHPScript.php');
我只需將相同的代碼粘貼到yourSecondPHPScript.php中,如下所示
<?php
include("connect.php");
$result = mysql_query("select * from rtable r inner join table_status as ts on ts.status_id=r.status_id where ts.status!='Booked' order by r.table_id desc")or die(mysql_error());
echo "<option disabled='disabled'>Select Table</option>";
while ($row=mysql_fetch_array($result))
{
echo "<option value=".$row['table_id'].">".$row['table_name']."</option>";
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.