2012-10-02 33 views
0

我真的是ajax领域的初学者,我真的需要帮助。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> 

我想显示的数据没有发送的页面和事情是,它总是发送fo rm返回任何数据之前。为此,我不需要Ajax。

我真的不知道什么是错,以及如何纠正它。

回答

1

你应该返回的结果查询为XML或JSON,并用调用文件中的XML填充表格。

这是一个php.file示例(需要转换为PDO) require(“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(); 
?> 
1

其实,你应该避免提交表单是这样的:

onclick="return doSomeAction();" 

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

doSomeAction()在你的情况 =阿贾克斯()不要忘记返回false

+0

亲爱的主席先生感谢您的回复我试过这样做,但它只是发送文档表单 –