Connecting Tech Pros Worldwide Help | Site Map

drop down menus and checkboxes

  #1  
Old June 22nd, 2009, 09:13 PM
Member
 
Join Date: Jun 2009
Posts: 52
I currently have an array that looks like this
Expand|Select|Wrap|Line Numbers
  1. array(
  2.       [testA]=>array(
  3.                     [0]=>phase1,
  4.                     [1]=>phase2,
  5.                     [2]=>phase3
  6.                     ),
  7.       [testB]=>array(
  8.                     [0]=>phase4,
  9.                     [1]=>phase5
  10.                    )
  11.         )
  12.  
I currently have two checkboxes(test and phase). What I am trying to do is if a checkbox is checked, a drop down menu will appear with the appropriate information. For example, if the test checkbox was selected, testA and testB would appear in menu. if the user also checks off the phase box, then the phases of the selected test will appear in another drop down menu. However, if the user only selects the phase checkbox, then all five phases will appear in the drop down menu. Anyone have any idea how to go about this? Thanks in advance.
  #2  
Old June 22nd, 2009, 09:38 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: drop down menus and checkboxes


Hi.

There are two ways to do something like that. (Two usable ways, anyways).

#1 Use AJAX.
Set up your HTML form, and have AJAX pull the data from a PHP file and update the form using JavaScript.

Google AJAX if you need to find out how to use it. Loads of material on that available.

#2 Submit the form and have PHP update it.
This is easier to set up, but a lot less appealing to the user.

You would have to have the form submit itself every time the user changes something that requires that the form be updated, and have PHP re-create the HTML page based on what is selected.
  #3  
Old June 22nd, 2009, 09:42 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: drop down menus and checkboxes


Since I have no experience with Java or AJAX, i think I'll go with the second way. Do you mind giving me a few lines of sample code to help me along? Thanks in advance.
  #4  
Old June 23rd, 2009, 09:06 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

re: drop down menus and checkboxes


Quote:
Originally Posted by phpnewbie26 View Post
Since I have no experience with Java or AJAX, i think I'll go with the second way. Do you mind giving me a few lines of sample code to help me along? Thanks in advance.
Here you go, this should get you started.

No comments on purpose (you must learn how it works), I left an assignment for you to do in it as well, try to do it and post it back here.


Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. function getTests($array){
  5.     return array_keys($array); 
  6. }
  7.  
  8. function getPhases($array) {
  9.     $phaseArr = array(); 
  10.     foreach ($array as $test) {
  11.         foreach ($test as $phase) {
  12.             $phaseArr[$phase] = $phase; 
  13.         }        
  14.     }
  15.     return $phaseArr;
  16. }
  17.  
  18. function getSelectedPhasesForTest($array,$selectedTest) {
  19.     // see if you can write this one. challenge yourself and comeback with questions. 
  20. }
  21.  
  22.  
  23. $tests = array(); 
  24. $phases = array(); 
  25. $array =  array('testA' =>array('phase1','phase2','phase3'),
  26.                 'testB' =>array('phase4','phase5','phase2'));
  27.  
  28. if(isset($_POST['displayTests']))  $tests = getTests($array); 
  29. if(isset($_POST['displayPhases']))  $phases = getPhases($array); 
  30.  
  31. ?>
  32. <html>
  33. <body>
  34.  
  35. <form name="myForm" action="" method="post">
  36. <input type="checkbox" name="displayTests" id="displayTests" value="1" />Display Tests<br /><br />
  37. <input type="checkbox" name="displayPhases" id="displayPhases" value="1" />Display Phases<br /><br />
  38.  
  39. <select name="tests">
  40. <?php foreach ($tests AS $test) echo "<option value='$test'>$test</option>"; ?>
  41. </select>
  42. <br /><br />
  43.  
  44. <select name="phases">
  45. <?php foreach ($phases AS $phase) echo "<option value='$phase'>$phase</option>"; ?>
  46. </select>
  47. <br /><br />
  48.  
  49. <input type="submit">
  50.  
  51. </form> 
  52. </body>
  53.  
  54.  
Your Welcome,




Dan
  #5  
Old June 23rd, 2009, 09:33 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: drop down menus and checkboxes


thanks for helping me out. Okay..so this is what I have so far for that function. I'm not really sure what this function really does and where it ultimately goes cause I couldn't find it in the code, but here it is.

Expand|Select|Wrap|Line Numbers
  1. function getSelectedPhasesForTest($array,$selectedTest) {
  2.  $i=0;
  3.  $phases[]=$array[$selectedTest][$i];
  4.  $i++;
  5. }
  6.  
What I did was just put the phases from the selected test in the phases array.
  #6  
Old June 23rd, 2009, 09:53 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

re: drop down menus and checkboxes


well your code is wrong either way ($i doesn't do anything), but I should have named it getPhasesForSelectedTest(). It's not used anywhere.

As the name suggest, it should be used when you want to select a test case then display the phases that are in that test only in the drop down box, not just display all the phases for all tests.

User clicks the display test -> tests are displayed
user clicks on a test -> the phases for that selected test are displayed

It's no used anywhere, but see if you can use this function to do the above scenario.

hint: select form element has an onChange() that allows you to "do" something when something is selected (changed), in this case, submit the form. =)


Dan
  #7  
Old June 24th, 2009, 02:34 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: drop down menus and checkboxes


so something like adding onChange="getPhasesForSelectedTest()" in the select form for tests or do I put submit()? Would the my code above be correct if I put the $i=0 outside of the function? Wouldn't it then loop through all of the phases in the array of the selected test and put it into the phases array which is used in printing the drop down menu?
  #8  
Old June 24th, 2009, 04:39 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

re: drop down menus and checkboxes


Quote:
Originally Posted by phpnewbie26 View Post
so something like adding onChange="getPhasesForSelectedTest()" in the select form for tests or do I put submit()? Would the my code above be correct if I put the $i=0 outside of the function? Wouldn't it then loop through all of the phases in the array of the selected test and put it into the phases array which is used in printing the drop down menu?
You would have to submit the form because the HTML is processed at the client's browser, long after PHP has finished its processing.

Just having the variable $i doesn't make it a loop. There are two times of for loops in PHP (the usually for() and php version the foreach() loop). You don't need either to complete that function. You will need to know how arrays work, specifically array hashes that have keys and values. Because in fact, you're sample array has keys (tests) and values (array of phases)




Dan
  #9  
Old June 24th, 2009, 06:03 PM
Member
 
Join Date: Jun 2009
Posts: 52

re: drop down menus and checkboxes


wow..i didn't even notice that I didn't have a loop there..i don't noe what i was thinking..i would have thought to use a for loop. Can I use something along the lines of $arrayphases=array_values($array[$selectedTest]) and return that array?
  #10  
Old June 24th, 2009, 09:58 PM
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,075

re: drop down menus and checkboxes


Partial answer. Just doing "return $array[$selectedTest]" would work. The test is the "key" in your case. When you call an array and pass a key to its brackets, it will get whatever value is stored for that key/position, thus it will return the phases for that test.

=)

Happy coding,



Dan
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to: Select from Dynamic drop down menus using script rpeterson84 answers 2 August 20th, 2007 10:48 AM
Selling Tickets for a School Production mcnaryxc answers 3 February 21st, 2007 09:12 AM
SIMPLE Javascript Calculator firstcustomer@gmail.com answers 24 July 13th, 2006 06:15 PM
Need general purpose javascript code to hide/display entry fields on a form Pat Scott answers 2 July 23rd, 2005 12:10 PM