I have 2 dropdown the main and sub. I'm trying to POST a value of two dropdownlist but everytime I submit I get this in url dyna.php?main=2&sub=2&submit=Submit
and nothing appears when I echo the post values. I want to post the value of two dropdown in same page because I'm going to make a query for the that values.
Here's my fullcode
dyna.php
<body>
<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF']?>">
Drop1
<?php
$mysqli = new mysqli("localhost", "root", "", "lists");
$result = $mysqli->query("SELECT * FROM main_list GROUP BY id ORDER BY id");
$option = '';
while($row = $result->fetch_assoc())
{
$option .= '<option value = "'.$row['id_no'].'">'.$row['value'].'</option>';
}
?>
<select id="main" name="main">
<option selected=selected>Choose</option>
<?php echo $option; ?>
</select>
<div id="sublist"></div>
<input type="submit" name="submit" value="Submit" />
</form>
<script type="text/javascript">
$('#main').change(function(){
$.ajax({
url : 'secondlist.php',
data :{mainlist_id : $(this).val()},
dataType:'html',
type:'POST',
success:function(data){
$('#sublist').html(data);
}
});
});
</script>
</body>
Secondlist.php
<?php
$out = $_POST['mainlist_id'];
$mysqli = new mysqli("localhost", "root", "", "lists");
$result1 = $mysqli->query("
select
a.id, a.value, a.id_no, b.id, b.category, b.value
from
(select *
from main_list) a
left outer join
(select *
from sub_list) b
on a.id_no=b.category
WHERE a.id_no='$out'
");
$option1 = '';
while($row = $result1->fetch_assoc())
{
$option1 .= '<option value = "'.$row['category'].'">'.$row['value'].'</option>';
}
$output = 'Drop2 ';
$output .= '<select name="sub" id="sub" onchange="run()">';
$output .= '<option value=" " disabled="disabled" selected="selected">Choose one</option>';
$output .= $option1;
$output .= '</select> ';
echo $output;
exit;
?>