Bonjour,

J'ai une fonction qui m'affiche parfois le bon résultat, parfois non et je n arrive pas a comprendre pourquoi :

Voici d 'abord mon php :

Code : 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
<?php
require_once ('incl_php/inc_connex.inc');
mysql_select_db('gersimmo_db') OR die('Sélection de la base impossible');
$tableau = array();
 
 
$varatrans = $_POST['varatrans'];
$type = $_POST['type'];
$longch= $_POST['longch'];
 
if (is_numeric($varatrans)) 
{
        $sql = "SELECT * FROM test WHERE value like '$varatrans%'";
}
else
{
        $sql = "SELECT * FROM test WHERE nom_ville like '$varatrans%'";
}
        $req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
 
        $resultat=mysql_query($req); 
 
        while($ligne = mysql_fetch_array($req))
        {
                $id=$ligne['id'];
                $codes = $ligne['value'];
                $nom_ville = $ligne['nom_ville'];
                $tableau [$varatrans][]= array($nom_ville." "."(".$codes.")");
        }  
echo json_encode($tableau);
?>
et mon js / html

Code : 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
    <title>Titre de la page</title>
    <meta name="keywords" lang="fr" content="motcle1,mocle2" />
    <meta name="description" content="Description de ma page web." />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Language" content="fr" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <link href="styles/style_menu.css" rel="stylesheet" type="text/css" />
	<link href="styles/styles.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" language="Javascript" src="js/jquery.js"></script>
	<script type="text/javascript">
 
 
 
 
$(function(){
	$("#loc").val(32);
	$("#loc").keyup(function (){
	var varatrans = ($("#loc").val());
	$("#code_pos").show();
 
	var type;
	var longch; 
	 if(isNaN(varatrans))
	{
	type = "string";	
	$("#loc").attr("maxlength","50");
	}
	else
	{
		type = "num";
		varatrans = $.trim(varatrans.replace(/[\s]/gi,""));
		$("#loc").val(varatrans);
		$("#loc").attr("maxlength","5");
	}	
	longch = varatrans.length;
 
		$("#sel_city").empty();
		$("#empt").empty();
		$("#code_pos").empty();
			$.ajax({
   			type:"POST",
   			url:"test.php",
   			data:"varatrans=" + varatrans + "&type=" + type + "&longch=" + longch,
			dataType:"json",
   			success: function(tableau){
				var villages = tableau[varatrans];
				for(var key in villages) {
					$("#code_pos").append("<p><a href=\"#\" id=\"key\">"+villages[key]+"</a></p>");
					$("#code_pos a").click(function (){
					$("#loc").val($("#key").text());					
					$("#code_pos").hide();
					});
 
				}		
  			}
			});			
	});	
});
	</script>
 
	<style>
	#cont_princ{width:980px;height:500px;margin:auto;border:1px solid green;}
	#cont_menu{width:980px;height:60px;border:1px solid red;background:yellow;}
	#cont_menu div {float:left;border:1px solid red;margin-right:10px;color:green;padding:15px;padding-top:3px;padding-bottom:3px;}
	#form_search{width:980px;height:300px;border:1px solid black;}
	#parts{width:220px;height:300px;border:1px solid black;float:left;}
	#search{width:600px;height:300px;float:left;}
	#empt{width:150px;height:300px;border:1px solid black;float:left;}
	#connex_util{background:grey;margin-top:0px;}
	#connex_util label{height:12px;padding:3px;}
	#connex_util input{height:12px;padding:3px;margin:5px;border:1px solid grey;color:red;font-weight:bold;line-height:0.8;}
	#code_pos{width:auto;height:auto;position:absolute;border:1px solid grey;padding:10px;margin-top:0px;margin-left:60px;background:white;display:none;}
	#code_pos a{color:red;font-weight:bold;}
	</style>
</head>
<body>
<div id="cont_princ">
 
	<a href="#"><div id="head" style="width:980px;height:80px;background-image:url(grafs/logofirst.jpg);background-repeat:no-repeat;">
 
	</div></a>
 
	<div id="connect" style="width:980px;height:50px;border:1px solid red;padding-bottom:5px;background:grey;">
		<form method="post" action="connex_mb.php" id="connex_util">
   			<p>
       			<label for="login">Login</label><input type="text" name="login" id="login" size="10" maxlength="8" />
				<label for="mpasse">Mot de passe</label><input type="password" name="mpasse" id="mpasse" size="10" maxlength="8" />
   			</p>
		</form>
	</div>
	<div id="cont_menu">			
			<div><input type="radio" name="cat" value="1" />Achat</div>
			<div><input type="radio" name="cat" value="2" />Location</div>
			<div><input type="radio" name="cat" value="3" />Viager</div>
			<div><input type="radio" name="cat" value="4" />Immobilier d'entreprise</div>
			<div><input type="radio" name="cat" value="5" />Fonds de commerce</div>
			<div><input type="radio" name="cat" value="6" />Vacances</div>
	</div> 
	<div id="corps">		
		<div id="form_search">
			<div id="parts">
			</div>
			<div id="search">
				<form method="post" action="test.php" id="search">
       				<label for="loc">Localisation</label><input type="text" name="loc" id="loc" size="20" maxlength="40" />
					<div id="code_pos"></div>   					
					<input value="RECHERCHE >>>" id="btn_engine_p" tabindex="50" type="submit" style="color:white;font-weight:bold;background:green;border:2px solid grey;">
				</form>
			</div>
			<div id="empt">
			</div>
		</div>
	</div
 
</div>
 
</body>
</html>
la partie qui me pose problème :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
for(var key in villages) {
					$("#code_pos").append("<p><a href=\"#\" id=\"key\">"+villages[key]+"</a></p>");
					$("#code_pos a").click(function (){
					$("#loc").val($("#key").text());					
					$("#code_pos").hide();
					});
 
				}

Par avance merci