2016-08-20 23 views
4

我只希望當我在選擇框中單擊以從數據庫中獲得的刷新選項值。只有當我點擊選擇框時,如何從數據庫中獲取刷新的選項值?

假定有兩個服務員在同時打開相同的順序面板頁。然後,在兩個面板中,表號: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> 

Select Table Image

table_status

rtable

+0

所以你嘗試過什麼相同的代碼? – coder

+0

那麼你可以使用Ajax,SQL和PHP?..解釋你所嘗試的將是一個好開始 – Satty

+0

想要開始與AJAX。 –

回答

0

還需要在選擇box.In呼叫的成功的焦點事件Ajax調用,數據(可用表)追加到選擇輸入。然後,將選擇框選項保留爲「加載」。希望這可以幫助!

1

在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選項選擇。

使用它的功能生成選擇(記住functions.php中應包含與printSelectOptions的使用的任何文件)

<?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代碼):

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 

} 
+0

連接問題顯示,當點擊..如何解決這個問題? –

+0

添加所有db代碼 - 連接等。printSelectOptions方法用法之前。 –

+0

仍然是同樣的問題。在這裏我給代碼。 order.php http://pastebin.com/92BE0w1b yourSecondPHPScript http://pastebin.com/qJ8KDYkr connect.php http://pastebin.com/f8excR1i –

0

@Maciej西科拉

問題是固定的。 printSelectOptions()函數無法從其他文件(如yourSecondPHPScript)中調用。 還需要從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>"; 
     } 
?> 
+1

我擴大了我的答案,以適應您的具體情況。我建議不要複製/粘貼代碼,因爲您在此顯示的是printSelectOptions,而不是此函數應該包含在使用的任何文件中。請檢查我是如何通過創建文件functions.php並在兩個文件中使用它。應對代碼總是錯誤的。我想現在我的答案可以被接受。 –

相關問題