i have two drop downs. first drop down populate from database. second drop down populated from database based on selected value of first drop down.
$(document).ready(function() {
$("#c").change(function() {
var c1 = $('#c :selected').text();
if(c1 != "") {
$.ajax({
url:'getstatw.php',
data:{c:c1},
type:'POST',
success:function(response) {
var resp = $.trim(response);
$("#c").html(resp);
}
});
} else {
$("#c").html("<option value=''>Select state</option>");
}
});
});
<form id = "world" method="post" action="insert.php">
<select name="country" id="c" style = "width:200px" class="btn btn-primary dropdown-toggle" ;>
<option>country</option>
<?php
$sql = "select DISTINCT country from table1";
$res = mysqli_query($con, $sql);
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_object($res)) {
echo "<option value='".$row->id."'>".$row->c."</option>";
}
}
?>
</select>
<br><br>
<label for="s" >State</label>
<select name="State" id="s" style = "width:200px " ; class="btn btn-primary dropdown-toggle";><option>Select state</option></select><br><br>
<button id = "sub" type="submit" class="btn btn-primary" disabled>Submit</button>
</form>
insert.php
$con=mysqli_connect("localhost","root","","world");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
//home tab
$c = mysqli_real_escape_string($con, $_POST['country']);
$s = mysqli_real_escape_string($con, $_POST['state']);
//query for table_mainast
$sql1="INSERT INTO table1 (Country, State)
VALUES ('$c', '$s',)";
//query for table_dataast
if (!mysqli_query($con,$sql1)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['c'])) {
$sql = "select DISTINCT `State` from `table2` where `Country`='".mysqli_real_escape_string($con, $_POST['c'])."'";
$res = mysqli_query($con, $sql);
if(mysqli_num_rows($res) > 0) {
echo "<option value=''>------- Select --------</option>";
while($row = mysqli_fetch_object($res)) {
echo "<option value='".$row->id."'>".$row->c."</option>";
}
}
} else {
header('location: ./');
}
?>
i have tried almost all solution given on net. but do not understand my data is not inserted into mysql database. How to insert HTML select value as text in MySQL via PHP PHP Drop down list selected value not inserted in the database