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 122 123 124 125
| <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="icon" href="http://www.thesoftwareguy.in/favicon.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Multiple dropdown with jquery ajax and php">
<meta name="keywords" content="Multiple dropdown with jquery ajax and php">
<meta name="author" content="Shahrukh Khan">
<title>Multiple dropdown with jquery ajax and php - thesoftwareguy</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<style>
select {
padding:3px;
border-radius:5px;
background: #f8f8f8;
color:#000;
border:1px solid #EB028F;
outline:none;
display: inline-block;
width:250px;
cursor:pointer;
text-align:left;
font:inherit;
}
</style>
</head>
<body>
<div id="container">
<div id="body">
<div class="mainTitle" >Multiple dropdown with jquery ajax and php</div>
<div class="height20"></div>
<article>
<table style="margin:0 auto;width:50%" >
<tr>
<td align="center" height="50">
<?php
$sql = "SELECT distinct(code_postal) FROM ef_servicepublic ORDER BY code_postal";
try {
$stmt = $DB->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo($ex->getMessage());
}
?>
<label>Code postal:
<select name="code_postal" id="code_postal" onChange="showState(this);">
<option value="">Please Select</option>
<?php foreach ($results as $rs) { ?>
<option value="<?php echo $rs["code_postal"]; ?>"><?php echo $rs["code_postal"]; ?></option>
<?php } ?>
</select>
</label>
</td>
</tr>
<tr>
<td align="center" height="50"><div id="output1"></div> </td>
</tr>
<tr>
<td align="center" height="50"><div id="output2"></div> </td>
</tr>
</table>
</article>
</div>
</div>
<script src="jquery-1.9.0.min.js"></script>
<script>
function showState(sel) {
var code_postal = sel.options[sel.selectedIndex].value;
$("#output1").html("");
$("#output2").html("");
if (code_postal.length > 0) {
$.ajax({
type: "POST",
url: "fetch_state.php",
data: "code_postal=" + code_postal,
cache: false,
beforeSend: function() {
$('#output1').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#output1").html(html);
}
});
}
}
function showCity() {
//var commune = sel.options[sel.selectedIndex].value;
var code_postal = document.getElementById("code_postal" );
var commune = document.getElementById("commune" );
typeof(document.getElementById(commune));
if ((code_postal.options[code_postal.options.selectedIndex].value)
&& (commune.options[commune.options.selectedIndex].value ))
{
if (commune.length > 0) {
$.ajax({
type: "POST",
url: "fetch_city.php",
data: "commune=" + commune +'&code_postal='+ code_postal,
cache: false,
beforeSend: function() {
$('#output2').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#output2").html(html);
}
});
} else {
$("#output2").html("");
}
}
}
</script> |
Partager