2013-04-30 245 views
0

我是新來的ajax和javascript,我到達了一個腳本,我確信它有基本的錯誤,但這是我目前所擁有的錯誤。通過Ajax傳遞mysql數據...我可以做到嗎?

我有幾個按鈕,當你點擊它們中的每一個時,打開或關閉檢查圖像(按鈕是請求來自mysql查詢的信息的表的一部分,並且它的長度取決於mysql結果的數量)。

我有用php和mysql編寫的腳本,但是因爲我需要一個表單來發布數據到那個頁面並且無法刷新我現在被ajax卡住了。

那麼我現在要做的是把MySQL的阿賈克斯裏面......好吧,我很抱歉,如果這是一個很大的錯誤,請幫我做正確的方式...

PHP代碼:

(...code...) 

$query = "SELECT `CÔR`, `keyword`, `Adds`, `PRMédio`, `PRDomínioMédio`, `Searches`, `CPC`, `.com`, `.org`, `.net`, `All in URL`, `All in Title`, `All in Desc.` 
FROM keywords WHERE (`Adds`>='$adds' && `Adds`<='$addsm' && `PRMédio`>='$pr' && `PRMédio`<='$prm' && `PRDomínioMédio`>= '$prdom' && `PRDomínioMédio`<= '$prdommax' 
&& `Searches`>='$s' && `Searches`<='$smax' && `CPC`>='$cpc' && `CPC`<='$cpcmax')"; 

if ($query_run = mysql_query($query)){ 


while($query_row = mysql_fetch_assoc($query_run)){ 

$côr = $query_row['CÔR']; 

(...code...) 

<td>    
<button id='ajaxButton'>Select</button> 
</td> 

(...code... 'CÔR' is the only variable that matters, it's the binnary one that turns on and off the image) 

的JavaScript:

(function() { 
    var httpRequest; 
    document.getElementById('ajaxButton').onClick = function('$q') { 


    $n = "SELECT `CÔR` FROM `keywords` WHERE `keyword`='$q'"; 
    $b = mysql_query ($n); 
    $row = mysql_fetch_array($b); 


    echo "$row['CÔR'];"; 
    $t = $row['CÔR']; 
    if ($t == 1) { 
    $m = "UPDATE `keywords` SET `CÔR`=0 WHERE `keyword`='$q'"; 
    mysql_query ($m); 
     } 
    if ($t == 0) { 
    $l = "UPDATE `keywords` SET `CÔR`=1 WHERE `keyword`='$q'"; 
    mysql_query ($l); 
     } 

}; 

    function makeRequest(index.php) { 
    if (window.XMLHttpRequest) { 
     httpRequest = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
     try { 
     httpRequest = new ActiveXObject('Msxml2.XMLHTTP'); 
     } 
     catch (e) { 
     try { 
      httpRequest = new ActiveXObject('Microsoft.XMLHTTP'); 
     } 
     catch (e) {} 
     } 
    } 

    if (!httpRequest) { 
     alert('Giving up :(Cannot create an XMLHTTP instance'); 
     return false; 
    } 
    httpRequest.onreadystatechange = alertContents; 
    httpRequest.open('GET', index.php); 
    httpRequest.send(); 
    } 

    function alertContents() { 
    if (httpRequest.readyState === 4) { 
     if (httpRequest.status === 200) { 
     alert(httpRequest.responseText); 
     } else { 
     alert('There was a problem with the request.'); 
     } 
    } 
    } 
}) 
(); 

再次我很抱歉,如果這冒犯了專業的想法,歡迎告訴我做了正確的方式。

+1

你應該使用'mysqli_ *'而不是'mysql_ *' – Dom 2013-04-30 11:38:18

+0

你可以做!免費 ! – 2013-04-30 11:39:32

+2

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://www.brightmeup.info/article.php?a_id=2)。 – 2013-04-30 11:40:40

回答

0

JavaScript是Clientside,PHP(和Mysql)是Serverside。這意味着您需要將從客戶端收集的JavaScript數據發送到服務器。這可以通過AJAX完成。如果你想,jQuery有一個真正的好和方便的接口使用AJAX:

http://api.jquery.com/jQuery.post/ 爲POST 和 http://api.jquery.com/jQuery.post/ 爲GET 最好的辦法是看看那個,和工作從那裏。

0

看來,你也把MySQL的代碼/函數在JavaScript中,這確實是不正確的地方。

你應該做的,什麼是如下:

  1. 創建與您所期待的AJAX調用的響應結果的PHP文件yourcode.php。

  2. 然後

    document.getElementById('ajaxButton').onClick = function('$q') { 
        url = 'yourcode.php'; 
        makeRequest(url); 
    

    然後作出更改代碼

    function makeRequest(url) { 
        httpRequest.open('GET', url); 
    
  3. 這將調用哪個inturn會打電話給你「yourcode.php」和返回輸出是由文件中呈現的方法。

如果你想發佈數據,那麼你應該考慮把它轉換成JSON對象。