I have looked in quite a few places and cannot find the a really good example of what I need. I have a button that when submitted needs to pop up a form. I have done this in the past using the input=hidden name. However, I am using trying to use if statements instead of just a variable. I will give you the code that I have in hopes that you can help find out what exactly I am doing wrong. You can see that I am indeed using the hidden name for some but I really don't know how to incorporate that into the if statements.
if($row[status] == 0) { print "<input type='image' src='images/rework-ticket.png' alt='Rework' value='Rework' name='button' style='height:50px; width:50px; '>"; }
elseif($row[status] == "Rework (In Progress)") { print "<img src='images/rework-ticket.png' alt='Rework' style='height:50px; width:50px; '>"; }
print "<td align='center'>";
print "<form method='post' action='test2.php'>";
print "<input type='image' src='images/rework-ticket.png' alt='Add Rework Ticket' value='Enter Employee ID' name='button' style='height:50px; width:50px; '>";
print "<input type='hidden' name='proceed_to_rework' value='true'>";
print "<input type='hidden' name='invoice_number' value='$row[invoice_number]'>";
print "<input type='hidden' name='last_name' value='$row[last_name]'>";
print "<input type='hidden' name='status' value='$status'>";
print "Status:";
print "<td><select name='status' size='1'>";
if($status != NULL) { print "<option value='$status'>$status</option>"; }
if($status != "Parts Prep (In Progress)") { echo "<option value='Parts Prep (In Progress)'>Parts Prep (In Progress)</option>"; }
if($status != "Parts Prep (Complete)") { echo "<option value='Parts Prep (Complete)'>Parts Prep (Complete)</option>"; }
if($status != "Assembly (In Progress)") { echo "<option value='Assembly (In Progress)'>Assembly (In Progress)</option>"; }
if($status != "Assembly (Complete)") { echo "<option value='Assembly (Complete)'>Assembly (Complete)</option>"; }
if($status != "Finish (In Progress)") { echo "<option value='Finish (In Progress)'>Finish (In Progress)</option>"; }
if($status != "Finish (Complete)") { echo "<option value='Finish (Complete)'>Finish (Complete)</option>"; }
if($status != "Plumbing (In Progress)") { echo "<option value='Plumbing (In Progress)'>Plumbing (In Progress)</option>"; }
if($status != "Plumbing (Complete)") { echo "<option value='Plumbing (Complete)'>Plumbing (Complete)</option>"; }
print "</form>";
print "</td>";
I really appreciate any comments on how I can not only fix this code but improve this question.
You can use the if
statement as follows.
<?php if (condition): ?>
<!-- HTML here -->
<?php if (anotherCondition): ?>
<!-- HTML here -->
<?php endif; ?>
<!-- HTML again -->
<?php if (anotherCondition): ?>
<!-- HTML here -->
<?php endif; ?>
<!-- HTML again -->
<?php endif; ?>
With this way, you don't have to use that much print
statements.
Read the examples in php docs.
Instead of the if statements, use an array. This will also help if you want to add more options in the future. Also, instead of outputing HTML code line by line, try using one single echo statement.
If you want to retrieve the status value from POST, then use $status = $_POST['status'];
before everything else
Be careful when writing $row[status]
because you are using a constant. I have not modified it, but if you wanted $row[$status]
instead, then add the $
before status
.
if($row[status] == 0) {
echo '
<button type="button" id="Rework" >
<img src="images/rework-ticket.png" alt="Rework" style="height:50px; width:50px; "></img>
</button>';
} elseif($row[status] == "Rework (In Progress)") {
echo '
<button type="button" id="Rework" >
<img src="images/rework-ticket.png" alt="Rework" style="height:50px; width:50px; "></img>
</button>';
}
echo "
<td align='center'>
<form method='post' action='test2.php'>
<input type='image' src='images/rework-ticket.png' alt='Add Rework Ticket' value='Enter Employee ID' name='button' style='height:50px; width:50px; '>
<input type='hidden' name='proceed_to_rework' value='true'>
<input type='hidden' name='invoice_number' value='{$row[invoice_number]}'>
<input type='hidden' name='last_name' value='{$row[last_name]}'>
<input type='hidden' name='status' value='{$status}'>
Status:
<td><select name='status' size='1'>
";
$statusOptions = array(
NULL,
"Parts Prep (In Progress)",
"Parts Prep (Complete)",
"Assembly (In Progress)",
"Assembly (Complete)",
"Finish (In Progress)",
"Finish (Complete)",
"Plumbing (In Progress)",
"Plumbing (Complete)"
);
foreach($statusOptions as $option){
if($status != $option){
echo "<option value='{$option}'>{$option}</option>";
}
}
echo '</select>';
echo "</form>";
echo "</td>";
echo '
<script type="text/javascript">
function showDocument(){
document.getElementById("ReworkForm").style.visibility="visible";
}
window.onload = function(){
document.getElementById("ReworkForm").style.visibility="hidden";
document.getElementById("Rework").addEventListener("click", showDocument);
};
</script>
';