2016-12-30 68 views
0

我已经查看了很多地方,找不到我需要的一个很好的示例。我有一个按钮,提交时需要弹出一个表单。我在过去使用input = hidden name完成了这个操作。但是,我正在尝试使用if语句而不仅仅是一个变量。我会给你我所希望的代码,希望你能帮我找出我做错了什么。你可以看到我确实使用了一些隐藏的名字,但是我真的不知道如何将这些名称合并到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

您可以使用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中的示例。

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> 
    '; 
+0

这真的有所帮助。但是我仍然需要能够隐藏statusOptions,直到按下返工按钮之后才需要看到它。有没有办法做到这一点? – Case

+0

也许你需要一点javascript。我已经通过添加一个按钮来修改答案(在第一部分中您有$ row [status])并在最后加入了JavaScript代码。 – RelaxedArcher