Connecting Tech Pros Worldwide Forums | Help | Site Map

drop down menus and checkboxes

Member
 
Join Date: Jun 2009
Posts: 52
#1: Jun 22 '09
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.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#2: Jun 22 '09

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.
Member
 
Join Date: Jun 2009
Posts: 52
#3: Jun 22 '09

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.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#4: Jun 23 '09

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
Member
 
Join Date: Jun 2009
Posts: 52
#5: Jun 23 '09

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.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#6: Jun 23 '09

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
Member
 
Join Date: Jun 2009
Posts: 52
#7: Jun 24 '09

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?
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#8: Jun 24 '09

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
Member
 
Join Date: Jun 2009
Posts: 52
#9: Jun 24 '09

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?
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#10: Jun 24 '09

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