通过Ajax传递mysql数据......我可以这样做吗?

时间:2013-04-30 11:37:17

标签: php mysql ajax

我是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.');
      }
    }
  }
})
();

如果这会冒犯职业头脑,我会再次感到抱歉,请告诉我正确的做法。

2 个答案:

答案 0 :(得分:0)

JavaScript是Clientside,PHP(和Mysql)是Serverside。这意味着您需要将JavaScript收集的数据从客户端发送到服务器。这可以通过AJAX完成。如果你愿意,jQuery有一个非常好用的界面来使用AJAX:

http://api.jquery.com/jQuery.post/ 用于POST 和 http://api.jquery.com/jQuery.post/ 为GET 最好的办法是看一看,并从那里开始工作。

答案 1 :(得分:0)

看来你也把mysql代码/函数放在javascript中,这确实不是正确的地方。

你应该做的是如下:

  1. 创建一个php文件yourcode.php,其中包含您期望作为AJAX调用响应的结果。

  2. 然后

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

    然后更改代码

    function makeRequest(url) {
        httpRequest.open('GET', url);
    
  3. 这将调用一个方法,该方法将调用您的'yourcode.php'并返回该文件呈现的输出。

  4. 如果您想发布数据,那么您应该考虑将其转换为JSON对象。

相关问题