genext.brite Posted December 9, 2011 Share Posted December 9, 2011 Hi freaks, I'm new to php first of all. I'm dynamically binding a dropdownlist with mysql database . After the user selects an item from it , I want to match that item with another table so as to populate another database. The code I'm using to populate dropdown: <?php $con = mysql_connect("localhost","root",""); if(!$con) { die ('Can not connect to : '.mysql_error()); } mysql_select_db("ims",$con); $result=mysql_query("select cat_id,cat_name from category"); echo "<select name=cat>"; while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[cat_id]> $nt[cat_name] </option>"; } echo "</select>"; mysql_close($con); ?> Now after the user selects any one of the item , I want to bind another dropdown on the same page using such query like $result=mysql_query("select subcategory.sc_id,subactegory.sc_name from subcategory,category where subcategory.sc_id=$nt[cat_id]"); Please anyone tell me the logic and code to do it. Also tell me do I need an intermediate page to post the 1st dropdown value and then continue with 2nd dropdown. I couldn't figure out the concept anyhow. Help on this will be highly appreiable . (Tell me if I'm not clear with my question) Quote Link to comment https://forums.phpfreaks.com/topic/252828-fetching-selected-value-from-one-dropdown-to-populate-another-dropdown/ Share on other sites More sharing options...
ialsoagree Posted December 9, 2011 Share Posted December 9, 2011 Since HTTP is stateless (a non-constant connection) you'll either need to use multiple pages, run all the queries every time and use javascript to display the appropriate 2nd drop down, or use AJAX to populate a 2nd drop down box without refreshing. Of those options, the 1st is the easiest (and far more resource efficient than the 2nd), the 3rd is the hardest although debatebly the most convenient for the user (assuming the user even has AJAX enabled on their browser - most users will but not all). For the 1st option, you'll need to rewrite your page to check if an option has been selected and submitted (and if it has, populate the 2nd drop down box) or just proceed as normal (with a "next" button, so after they make their first choice, that choice is submitted and a 2nd drop down is populated on the next page). I hope that makes sense, I can provide a quick example if you need. Quote Link to comment https://forums.phpfreaks.com/topic/252828-fetching-selected-value-from-one-dropdown-to-populate-another-dropdown/#findComment-1296205 Share on other sites More sharing options...
genext.brite Posted December 9, 2011 Author Share Posted December 9, 2011 Thanks for ur reply.. yeah sure a quick example as u say will make it more clear. Quote Link to comment https://forums.phpfreaks.com/topic/252828-fetching-selected-value-from-one-dropdown-to-populate-another-dropdown/#findComment-1296212 Share on other sites More sharing options...
ialsoagree Posted December 9, 2011 Share Posted December 9, 2011 If you do the first method: $con = mysql_connect("localhost","root",""); if(!$con){ die ('Can not connect to : '.mysql_error());} mysql_select_db("ims",$con); if (!isset($_POST['cat'])) { $result=mysql_query("select cat_id,cat_name from category"); echo "<select name=cat>"; while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[cat_id]> $nt[cat_name] </option>"; } echo "</select> <input type="submit" name="Next >>" />"; } else { //Check to make sure $_POST['cat'] is valid BEFORE you run any MySQL query!!! //display their original choice, include code for the 2nd drop down box } mysql_close($con); Keep in mind that code that echoes HTML before the database is declared and after the database is closed will be displayed both in the 1st and 2nd page. Quote Link to comment https://forums.phpfreaks.com/topic/252828-fetching-selected-value-from-one-dropdown-to-populate-another-dropdown/#findComment-1296220 Share on other sites More sharing options...
genext.brite Posted December 9, 2011 Author Share Posted December 9, 2011 Thanks again for your help. In your example you used a button. But I want that on the selection of first dropdown list , the 2nd dropdown gets automatically populated. Is it possible?? In Asp. net we have a separate event to work on OnSelect event of dropdownlist. How can we do the same here. (Sorry I have lil knowledge of php.) I think you got my point. Quote Link to comment https://forums.phpfreaks.com/topic/252828-fetching-selected-value-from-one-dropdown-to-populate-another-dropdown/#findComment-1296346 Share on other sites More sharing options...
ddubs Posted December 9, 2011 Share Posted December 9, 2011 PHP is dynamic only on the server back end (before presented to the user). It doesnt do to well with "on-the-fly user-input oriented" dynamic. That is more JavaScript's domain. Quote Link to comment https://forums.phpfreaks.com/topic/252828-fetching-selected-value-from-one-dropdown-to-populate-another-dropdown/#findComment-1296359 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.