2014-10-17 39 views
0

getseats.php包含回報,如果座位的狀態爲0,則可點擊圖標應該變爲不可用,難以實現!任何幫助或建議將不勝感激!香港專業教育學院下面所示我的代碼:根據數據庫在頁面上改變圖像

<?php 
$noerrors=dbconnect(); 
if($noerrors <> 0) { 
    echo '{"errorcode":"'.$noerrors.'"}'; 
} else { 
    $query = "select seatnum from seats where status='0'"; 
    $link = mysql_query($query); 
    if (!$link) { 
    echo '{"errorcode":"3"}'; 
    } else { 
    $rows = array(); 
    while($r = mysql_fetch_assoc($link)) { 
    $rows[] = $r; 
    } 
    $json=json_encode($rows); 
    echo $json; 
    } 
} 
function dbconnect(){ 
    $hostname = "localhost"; 
    $username = "root"; 
    $password = "root"; 
    $noerrors = 0; 
    $link = @ mysql_connect($hostname, $username, $password); 
    if (!$link) { 
    $noerrors = 1; 
    } else { 
    $db_selected = @ mysql_select_db('bookings', $link); 
    if (!$db_selected) { 
    $noerrors = 2; 
    } 
    } 
    return $noerrors; 
} 

我也有一個JavaScript文件(booking.js)將包含在HTML腳本中提到的功能,但不包含代碼爲套牢部分IM。以下是第一行的HTML。

HTML:

<div id='right' style='float:right; margin-top:2%;margin-right:15%'> 
    <div style=""> 
    <table style="align:center"> 
    <tr> 
    <td ColSpan="7">A </td> 
    <td><img id = "A1" src="images/available.gif" style="border:none" onmouseover="over(this)"  onmouseout="out(this)" onclick="sold(this.id)" /></td> 
    <td><img id = "A2" src="images/available.gif" style="border:none" onmouseover="over(this)" onmouseout="out(this)" onclick="sold(this.id)" /></td> 
    <td><img id = "A3" src="images/available.gif" style="border:none" onmouseover="over(this)" onmouseout="out(this)" onclick="sold(this)" /></td> 
    <td><img id = "A4" src="images/available.gif" style="border:none" onmouseover="over(this)" onmouseout="out(this)" onclick="sold(this)" /></td> 
<td><img id = "A5" src="images/available.gif" style="border:none" onmouseover="over(this)" onmouseout="out(this)" onclick="sold(this)" /></td> 
<td><img id = "A6" src="images/available.gif" style="border:none" onmouseover="over(this)" onmouseout="out(this)" onclick="sold(this)" /></td> 
<td ColSpan="6"></td> 
</tr> 

我如何實現圖像從根據使用JSON回報AJAX請求 'available.gif' 到 'taken.gif' 而改變。

+0

注意:你不應該再使用mysql_'了。 – JiFus 2014-10-17 09:34:15

回答

0

我只關注售出的功能,因爲你的代碼有很多擔心(也許你還沒有提交足夠的代碼)。我不確定你想要放置Ajax調用的位置,但是這裏有一個函數可以切換可用/已拍攝的圖像。

function sold(id) { 
    var ele = document.getElementById(id); // get image element 
    //--- toggle image 
    var src = (ele.src == 'images/available.gif') ? 'images/taken.gif' 
               : 'images/available.gif'; 
    ele.src = src; //--- update image src 
} 
+0

ive將getSeats()放置在html中的腳本標記中,因爲它將在頁面加載時更改。感謝您的輸入! – user3487430 2014-10-17 09:59:22

相關問題