【问题标题】:ajax to receive data from database request does not workajax 从数据库请求接收数据不起作用
【发布时间】:2012-09-23 00:44:20
【问题描述】:

我是 ajax 领域的真正初学者,我真的需要帮助。

事实上我已经做了一个查询来从数据库中接收一些数据。

在我的标题上:

<script type="text/javascript">

    <?php if(isset($_GET['p']) AND $_GET['p']=='create_dossier') {?>
    function writeInDiv(text){
        var objet = document.getElementById('code_client');
        objet.innerHTML = text;
    }

    function ajax()
    {
        var xhr=null;

        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhr.open("GET", "ajaxclient.php?code_client=<?php echo $_GET['code_client'] ; ?>", false);
        xhr.send(null);
        writeInDiv(xhr.responseText);
            setInterval("ajax()",5000);
    }
    <?php }?>
    </script>

在页面 ajaxclient.php 我有以下代码:

<?php require_once('Connections/localhost.php'); ?><?php mysql_select_db( $database_localhost ); ?>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
</head>
<body>
 <table width="100%" border="0" id="box-table-a">
  <tr>
    <th scope="col" width="15%">CODE CLIENT</th>
    <th scope="col" width="15%">RAISON SOCIALE</th>
    <th scope="col" width="55%">ADRESSE </th>
    <th scope="col" width="15%">ACTION</th>
  </tr>
<?php 
$code_client=$_GET["code_client"];
      $sql="SELECT * FROM `client` INNER JOIN `adresse_client` ON `client`.`code_client` = `adresse_client`.`code_client` WHERE `client`.`code_client`='".$code_client."'";
        if (mysql_errno() <> 0) 
        {
            echo mysql_error(),'<br>',$sql,'<br>';
            die();
        } 
        $result=mysql_query($sql);
        while($donnees=mysql_fetch_assoc($result))
        {?>

  <tr>
    <td><?php echo $donnees['code_client'] ; ?></td>
    <td><?php echo $donnees['raison_sociale'] ; ?></td>
    <td><p align="left">&nbsp;<?php echo $donnees['rue'].'<br>'.$donnees['complement1'].'<br>'.$donnees['complement2'].'<br>'.$donnees['code_postal'].' - '.$donnees['ville'].'<br>'.$donnees['pays'] ; ?></p></td>
    <td><a href="index.php?p=create_dossier2&amp;code_client=<?php echo $data['code_client'] ; ?>"><img src="images/001_18.gif" width="24" height="24" /></td>
  </tr>
 <?php }
?></table>
</body>
</html>

名为 create_dossier.php 的页面包含:

<form action="#" method="get"><fieldset>
 <legend>CR&Eacute;ANCIER</legend>
 <br />
 <label>Raison sociale:</label><input type="hidden" name="p" value="create_dossier" /> <input type="text" name="code_client" onkeypress="form.submit()" /></fieldset><p align="center"><input type="submit" name="enreg" value="Chercher !" /></form>

我想要的是在不发送页面的情况下显示数据,而且它总是在返回任何数据之前发送表单。为此,我不需要 ajax。

我真的不知道哪里错了,如何改正。

【问题讨论】:

    标签: javascript mysql ajax request xmlhttprequest


    【解决方案1】:

    您应该以 XML 或 JSON 格式返回查询结果,并在调用文件中使用 XML 填充表格。

    这是一个示例 php.file (需要转换为 PDO) 要求("dbinfo.php");

    // Get parameters from URL
    $code_client=$_GET["code_client"];
    // Start XML file, create parent node
    $dom = new DOMDocument("1.0");
    $node = $dom->createElement("markers");
    $parnode = $dom->appendChild($node);
    
    // Opens a connection to a mySQL server
    $connection=mysql_connect ($host, $username, $password);
    if (!$connection) {
      die("Not connected : " . mysql_error());
    }
    
    // Set the active mySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
    die ("Can\'t use db : " . mysql_error());
     }
    
    // Search the rows in the markers table
    $sql="SELECT * FROM `client` INNER JOIN `adresse_client` ON `client`.`code_client` = `adresse_client`.`code_client` WHERE `client`.`code_client`='".$code_client."'";
        if (mysql_errno() <> 0) 
        {
            echo mysql_error(),'<br>',$sql,'<br>';
            die();
        } 
        $result=mysql_query($sql);
        while($donnees=mysql_fetch_assoc($result))
    }
    header("Content-type: text/xml");
    
        // Iterate through the rows, adding XML nodes for each
                while ($row = @mysql_fetch_assoc($result)){
                $node = $dom->createElement("client");
                $newnode = $parnode->appendChild($node);
                $newnode->setAttribute("'code_client'", $row['code_client']);
                //$newnode->setAttribute("xxx", $row['xx']); list all other
    
        }
    }
    
    
    echo $dom->saveXML();
    ?>
    

    【讨论】:

      【解决方案2】:

      事实上,你应该阻止这样的表单提交:

      onclick="return doSomeAction();"
      
      function doSomeAction() {
          // create XHR object
          // send data
          // handle response
          return false;
      }
      

      doSomeAction() = ajax() 在你的情况下 不要忘记返回false

      【讨论】:

      • 尊敬的先生,感谢您的回复我已经尝试这样做,但它只是发送文档表格
      猜你喜欢
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      相关资源
      最近更新 更多