php - how can i get countries states list from states table ,when i select country from countries table using multiple selection dropdown?
Get the solution ↓↓↓Solution:
Hi Ahmed,
Please have a look at the below code. I reworked the code and modified for multiple selections Anyway i have ignored the javascript validation. please work on that and name of this page was form3.php
<html>
<head>
<script type="text/javascript">
function viewstates()
{
jQuery('#langucountry').change(function () { });
}
$(function() {
$('#basicmultiselect').multiSelect({select_all_min: 3});
$('#langucountry').multiSelect({
select_all_min: 3, no_selection: "Please select!", selected_text: " clicked" });
$('#methods').multiSelect(); });
</script>
</head>
<body>
<form name="form3" action="form3.php" method="post">
<?php
/****** connection string *******/
/* start */
$conn = mysql_connect("localhost","root","");
$dbSelect = mysql_select_db("test");
/* end */
/*----checks whether form is submited -------*/
if(isset($_POST['cbocountry']))
{
$lstStates = array();
foreach($_POST['cbocountry'] as $countryCode)
{
$fetch_states="SELECT stateName FROM states where countryID = '$countryCode' ORDER by stateName ASC";
$Type_result=mysql_query($fetch_states);
while($Type_rows=mysql_fetch_array($Type_result)){
if(!in_array($Type_rows['stateName'],$lstStates))
array_push($lstStates,$Type_rows['stateName']);
}
}
}
$Type_sql="SELECT countryID,countryName FROM countries ORDER by countryName ASC";
$Type_result=mysql_query($Type_sql);
echo"Countries:<select id=\"langucountry\" name=\"cbocountry[]\" class=\"arc90_multiselect\" multiple=\"multiple\" title=\"Languages\"\>";
while($Type_rows=mysql_fetch_array($Type_result)){
echo "<option value='".$Type_rows['countryID']."'>";
echo $Type_rows['countryName'];
echo "</option>";
}
echo"</select>";
echo"States:<select><option value=''>--select--</option>";
if(count($lstStates) > 0)
{
foreach($lstStates as $item)
{
echo "<option value='";
echo $item;
echo "'>";
echo $item;
echo "</option>";
}
}
echo"</select>";
echo"<input type=\"submit\" value=\"submit\" text=\"submit\"/>";
?>
</form>
</body>
</html>
Enjoy!!!!
Answer
Solution:
This can be accomplished by putting the code for the states dropdown into a separate php handler, a bit modified:
<select name="state">
<?php
$countryID= mysql_real_escape_string($_GET['countryID']);
$Type_sql="SELECT stateName FROM states s where s.countryID = '$countryID' ORDER by stateName ASC";
$Type_result=mysql_query($Type_sql);
while($Type_rows=mysql_fetch_array($Type_result)){
echo "<option value='";
echo $Type_rows['stateName'];
echo "'>";
echo $Type_rows['stateName'];
echo "</option>";
}
?>
</select>
This script will be called by javascript using Ajax every time there is an onChange event in the countries select list. Please refer to ajax documentation like http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml or use jQuery for that: http://api.jquery.com/jQuery.get/
Modifying the content of the state select like your plan is now is possible too, but then you'd need two select lists, one more hidden list to store your removed entries or just all of them.
Answer
Solution:
This is a fully working code. Just place this code in a php page and change the name of the form in the onChange parameter to your page name.Also, change the database name in mysql_select_db.
My page name was form1.php
<html>
<body>
<form name="form1">
<?php
if(isset($_REQUEST['stateId']))
{
$stateId = $_REQUEST['stateId'];
}
else
{
$stateId = "";
}
$conn = mysql_connect("localhost","root","");
$dbSelect = mysql_select_db("test");
$Type_sql="SELECT countryID,countryName FROM countries ORDER by countryName ASC";
$Type_result=mysql_query($Type_sql);
echo"Countries:<select onChange=\"window.location='form1.php?stateId='+this.value\">";
while($Type_rows=mysql_fetch_array($Type_result)){
if($Type_rows['countryID']==$_REQUEST['stateId'])
{
echo "<option value='".$Type_rows['countryID']."' selected>";
echo $Type_rows['countryName'];
echo "</option>";
}
else
{
echo "<option value='".$Type_rows['countryID']."'>";
echo $Type_rows['countryName'];
echo "</option>";
}
}
echo"</select>";
$Type_sql="SELECT stateName FROM states where countryID = '$stateId' ORDER by stateName ASC";
$Type_result=mysql_query($Type_sql);
echo"States:<select><option value=''>--select--</option>";
while($Type_rows=mysql_fetch_array($Type_result)){
echo "<option value='";
echo $Type_rows['stateName'];
echo "'>";
echo $Type_rows['stateName'];
echo "</option>";
}
echo"</select>";
?>
</form>
</body>
</html>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: use the option --with-all-dependencies (-w) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.