943,645 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1188
  • PHP RSS
Sep 27th, 2009
0

chained select boxes

Expand Post »
i have tree select boxes. i fill those with data coming from a database (mysql)
i want to change the data of the last two selects regarding the selection of the first.
here is my code. it doesnt work (i used a little bit of javascript)

in head
JavaScript Syntax (Toggle Plain Text)
  1. <SCRIPT language=JavaScript>
  2. function reload(form)
  3. {
  4. var val=form.cat.options[form.cat.options.selectedIndex].value;
  5. self.location='apousies.php?cat=' + val ;
  6. }</script>

in body

php Syntax (Toggle Plain Text)
  1. //connect with database works fine
  2. @$cat=$_GET['cat'];
  3. @$cat=$HTTP_GET_VARS['cat'];
  4.  
  5. ///////// Getting the data from Mysql table for first list box//////////
  6. $quer2=mysql_query("SELECT DISTINCT eidikotita, id_eidikotitas FROM eidikotita order by eidikotita");
  7. ///////////// End of query for first list box////////////
  8.  
  9.  
  10. if(isset($cat) and strlen($cat) > 0){
  11. //it never gets here
  12. $quer=mysql_query("SELECT lastname, firstname, fathersname FROM members where id_eid=$cat order by lastname");
  13. $quer1=mysql_query("SELECT mathima FROM mathimata where id_eid=$cat order by mathima");
  14. }else{$quer=mysql_query("SELECT lastname, firstname, fathersname FROM members order by lastname");
  15. $quer1=mysql_query("SELECT mathima FROM mathimata order by mathima"); }
  16.  
  17.  
  18. echo "<form method=post name=f1 action='apousies-ins.php'>";
  19.  
  20. ////////// Starting of first drop downlist /////////
  21. echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>lessons</option>";
  22. while($noticia2 = mysql_fetch_array($quer2)) {
  23. if($noticia2['id_eidikotitas']==@$cat){echo "<option selected value='$noticia2[id_eidikotitas]'>$noticia2[eidikotita]</option>"."<BR>";}
  24. else{echo "<option value='$noticia2[id_eidikotitas]'>$noticia2[eidikotita]</option>";}
  25. }
  26. echo "</select>";
  27. ////////////////// This will end the first drop down list ///////////
  28.  
  29. ////////// Starting of second drop downlist /////////
  30. echo "<select name='subcat'><option value=''>Êáôáñôéæüìåíïò</option>";
  31. while($noticia = mysql_fetch_array($quer)) {
  32. echo "<option value='$noticia[lastname], $noticia[firstname], $noticia[fathersname]'>$noticia[lastname], $noticia[firstname], $noticia[fathersname] </option>";
  33. }
  34. echo "</select>";
  35. ////////////////// This will end the second drop down list ///////////
  36.  
  37. ////third list
  38.  
  39. echo "<select name='subcat1'><option value=''>ÌÜèçìá</option>";
  40. while($noticia = mysql_fetch_array($quer1)) {
  41. echo "<option value='$noticia[mathima]'>$noticia[mathima] </option>";
  42. }
  43. echo "</select>";
  44. ////////////////// This will end the third drop down list ///////////
  45.  
  46. echo "</form>";

the $cat variable seems empty.

i know i may ask a lot. if it is not possible to check this code could u advice me on how can i solve this problem? (the content of one select box is geting data regarding the selection of a first select box)
Last edited by peter_budo; Sep 28th, 2009 at 2:41 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dourvas is offline Offline
20 posts
since Dec 2008
Sep 27th, 2009
0

Re: chained select boxes

If $cat seems to be empty on that page, go back the page where you originally defined $cat. Since you are passing the variable from another page it would seem to me that you are merely passing an empty variable. What does the code look like where you originally define $cat?
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009
Sep 27th, 2009
0

Re: chained select boxes

Sorry, wasn't paying attention and just realized that $cat was coming from the same page. This snippet looks a little suspicious to me ... <option value=''>lessons</option> .
Last edited by peter_budo; Sep 28th, 2009 at 2:42 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009
Sep 28th, 2009
0

Re: chained select boxes

it does not work even if i put this line out.

when i pick something from first select box the form reloads. in the adress line shows my pick (ex. http//:......./bathmoi.php?cat=11, but the select box does not keep my selection and the second and third select boxes does not contain the data they should regarding my pick

the variable cat is still empty. thats why. should it be empty? someone please....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dourvas is offline Offline
20 posts
since Dec 2008
Sep 28th, 2009
0

Re: chained select boxes

I wouldn't omit that line, it may be correct I just never seen it done that way. I would typically use <option value="lessons">lessons</option> or simply <option>lessons</option> if you don't need to assign a value. Have you tried to echo $cat to be sure it is empty? What I would do to make sure a selection reappears upon submitting the form would be something like <option selected="selected"><?=$cat?></option> <option>lessons</option> . That would ensure your select box holds your selection given $cat is not empty, and it does not appear to me that it would be.
Last edited by peter_budo; Sep 28th, 2009 at 2:43 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009
Sep 28th, 2009
0

Re: chained select boxes

Does this just "reload" the form, or does it actually submit it? <select name='cat' onchange=\"reload(this.form)\"> If it is simply reloading it $cat will not contain a value.
Last edited by peter_budo; Sep 28th, 2009 at 2:43 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009
Sep 29th, 2009
0

Re: chained select boxes

of course i echoed the variable. it is empty for sure!

it never gets in here
PHP Syntax (Toggle Plain Text)
  1.  
  2. if(isset($cat) and strlen($cat) > 0){
  3. //never in here!!!!!
  4. $quer=mysql_query("SELECT lastname, firstname, fathersname FROM members where id_eid=$cat order by lastname");
  5. $quer1=mysql_query("SELECT mathima FROM mathimata where id_eid=$cat order by mathima");
  6. }else{$quer=mysql_query("SELECT lastname, firstname, fathersname FROM members order by lastname");
  7. $quer1=mysql_query("SELECT mathima FROM mathimata order by mathima"); }

i think u r right about the reload issue. it probably just reloads the form without submiting it. i did not know how could i submit the form by just picking somenthing from a select box. (it would be easy for me if i could place a submit button) how could i do that?

A! an issue is that right now when i pick something from the first select box - the form reloads, and in the adress line the $cat has value!!!
adress line after a random pick:
http://iek-kozan.koz.sch.gr/admin/ba...moi.php?cat=14

remenber the reload function
PHP Syntax (Toggle Plain Text)
  1. <SCRIPT language=JavaScript>
  2. function reload(form)
  3. {
  4. var val=form.cat.options[form.cat.options.selectedIndex].value;
  5. self.location='bathmoi.php?cat=' + val ;
  6. }</script>

but my echoes still remains empty. i am confused. what shoul i do???
Last edited by dourvas; Sep 29th, 2009 at 4:05 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dourvas is offline Offline
20 posts
since Dec 2008
Sep 29th, 2009
0

Re: chained select boxes

If I'm understanding correctly and you want to use a submit button, that is easy. For the code you are working with it would look something like
 echo '<input type="submit" name="button_name" id="button_id" value="Submit" />'; 
echo '</form>';
As far $cat showing up in the URL, I would think that $_GET['cat']; should do what you want. I've used '@' to suppress errors before, but I never seen it used in front of a variable. Is there are a reason you are doing it that way?
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009
Sep 29th, 2009
0

Re: chained select boxes

One other thing you may want to adjust is that you are using POST as your form method, and using GET to set your variable. One or the other should be changed so they are the same.
Reputation Points: 21
Solved Threads: 31
Posting Pro in Training
CFROG is offline Offline
405 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Dehasher script malfunctioning
Next Thread in PHP Forum Timeline: What is $form<<<POST





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC