Reputation: 111
I have 2 drop down list, one is college and other is branch, so what i want to do is that whenever a user selects a college from the first drop down list, the script should automatically check for the branches available in that college and should add them to the second drop down list i.e. of branches, currently I am doing it manually.
<?php
mysql_connect("localhost", "root", "") or die("Connection Failed");
mysql_select_db("pet")or die("Connection Failed");
$query = "SELECT distinct insti_name FROM maininfo";
$result = mysql_query($query);
$query2 = "SELECT distinct insti_name FROM maininfo";
$result2 = mysql_query($query2);
?>
<form name="myform" id="one" method="post" action="compare.php">
Select first college:
<select name="coll1">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['insti_name'];?>"> <?php echo $line['insti_name'];?>
</option>
<?php
}
?>
</select>
<br /><p>Select the branch:
<select name="brn1">
<option value="0" selected>(please select:)</option>
<option value="ce">Civil</option>
<option value="cse">Computer Science Engineering</option>
<option value="ec">Electronics and Communication</option>
<option value="it">Information Technology</option>
<option value="aeie">AEIE</option>
<option value="mech">Mechanical</option>
<option value="elect elex">Elect Elex</option>
<option value="ee">Electricals</option>
<option value="el">EL</option>
<option value="chem">Chemical</option>
<option value="fire">Fire</option>
<option value="ei">ei</option>
<option value="food">Food</option>
<option value="ip">Information Practises</option>
<option value="bt">Bio Technology</option>
</select>
<br>
<hr width=100% height=4>
Select second college:
<select name="coll2">
<?php
while ($line = mysql_fetch_array($result2, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['insti_name'];?>"> <?php echo $line['insti_name'];?>
</option>
<?php
}
?>
</select>
<br /><p>Select the branch:
<select name="brn2">
<option value="0" selected>(please select:)</option>
<option value="ce">Civil</option>
<option value="cse">Computer Science Engineering</option>
<option value="ec">Electronics and Communication</option>
<option value="it">Information Technology</option>
<option value="aeie">AEIE</option>
<option value="mech">Mechanical</option>
<option value="elect elex">Elect Elex</option>
<option value="ee">Electricals</option>
<option value="el">EL</option>
<option value="chem">Chemical</option>
<option value="fire">Fire</option>
<option value="ei">ei</option>
<option value="food">Food</option>
<option value="ip">Information Practises</option>
<option value="bt">Bio Technology</option>
</select>
<br><br>
<input type="submit" name="Submit" value="Compare Colleges" >
</form>
Upvotes: 0
Views: 4914
Reputation: 121
I would use ajax if your lists are huge.
If not you can prepopulate an array in Javascript and change the options on onchange
.
Upvotes: 0
Reputation:
For something like this, you would either have to implement an AJAX solution that updated the page dynamically, or you would have to POST the "college" value back to your page and.... (I'm sitting here typing this and three new answers have just been posted...?) ...requery your database based on the college.
Upvotes: 0
Reputation: 39520
Remember the distinction between the PHP (running on the server) and the client (which is where things like Javascript will run).
What you want is one of two methods: First, you can pre-populate all of the branches that are available for a given college, and on the client, have a script that is executed when a college is changed, which will change the set of branches presented. Note that this involves getting all of the branches for all of the colleges ahead of time.
Second, you could have a script which, when the college changes, queries the server for the branches that are supported and changes the branches presented. This way requires much less processing up front, and is usually referred to as an AJAX solution.
Note that both ways involve writing some Javascript that will run on the client in response to the college changing.
Upvotes: 1
Reputation: 909
I'll try to answer.
If you're willing to use JavaScript, then take a look at jQuery,
in particular,
http://api.jquery.com/change/
Otherwise to do it with PHP, as far as I know, you'd have to submit a form and refresh the page.
Which probably isn't what you want. You could use PHP/ajax/json,
but if you could do that you may as well use JavaScript directly.
jQuery should make it easy, there's probably even a dedicated plugin.
Upvotes: 0
Reputation: 7673
As a note, this is a better user-experience if done with AJAX. However, you need to set your form to submit onchange of your college select box. Then, in your PHP, you need to do:
if( isset( $_POST['coll1'] ) ) {
//query your branches and populate select same as you did for colleges.
//e.g. "SELECT * FROM Branches WHERE CollegeId=".mysql_real_escape_string($_POST['coll1']);
}
Is that enough info for you to work from?
Upvotes: 0