[AJAX] Select list et selected value
Bonjour !
Voila j'essaie depuis ce matin de faire une liste chainer pour le pays département, region...
cela fonctionne ! mais parcontre dès que je soumet le formulaire il perd les values !
Voici mon code location.php:
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 58 59 60
| <?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("content-type: application/x-javascript; charset=utf-8");
require("connect.php");
$data = $_GET['data'];
$val = $_GET['val'];
if($data == 'states')
{
echo "<select name=\"states\" onchange=\"dochange('region', this.value)\" class=\"location\">\n";
echo "<option value='0'>Choose state</option>";
foreach(MYSQLLocation::getCountries() as $country)
{
echo '<option value="'.$country['id_country'].'">'.utf8_encode($country['fr'])."</option>\n";
}
}
else if($data == 'region')
{
echo "<select name=\"regions\" onchange=\"dochange('departement', this.value)\" class=\"location\">\n";
echo '<option value="0">Selectionnez une region</option>'."\n";
foreach (MYSQLLocation::getRegions($val) as $region)
{
echo '<option value="'.$region['id'].'">'.utf8_encode($region['name'])."</option>\n";
}
}
else if($data == 'departement')
{
echo "<select name=\"departements\" onchange=\"dochange('city', this.value)\" class=\"location\">\n";
echo '<option value="0">Chose a region</option>'."\n";
foreach (MYSQLLocation::getDepartements($val) as $dep)
{
echo '<option value="'.$dep['id'].'">'.$dep['id']." - ".utf8_encode($dep['name'])."</option>\n";
}
}
else if($data == 'city')
{
echo '<select name="cities" class=\"location\">'."\n";
echo '<option value="0">Selectionnez une Ville</option>'."\n";
foreach (MYSQLLocation::getCities($val) as $city)
{
echo '<option value="'.$city['id'].'">'.$city['zipcode'] . " - " .utf8_encode($city['city'])."</option>\n";
}
}
echo "</select>\n";
?> |
Mon fichier javascript :
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
| function getXHR()
{
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
}
function dochange(src, val)
{
var req = getXHR();
req.onreadystatechange = function ()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
document.getElementById(src).innerHTML=req.responseText;
}
}
};
req.open("GET", "http://www.monsite.dev/ajax/location.php?data="+src+"&val="+val);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
req.send(null);
}
window.onLoad=dochange('states', -1); |
et mon fichier php d'affichage (je met que la partie concerné)
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
| <tr>
<td>Pays : </td>
<td>
<div id="states"> </div>
</td>
</tr>
<tr>
<td>Region : </td>
<td><div id="region">
<select disabled="disabled" class="location">
<option value="0">Veuillez choisir un pays</option>
</select>
</div></td>
</tr>
<tr>
<td>Departement : </td>
<td>
<div id="departement">
<select disabled="disabled" class="location">
<option value="">Veuillez choisir un departement</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Ville : </td>
<td>
<div id="city">
<select name="cities" disabled="disabled" class="location">
<option value="">Veuillez choisir une ville</option>
</select>
</div></td>
</tr> |
Je sais pas si c'est super clean mais trouver des dropdowns sur le net qui gère le selected j'ai pas trouver ! même au us
Donc si quelqu'un à un tuyau je suis prenneur