drop down menus and checkboxes | Member | | Join Date: Jun 2009
Posts: 52
| |
I currently have an array that looks like this -
array(
-
[testA]=>array(
-
[0]=>phase1,
-
[1]=>phase2,
-
[2]=>phase3
-
),
-
[testB]=>array(
-
[0]=>phase4,
-
[1]=>phase5
-
)
-
)
-
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.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,748
| | | 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
| | | 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.
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | re: drop down menus and checkboxes Quote:
Originally Posted by phpnewbie26 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. -
-
<?php
-
-
function getTests($array){
-
return array_keys($array);
-
}
-
-
function getPhases($array) {
-
$phaseArr = array();
-
foreach ($array as $test) {
-
foreach ($test as $phase) {
-
$phaseArr[$phase] = $phase;
-
}
-
}
-
return $phaseArr;
-
}
-
-
function getSelectedPhasesForTest($array,$selectedTest) {
-
// see if you can write this one. challenge yourself and comeback with questions.
-
}
-
-
-
$tests = array();
-
$phases = array();
-
$array = array('testA' =>array('phase1','phase2','phase3'),
-
'testB' =>array('phase4','phase5','phase2'));
-
-
if(isset($_POST['displayTests'])) $tests = getTests($array);
-
if(isset($_POST['displayPhases'])) $phases = getPhases($array);
-
-
?>
-
<html>
-
<body>
-
-
<form name="myForm" action="" method="post">
-
<input type="checkbox" name="displayTests" id="displayTests" value="1" />Display Tests<br /><br />
-
<input type="checkbox" name="displayPhases" id="displayPhases" value="1" />Display Phases<br /><br />
-
-
<select name="tests">
-
<?php foreach ($tests AS $test) echo "<option value='$test'>$test</option>"; ?>
-
</select>
-
<br /><br />
-
-
<select name="phases">
-
<?php foreach ($phases AS $phase) echo "<option value='$phase'>$phase</option>"; ?>
-
</select>
-
<br /><br />
-
-
<input type="submit">
-
-
</form>
-
</body>
-
-
Your Welcome,
Dan
| | 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. -
function getSelectedPhasesForTest($array,$selectedTest) {
-
$i=0;
-
$phases[]=$array[$selectedTest][$i];
-
$i++;
-
}
-
What I did was just put the phases from the selected test in the phases array.
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | 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
| | | 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?
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | re: drop down menus and checkboxes Quote:
Originally Posted by phpnewbie26 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
| | | 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?
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,095
| | | 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|