簡體   English   中英

如何回顯輸入類型的文本?

[英]How do I echo the input type text?

我想回顯: input type="text" name="aantal" 因此,當您單擊提交按鈕時,它將顯示用戶輸入的相冊數量。

以及如何在輸入區域中保持相同的數字,以便當用戶輸入數字並單擊“提交”按鈕時,數字將保留在那里並且不會重置為0? 如您所見,我設法使選中的復選框保持選中狀態。

<!DOCTYPE html>
  <html lang="nl">
    <head>
      <meta http-equiv="Content-Type"
            content="text/html;
            charset=UTF-8" />
      <title>Mijn Muziek</title>
   </head>
    <body>
      <!-- shoppingcart starts here -->
       <table border=0 cellpadding=0 cellspacing=0 width=100%>
        <form name="order"
              action="lab07.php"
              method="POST">
         <tr>
          <td>
           <img src="images/evora.jpg" width="100px" alt="X" />
          </td>
         </tr>
         <tr>
          <td>
           Cesaria Evora "Em Um Concerto" Track:10 Prijs: 9.99
          </td>
         </tr>
         <tr>
          <td>
           <input type="hidden" name="albumcode[0]"
              value="001" />
           <input type="hidden" name="artiest[0]"
                  value="Cesaria Evora" />
           <input type="hidden" name="titel[0]"
                  value="Em Um Concerto" />
           <input type="hidden" name="tracks[0]"
                  value="10" />
           <input type="hidden" name="prijs[0]"
                  value="9.99" />
           <input type="hidden" name="genre[0]"
                  value="World" />

           Aantal: <input type="text" size=2 maxlength=3
                          name="aantal" value="0"
                          style="background-color:#f8ce6c" />

           <hr />
          </td>
         </tr>

         <tr>
          <td>Korting:<br />
          <input type="checkbox" name="student" id="student"
             value="15" <?php if(isset($_POST['student'])) echo 
 "checked='checked'"; ?> />
                 Student 15%<br />
          <input type="checkbox" name="senior" id="senior"
                 value="10" <?php if(isset($_POST['senior'])) echo 
"checked='checked'"; ?> />
                 Senior 10%<br />
          <input type="checkbox" name="klant" id="klant"
             value="5" <?php if(isset($_POST['klant'])) echo 
"checked='checked'"; ?> />
                 Klant 5%<br />
      <hr />
      </td>
     </tr>
     <tr>
       <td>
        <input type="submit" width="300px" name="submit"
              value="      Bestellen      " />
        <hr />
       </td>
      </tr>
    </form>
   </table>
  <!-- Shoppingcart ends here-->
  <?php

    echo isset($_POST['aantal']);

    $korting = 0;
      if( isset($_POST["student"]) ) $korting = $korting + 15;
      if( isset($_POST["senior"]) ) $korting = $korting + 10;
      if( isset($_POST["klant"]) ) $korting = $korting + 5;
      echo "Korting is: $korting %"; 
    ?>
</body>

echo isset($_POST['aantal'])將簡單地返回一個二進制值,該值是否已設置。 相反,您將需要檢查它是否已設置,如果已設置, 則將其回顯:

if(isset($_POST['aantal'])) {
  echo $_POST['aantal'];
}

至於在提交表單后保留字段中輸入的數字,您只需要將$_POST作為value屬性(如果已設置)回顯,如果$_POST不存在,則默認為原始0

這是最簡單的三元

<input type="text" size=2 maxlength=3 name="aantal"
       value="<?php echo (isset($_POST['aantal'])) ? $_POST['aantal'] : '0'; ?>"
       style="background-color:#f8ce6c" />

雖然也可以長期完成:

<input type="text" size=2 maxlength=3 name="aantal"
       value="<?php if (isset($_POST['aantal'])) { echo $_POST['aantal']; } else { echo '0'; } ?>"
       style="background-color:#f8ce6c" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM