ellie stone
ellie stone

Reputation: 125

How is it possible to keep values still in their fields after submit?

   <form action='checks.php' method='POST'>
    <div class ="bookinform">
      <table>
         <tr>
            <td>Name of Driver *</td>
            <td>
               <input type='Text' id="driver_name" name='driver_name' required autocomplete="off" size='7' maxlength='25' style='width:400px;font-family:Century Gothic' value = "">
            </td>
         </tr>
         <tr>
            <td>&nbsp;</td>
            <td></td>
         </tr>
         <tr>
            <td>Vehicle Reg *</td>
            <td>
               <input type='Text' id="Vehicle_Reg" name='Vehicle_Reg' required autocomplete="off" size='7' maxlength='15' style='width:400px;font-family:Century Gothic; text-transform:uppercase;' value = "">
            </td>
         </tr>
         <tr>
            <td>&nbsp;</td>
            <td></td>
         </tr>
         <tr>
            <td valign='top'>Haulier *</td>
            <td valign='top'>
              <input type='Text' id="query" name='haulier' style='width:400px; font-family:Century Gothic; text-transform:uppercase;' value = "">

         <tr>
            <td>&nbsp;</td>
            <td></td>
         </tr>

         </table>

     <input type='submit' name='FORM' value="Book-in Piece/s" style='width:100%;font-family:Century Gothic; margin-top:10px; color:#2EFE2E;'>
   </form>

How can I keep the values entered into Name of drive, Vehicle reg and Haulier to stay there after the submit button has been pressed? I want this so they don't have to be continuously entered. My form already as an action going else where so I can use $_POST on this page?

Upvotes: 1

Views: 68

Answers (2)

Ben
Ben

Reputation: 8991

All you need to do is to check to see if the value for that input has already been POSTed and, if it has, to echo that value.

To do this, in the val attribute, add:

<?php if(isset($_POST["name_of_input"])) echo $_POST["name_of_input"]; ?>

...or using ternary operators:

<?php echo(isset($_POST["name_of_input"]) ? $_POST["name_of_input"] : ""); ?>

It is also good practice to output using htmlspecialchars() as some POST content might contain invalid HTML.


Full example

<input type='Text'
    id="driver_name"
    name='driver_name'
    required
    autocomplete="off"
    size='7'
    maxlength='25'
    style='width:400px;font-family:Century Gothic'
    value="<?php echo (isset($_POST["driver_name"]) ? htmlspecialchars($_POST["driver_name"]) : ""); ?>">

Upvotes: 7

Nere
Nere

Reputation: 4097

You can maintain the value after submit by this way:

<input type='Text' id="driver_name" name='driver_name' required autocomplete="off" size='7' maxlength='25' style='width:400px;font-family:Century Gothic' value = "<?php echo isset($_POST['driver_name']) ? $_POST['driver_name'] : NULL ?>">

Upvotes: 0

Related Questions