I have a page which is split into 3 parts with 2 submit buttons:
Part 1: Contains a module drop down menu. User selects option from drop down menu and submits. Everytime it is submitted, it changes part 2 and part 3 is hidden. After submission drop down menu goes back to Please Select option.
Part 2: Contains Assessment drop down menu, only appears after user has submitted part 1. When this part is submitted by user selecting option from Assessment drop down menu and clicking on submit button, drop down menu goes back to Please Select and part 3 appears. Part 3 changes everytime part 2 is submitted with different options.
Part 3: Display details from part 2. Only appears and changes depending on Assessment chosen and submitted from part 2. If user submits from part 1, then this part is hidden.
My question is what is the right structure in order to be able to match what I stated above. What I mean is that where does the if statements go, what goes in the if statements and when do I close the if statements?
Below is my code, problem is that if I click on submit button in part 2, instead of display part 3, it just removes parts 2 and just shows part 3 where part 2 is, so I believe my page structure is incorrect. I commented where each part is.
//PART 1
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Module:
<select name="modules" id="modulesDrop">
<option value="">Please Select</option>
<option value="1">Art</option>
<option value="2">ICT</option>
<option value="3">Business</option>
</select>
</th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit Module" name="moduleSubmit" /></p>
</form>
<?php
if (isset($_POST['moduleSubmit']) || isset($_POST['sessionSubmit'])){
//PART 2
if($_POST['modules'] == ''){
$pHTML = "<span style='color: red'>Please Select a Module</span>";
}else if($sqlnum == 0){
$pHTML = "<span style='color: red'>Sorry, You have No Assessments under this Module</span>";
} else{
$sessionHTML = '';
$sessionHTML = '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$sessionHTML .= '<option value="one">AAA</option>'.PHP_EOL;
$sessionHTML .= '<option value="two">AAB</option>'.PHP_EOL;
$sessionHTML .= '<option value="three">AAC</option>'.PHP_EOL;
$sessionHTML .= '</select>';
$assessmentform = "
<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post' id='assessmentForm'>
<p id='warnings'>{$pHTML}</p>
<p><strong>Assessments:</strong> {$sessionHTML} </p>
<p><input id='sessionSubmit' type='submit' value='View Assessment Details' name='sessionSubmit' /></p>
</form>";
echo $assessmentform;
}
}
if (isset($_POST['sessionSubmit'])) {
//PART 3
if($_POST['session'] == ''){
$sHTML = "<span style='color: red'>Please Select a Session</span>";
} else{
$sessiondetails ="
<p id='warnings'>{$sHTML}</p>
<p>You have selected an Assessment where the details will be displayed here:</p>
";
echo $sessiondetails;
}
}
You really need to separate data logic (i.e. what you’re asking your PHP to do), and the rendering (what you’re asking your HTML to do). I’d open your script with PHP functionality, determine what you want to show, and put your HTML into separate phtml files.
I’d also include a hidden field, indicating what step the user is currently on. This would allow you to do something like: