Bonjour,
J'aimerais utiliser l'AJAX our discuter avec ma database. Un peu comme dans l'exemple ci après, mais au lieu d'avoir une liste déroulante j'aimerais rentrer quelque chose dans une barre de recherche.
j'ai commencé à coder avec le modèle du site, mais j'ai quelques problèmes et je suis bloqué. merci d'avance pour votre aide.
page index
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (this.readyState==4 && this.status==200) { document.getElementById("txtHint").innerHTML=this.responseText; } } xmlhttp.open("GET","traitement.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <FORM name="ajax_demo" method="GET" action=""> <INPUT type="text" name="this" maxlength="40" > <INPUT type="BUTTON" value="Vérifier" ONCLICK="showUser(this.value)"> </FORM> <br> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html>
page traitement.php
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 <!DOCTYPE html> <html> <head> <style> table { width: 100%; border-collapse: collapse; } table, td, th { border: 1px solid black; padding: 5px; } th {text-align: left;} </style> </head> <body> <?php $q = ($_GET['q']); $con = mysqli_connect('localhost','navalgroup','navalgroup','naval_group2'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"ajax_demo"); $sql="SELECT * FROM bd_tuyaux2 WHERE ordre_fabrication = '".$q."' "; $result = mysqli_query($con,$sql); echo "$q"; echo "<table> <tr> <th>Ordre de fabrication</th> <th>Repère Produit</th> <th>branche</th> <th>Diamètre</th> <th>EP</th> <th>explication matière</th> <th>Longueur nette</th> <th>quantité totale</th> </tr> "; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['ordre_fabrication'] . "</td>"; echo "<td>" . $row['repere_produit'] . "</td>"; echo "<td>" . $row['branche'] . "</td>"; echo "<td>" . $row['diametre'] . "</td>"; echo "<td>" . $row['explication_matiere'] . "</td>"; echo "<td>" . $row['longueur_nette'] . "</td>"; echo "<td>" . $row['quantite_totale'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> </body> </html>
Partager