Reputation: 35
This is jquery
$(document).ready(function () {
$('#Main').change(function () {
var Sel = $(this).val();
console.log(Sel);
$(".Subselect").hide();
$(".lastSel").hide();
$("#" + Sel).show();
$.ajax({
type: "POST",
url: "test.php",
data: "id=" + Sel + "&part_id="+$(this).attr('name'), //part_id = region
success: function(data) {
$(".region").html(html);
}
});
});
$('.Subselect').change(function () {
var subSel = $(this).val();
console.log(subSel);
$(".lastSel").hide();
$("#" + subSel).show();
$.ajax({
type: "POST",
url: "test.php",
data: "id=" + subSel + "&part_id="+$(this).attr('name'), //part_id = city
success: function(data) {
$(".city").html(html);
}
});
})
});
This is mysql table
CREATE TABLE IF NOT EXISTS `s_countries` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(2) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0',
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_name` (`name`),
KEY `id_code` (`code`)
) ENGINE=MyISAM;
CREATE TABLE IF NOT EXISTS `s_regions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`countryid` int(11) unsigned NOT NULL DEFAULT '0',
`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_countryid` (`countryid`),
KEY `id_name` (`name`)
) ENGINE=MyISAM;
CREATE TABLE IF NOT EXISTS `s_cities` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`countryid` int(11) unsigned NOT NULL DEFAULT '0',
`regionid` int(10) unsigned NOT NULL DEFAULT '0',
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `id_name` (`name`),
KEY `id_regionid` (`regionid`),
KEY `id_countryid` (`countryid`)
) ENGINE=MyISAM;
This is mailtest.php (i tried many options, but cat do it work) here is example in (html + Jquery http://jsfiddle.net/Nazaret2005/bPUbb/
<?
require "include/db.php";
dbconn(true);
loggedinorreturn(true);
echo "<form method=\"POST\">";
$cat = "<select id='Main' name='country'>";
$cat .= "<option value='0'>Выбрать</option>";
$results = do_mysql_query("SELECT id, code, name FROM s_countries") or sqlerr(__FILE__, __LINE__);
while($c = mysqli_fetch_array($results)){
$cat .= "<option value='".$c['id']."'>".$c['name']."</option>";
}
$cat .= "</select>";
echo $cat;
echo "<select name='region' class='Subselect'>";
echo "<option value='0'>-Выбрать-</option>";
echo "</select>";
?>
and this is test.php
<?
require_once("include/db.php");
dbconn(true);
if(isset($_POST['id'])){
$id=(int)$_POST['id'];
$res = do_mysql_query("SELECT id, name FROM s_regions WHERE countryid = '".$id."'") or sqlerr(__FILE__, __LINE__);
if(mysqli_num_rows($res) > 0){
while($r = mysqli_fetch_array($res)){
echo "<option value='".$r['id']."'>".$r['name']."</option>";
}
} }
?>
I wanna do this work with database like in html example http://jsfiddle.net/Nazaret2005/bPUbb/ , countries, regions and cities. But i can't do it work....
If it's ease for some one. please help to do it work. thanks
Upvotes: 1
Views: 171
Reputation: 840
Make sure about
data: "id=" + Sel + "&part_id="+$(this).attr('name'),
Sel variable and $(this).attr('name') have value and you are getting that value.
Upvotes: 0
Reputation: 11104
here is i found two mistake..
GET
" coz u sending data in url$_GET['id']
insted of $_POST
made changes abouve and lets see what happen..
OR try this .. IF u want to send data using POST
if you want to send data using post then
$(document).ready(function () {
$('#Main').change(function () {
var Sel = $(this).val();
console.log(Sel);
$(".Subselect").hide();
$(".lastSel").hide();
$("#" + Sel).show();
$.ajax({
type: "POST",
url: "test.php",
data: {id: Sel ,part_id: $(this).attr('name'), //part_id = region
success: function(data) {
$(".region").html(data);
}
});
});
$('.Subselect').change(function () {
var subSel = $(this).val();
console.log(subSel);
$(".lastSel").hide();
$("#" + subSel).show();
$.ajax({
type: "POST",
url: "test.php",
data: {id:subSel ,part_id :$(this).attr('name'), //part_id = city
success: function(data) {
$(".city").html(data);
}
});
})
});
and in ur PHP file keep it same
Upvotes: 1
Reputation: 347
change the $(".city").html(html); to
$(".city").html(data);. your mention as a function(data)
so call as html(data) ok.
Upvotes: 0