Jump to content

Fetching selected value from one dropdown to populate another dropdown


genext.brite

Recommended Posts

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.