DropDownBox Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

alexmaster_2004
Forum Commoner
Posts: 35
Joined: Wed Sep 14, 2005 8:44 am

DropDownBox Problem

Post by alexmaster_2004 »

Hi there
i was doing some improvments in my site.
then i have faced a problem that i need you to help me solve it.
the problem is:
i was have a web page that i use to add products
under the existing categories.
i have updated the categories section and now want to upgrade this web page.
now i use 2 dropdown box to choose the main categoery and the sub category.
the problem that when i change the selected item on the main category dropdown box i want also to change the items on the sub category dropdown box. my problem is that i don't know how to do this thing.
i want anyone to tell me how i can make my code retreive the sub categories depending on the selected value of the main category dropdown box

Thanks in Advance
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

have a look at thisarticle
if you can not understand anything, then I would help you further.
if you want further help. list your main categories and subcategories from main categories.
if they are coming from the database, we do not need the list.
alexmaster_2004
Forum Commoner
Posts: 35
Joined: Wed Sep 14, 2005 8:44 am

Post by alexmaster_2004 »

Hi
i have reviewed it.
but i don't want to use javascript.
Please if you can give me another solution it will be very nice.
Thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

submit the form you can after the box is selected. then populate the second box you will.

ex (untested):

Code: Select all

$firstresult = mysql_query("select * from categoryone")
  or die(mysql_error());

echo "<form name=\"MyForm\" method=\"post\">
echo "<select name=\"categoryone\" onChange=\"document.MyForm.submit()\">";
echo "<option value=\"\"></option>";
while($firstrow = mysql_fetch_assoc($firstresult))
{
  echo "<option value=\"".$firstrow['id']."\"".(isset($_POST['categoryone'] && $_POST['categoryone'] == $firstrow['id'] ? " selected=\"selected\"" : "").">".$firstrow['value']."</option>";
}
echo "</select>";

echo "<br>";

echo "<select name=\"categorytwo\">";
echo "<option value=\"\"></option>";
if(isset($_POST['categoryone']))
{
  $secondresult = mysql_query("select * from categorytwo where categoryone = ".$_POST['categoryone'])
     or die(mysql_error());
  while($secondrow = mysql_fetch_assoc($secondresult))
  {
    echo "<option value=\"".$secondrow['id']."\">".$secondrow['value']."</option>";
  }
}
echo "</select>";
echo "</form>";
^^ not tested this is, but get you goign it should...
alexmaster_2004
Forum Commoner
Posts: 35
Joined: Wed Sep 14, 2005 8:44 am

Post by alexmaster_2004 »

Hi
i want to thank you for your help.
but i have one more problem:
before these changes i was use a button to redirect the form to another one that retreive
the values of the 2 dropdownbox and also some other controls and use them to update my database.
now i can't redirect it to that page because it goes to itself.
i will be happy if you can tell me what should i do to make it work as before.
Many Thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

conditionally check all of your post data on the same page you could. Or redirect, conditionally, to another action page if both selects are "selected" you could.
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

On displaying the second textbox, you can change the form to submit to the second page.

for the initial run it is

Code: Select all

<form action='thisPage'>
then when it is submitted, make it post to the other page.

Code: Select all

<form action='theOtherPage'>
Should do the trick.
alexmaster_2004
Forum Commoner
Posts: 35
Joined: Wed Sep 14, 2005 8:44 am

Post by alexmaster_2004 »

I'm sorry for my lot questions.
Can you explain more.
Thanks
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

you said the form now submits to itself. so after the submission, make it so that the form submits to the other page.

Code: Select all

<?
if(isset($_POST['submitFirstBox']))
{
   // get the data for second dropdown box here
   ***;
 

   $action = 'theOtherPage';
}
else
{
   $action = 'thisPage';
}
?>

<form action='<? php echo $action; ?>' method='post'>
blah blah blah
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Refer to CoderGloblin's tutorial post:
viewtopic.php?t=29084
alexmaster_2004
Forum Commoner
Posts: 35
Joined: Wed Sep 14, 2005 8:44 am

Post by alexmaster_2004 »

Hi
I have tried this solution but it didn't do what i want.
i will explain my my problem in details and hope that anyone can help me:
i use that form to enter the new products for my web site.
in order to enter the product i prompet the user to choose the category which
he want to put his product under it.
i use 5 DropDownBoxs for this process.
the user choose the main category then he can choose the sub categories
depending on his choice for the previous category.
Exactly he first choose the main category. this fill up the first sub-category.
if he selects a sub category from them then,this fill up the second sub category.
and so on till he choose the specific category that he want to put his product under it.
then he enter the other information about that product(Name,Price,etc...).
after he finish entering all required data he click on the submit button.
this button redirect to another page.this page is responsible for the data processing.
it get all the information from the main page and insert them into the database.

Now i can handel the fill up operation for the DropDownBoxs.But this prevent me from
redirect to the other page.

after i have tested all solutions that you have posted here all.
i still have this problem.

i have get an idea and wonder if it is possible.
the idea is to check for the control that have submitted the form.
and if it is one of the DropDownBoxs then submit the form to itself.
else redirect to the other form.

i don't know if it is possible to do this or not.
PLEASE if this is possible can anyone help me and tell me how to
implement this idea.

Thanks in Advance
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

i don't quite understand what you want. are all the drop down boxes available all at once or are they generated one by one after the user submits the form?
alexmaster_2004
Forum Commoner
Posts: 35
Joined: Wed Sep 14, 2005 8:44 am

Post by alexmaster_2004 »

Hi
i was thinking of generating them one by one but i didn't know how to make this?
so i made them all available at once.
if you ccan tell me how to generate then one by one i will be very happy
Thanks
alexmaster_2004
Forum Commoner
Posts: 35
Joined: Wed Sep 14, 2005 8:44 am

Post by alexmaster_2004 »

Hi
i hope that you can help me on that.
i'm ready to use any solution for this problem
just please tell me how to solve it
Thanks
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

burritos posted the solution to generating the boxes one by one already:

Code: Select all

$firstresult = mysql_query("select * from categoryone")
  or die(mysql_error());

echo "<form name=\"MyForm\" method=\"post\">";
echo "<select name="categoryone\" onChange=\"document.MyForm.submit()\">";
echo "<option value=\"\"></option>";
while($firstrow = mysql_fetch_assoc($firstresult))
{
  echo "<option value=\"".$firstrow['id']."\"".(isset($_POST['categoryone'] && $_POST['categoryone'] == $firstrow['id'] ? " selected=\"selected\"" : "").">".$firstrow['value']."</option>";
}
echo "</select>";

echo "<br>";

echo "<select name=\"categorytwo\">";
echo "<option value=\"\"></option>";
if(isset($_POST['categoryone']))
{
  $secondresult = mysql_query("select * from categorytwo where categoryone = ".$_POST['categoryone'])
     or die(mysql_error());
  while($secondrow = mysql_fetch_assoc($secondresult))
  {
    echo "<option value=\"".$secondrow['id']."\">".$secondrow['value']."</option>";
  }
}
echo "</select>";
echo "</form>";
now since you also need to have it submit to other form when all the data are ready, you cannot put a static page in the form action field. so let's modify burritos code to make the action a variable, and add the code to branch the submission

Code: Select all

$firstresult = mysql_query("select * from categoryone")
  or die(mysql_error());

// choose which page to go, determined by having categoryone selected
if(isset($_POST['categoryone']))
{
   $myForm = 'nextPage.php';
}
else
{
  $myForm = 'thisPage.php';
}

echo "<form name=\"$myForm\" method=\"post\">"; // NOTE HERE, we changed MyForm to $myForm, a variable.
echo "<select name="categoryone\" onChange=\"document.MyForm.submit()\">";
echo "<option value=\"\"></option>";
while($firstrow = mysql_fetch_assoc($firstresult))
{
  echo "<option value=\"".$firstrow['id']."\"".(isset($_POST['categoryone'] && $_POST['categoryone'] == $firstrow['id'] ? " selected=\"selected\"" : "").">".$firstrow['value']."</option>";
}
echo "</select>";

echo "<br>";

echo "<select name=\"categorytwo\">";
echo "<option value=\"\"></option>";
if(isset($_POST['categoryone']))
{
  $secondresult = mysql_query("select * from categorytwo where categoryone = ".$_POST['categoryone'])
     or die(mysql_error());
  while($secondrow = mysql_fetch_assoc($secondresult))
  {
    echo "<option value=\"".$secondrow['id']."\">".$secondrow['value']."</option>";
  }
}
echo "</select>";
echo "</form>";
Post Reply