[AJAX] Affichage dynamique à partir d'un formulaire
Bonjour,
J'ai un formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<table width="361" border="0" cellspacing="0" cellpadding="0" hspace="0" vspace="0" class="tab-contenu-jacks-chez-vous">
<form name="formulaire2">
<tr>
<td width="129"> </td>
<td width="125" valign="top">Trouver mon resto<br/>et commander :</td>
<td width="68" align="left"><input name="departement" type="text" onCLick="return field2()" class="departement" value="Dept. <? //echo $_POST['departement']; ?>" size="10" /></td>
<td width="37" align="left"><div onClick="cherchedep('result',formulaire2.departement.value)">OK</div></td>
</tr>
</form>
<tr height="73">
<td> </td>
<td colspan="3" valign="top">
<div id="result" class="result-recherche-adresse"></div>
</td>
</tr>
</table> |
une fonction Ajax :
Code:
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
|
var xhr = null;
function getXhr(){
if(window.XMLHttpRequest) // Firefox et autres
xhr = new XMLHttpRequest();
else if(window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
xhr = false;
}
}
function cherchedep(div, departement){
getXhr();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
contenuajax = xhr.responseText;
document.getElementById(div).innerHTML = contenuajax;
}
}
xhr.open("POST","func_dep.php",true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("departement="+departement);
} |
Et ma page php qui va faire le traitement pour l'affichage
Code:
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
|
<?
include("admin/connect.php");
$deptRech=substr($_POST['departement'], 0, 2);
$req="select id_restos, rueResto, cpResto, villeResto, telResto from ".$extension."restos";
$res=mysql_query($req, $link);
$trouve=0;
$i=0;
while($rechCp=mysql_fetch_object($res))
{
$dept=substr($rechCp->cpResto, 0, 2);
if(strcmp($deptRech, $dept) == 0)
{
$trouve=1;
$identifiant[$i] = $rechCp->id_restos;
$rue[$i]=stripslashes($rechCp->rueResto);
$cp[$i]=stripslashes($rechCp->cpResto);
$ville[$i]=stripslashes($rechCp->villeResto);
$tel[$i] = $rechCp->telResto;
$i++;
}
}
if($trouve==1)
{
if($i>0)
{
for($j=0; $j<$i; $j++)
{
$result[$j] = "<a href='outils/identification_membre.php?resto=".$identifiant[$j]."'>".$rue[$j]."<br />".$cp[$j]." ".$ville[$j]."<br />".$tel[$j]."<br /></a><br />";
}
}else{
$result[0] = "<a href='outils/identification_membre.php?resto=".$identifiant[0]."'>".$rue[0]."<br />".$cp[0]." ".$ville[0]."<br />".$tel[0]."</a>";
}
}else{
$result[0] = "Désolé il n'y a pas de restaurant dans ce département";
}
for($j=0; $j<=$i; $j++){
echo $result[$j];
}
?> |
En gros je fais une recherche a partir d'un champs texte et sur envoie j'essaie d'exécuter ma fonction Ajax qui va afficher dans mon <div>LE RESULTAT</div> mais bon sa marche pas lol
Pouvez vous m'aider ??