如何回显输入类型文本?

时间:2018-03-28 21:52:02

标签: php html

我想回应一下: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>

1 个答案:

答案 0 :(得分:0)

echo isset($_POST['aantal'])将只返回值是否设置的二进制值。相反,您要检查是否,如果设置了,请将其回复:

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

至于在提交表单后保留在字段中输入的数字,您只需将$_POST作为 value 属性回显即可它已设置,如果0不存在,则默认为原始$_POST

使用 ternary

最简单
<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" />