我看过很多地方,找不到我需要的一个很好的例子。我有一个按钮,提交时需要弹出一个表单。我过去使用input = hidden name这样做了。但是,我正在尝试使用if语句而不仅仅是变量。我会给你你的代码,希望你可以帮我找出我做错了什么。您可以看到我确实使用了隐藏名称,但我真的不知道如何将其合并到if语句中。
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>";
我非常感谢任何关于如何解决此代码而不是改进此问题的评论。
答案 0 :(得分:0)
您可以使用if
语句,如下所示。
<?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; ?>
通过这种方式,您不必使用那么多print
语句。
阅读php docs中的示例。
答案 1 :(得分:0)
使用数组而不是if语句。如果您希望将来添加更多选项,这也会有所帮助。此外,不是逐行输出HTML代码,而是尝试使用一个echo语句。
如果要从POST检索状态值,请先使用$status = $_POST['status'];
写$row[status]
时要小心,因为你使用的是常量。我没有对其进行修改,但如果您想要$row[$status]
,请在$
之前添加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>
';