2016-12-30 108 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