Search code examples
javascriptmysqlajaxrequestxmlhttprequest

ajax to receive data from database request does not work


I'm a really beginner in the ajax domain, and I really need help.

In fact I have done a query to receive some data from a database.

Here on the header I have:

<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>

on the page ajaxclient.php I have the following code:

<?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>

The page called create_dossier.php contain:

<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>

What I would like is to display the data without sending the page and the thing is that it always send the form before returning any data. For that I do not need ajax.

I really do not know what wrong and how to correct it.


Solution

  • You should returning the result of query as XML or JSON and filling the tables with XML in calling file.

    This is an example php.file (Need to convert to 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();
    ?>