我是 ajax 和 javascript 的新手,我找到了一个脚本,我确信它有基本的错误,但这是我目前所拥有的。
我有几个按钮,当您单击每个按钮时,检查图像会打开或关闭(这些按钮是从 mysql 查询请求信息的表的一部分,它的长度取决于 mysql 结果的数量)。
我有用 php 和 mysql 编写的脚本,但是由于我需要一个表单来将数据发布到该页面并且无法刷新,所以我现在坚持使用 ajax。
所以我想做的是把mysql放在ajax里面......好吧,如果这是一个很大的错误,我很抱歉,请帮助我以正确的方式......
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.');
}
}
}
})
();
再次抱歉,如果这冒犯了专业人士,请告诉我正确的方法。