我是新來的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.');
}
}
}
})
();
再次我很抱歉,如果這冒犯了專業的想法,歡迎告訴我做了正確的方式。
你應該使用'mysqli_ *'而不是'mysql_ *' – Dom 2013-04-30 11:38:18
你可以做!免費 ! – 2013-04-30 11:39:32
[**請不要在新代碼中使用'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