简体   繁体   中英

Insert all HTML form fields (filled and empty) to array php

I have a large HTML form where I currently insert filled html fields into and array. When I print out my $_POST['rep_list']; it gives me the keys and values filled out from the form. But what I need is an array with all form fields ( even the ones not filled out ).

Any suggestion/examples on how to achieve this?

    <form action="insert-data.php" name="workCard" method="POST">
    <fieldset>
    <legend><h3>Repair</h3></legend>
    <table>
      <tr>
        <th>Description</th>
        <th>Front</th>
        <th>Back</th>       
      </tr>
      <tr>
        <td>tire</td>
        <td><input type="checkbox" name="rep_list[tire_front]" value="tire_front" /></td>
        <td><input type="checkbox" name="rep_list[tire_back]" value="tire_back" /></td>
        <td>alm <input type="checkbox" name="rep_list[tire_reg]" value="tire_reg" /></td>
        <td>indl <input type="checkbox" name="rep_list[tire_indl]" value="tire_indl" /></td>
      </tr>
      <tr>
        <td>Tube</td>
        <td><input type="checkbox" name="rep_list[tube_front]" value="tube_front" /></td>
        <td><input type="checkbox" name="rep_list[tube_back]" value="tube_back" /></td>
      </tr>
      <tr>
        <td>Hub.rep.</td>
        <td><input type="checkbox" name="rep_list[hub_front]" value="hub_front" /></td>
        <td><input type="checkbox" name="rep_list[hub_back]" value="hub_back" /></td>
        <td>just. <input type="checkbox" name="rep_list[hub_adjust]" value="hub_adjust" /></td>
 etc…....

If you want to do this without javascript you can create text input's for all checkboxes. For example:

<td>
    <input type="hidden" name="rep_list[tire_front]" value="False" />
    <input type="checkbox" name="rep_list[tire_front]" value="True" />
</td>

When you check the POST in PHP you can determine if a checkbox is checked or not by checking the value .When the POST value is True the checkbox was checked and False is unchecked.

With checkboxes if they are ticked they will be set in the HTTP post with their given value (or default of 'on' if a value is not set). If they are not ticked no value will be sent.

To check if a checkbox was ticked in PHP you could simply check if a POST value was set:

$tire_front = isset($_POST['rep_list']['tire_front']);

If you wanted to do this for every item you could create an array of the keys and loop through each of these:

$fields = array(
    'tire_front',
    'tire_back',
    'tire_reg',
    'tire_indl',
    'tube_front',
    'tube_back',
    'hub_front',
    'hub_back',
    'hub_adjust'
);

foreach ($fields as $field) {
    $rep_list[$field] = isset($_POST['rep_list'][$field]);
}

$rep_list would then contain an entry for each of the fields with a value of true if ticked, or false if not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM