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 .
select (, functions.php printSelectOptions):
<?php
require_once("functions.php");
?>
<select name="table_id" class="form-control tablename">
<?php printSelectOptions() ?>
</select>
bind ( javascript):
document.addEventListener("DOMContentLoaded", function(event) {
var select=document.querySelector("select");
function refreshOptions(){
select.innerHTML="<option>Loading..</option>";
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", 'yourSecondPHPScript.php');
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if(xmlhttp.status == 200){
select.innerHTML = xmlhttp.responseText;
}else{
console.log('Error: ' + xmlhttp.statusText )
select.innerHTML="<option>Connection problem</option>";
}
}
}
xmlhttp.send();
};
select.addEventListener("focus",function(){
refreshOptions();
});
});
yourSecondPHPScript.php :
<?php
require_once("functions.php");
printSelectOptions();
, , , - . , , select ( 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 CODE):
if (tableCanBeTaken($youOptionId)){
//here code for taking option
}else{
//here option is taken
}