0

I made table with inputs inside a form. One row shows details of a car, and under it there is another row with 5 inputs for updating the details.

When I fill the inputs for the first car and doing POST, It's not taking the values in the fields. But when I fill inputs for the second car It do taking the values.

Only the inputs of the second car are sending with POST method.

It might be an HTML syntax mistake but i can't find it.

This is how I'm checking the POST:

if (isset($_POST["Name_OF_Input"]) && count($_POST) > 0){code for updating the car details in DB};

HTML code:

    <form action='main.php' method='POST'>

    <table class='table table-striped'>
        <tr>
            <th style='width:252px'>Car ID</th>
            <th style='width:185px'>Car Color</th>
            <th style='width:260px'>Car Manufactuer</th>
            <th style='width:252px'>Model</th>
            <th style='width:253px'>Year</th>
            <th style='width:147px'>Options</th>
        </tr>
        <tr>
            <td>65-842-99</td>
            <td>#ff0000</td>
            <td>Dacia</td>
            <td>Albero</td>
            <td>2015</td>
            <td><button class='btn btn-primary glyphicon glyphicon-pencil' value='65-842-99' onclick='show(this);return false;'></button></td>
        </tr>
        <tr value='65-842-99' id='65-842-99'>
            <td><input type='text' name='id' placeholder='Car ID'></td>
            <td><label>Car Color:</label><input type='color' name='color'></td>
            <td><select name='manufacturer'>
        <option value=''>Pick car manufacturer</option>
        <option value='Fiat'>Fiat</option>
        <option value='Mercedes'>Mercedes</option>
        <option value='Audi'>Audi</option>
        <option value='Volkswagen'>Volkswagen</option>
        <option value='Mazda'>Mazda</option>
        <option value='Honda'>Honda</option>
        <option value='Dacia'>Dacia</option>
        <option value='Lincoln'>Lincoln</option>
        <option value='Volvo'>Volvo</option>
        </select></td>
            <td><input type='text' name='model' placeholder='Car Model'></td>
            <td><input type='text' name='year' placeholder='Car Year'></td>

            <td><button type='submit' class='btn btn-info glyphicon glyphicon-floppy-disk' name='carID' value='65-842-99'></button></td>
        </tr>
        <tr>
            <td>66-815-55</td>
            <td>#0080ff</td>
            <td>Volkswagen</td>
            <td>Golf</td>
            <td>2000</td>
            <td><button class='btn btn-primary glyphicon glyphicon-pencil' value='66-815-55' onclick='show(this);return false;'></button></td>
        </tr>
        <tr value='66-815-55' id='66-815-55'>
            <td><input type='text' name='id' placeholder='Car ID'></td>
            <td><label>Car Color:</label><input type='color' name='color'></td>
            <td><select name='manufacturer'>
        <option value=''>Pick car manufacturer</option>
        <option value='Fiat'>Fiat</option>
        <option value='Mercedes'>Mercedes</option>
        <option value='Audi'>Audi</option>
        <option value='Volkswagen'>Volkswagen</option>
        <option value='Mazda'>Mazda</option>
        <option value='Honda'>Honda</option>
        <option value='Dacia'>Dacia</option>
        <option value='Lincoln'>Lincoln</option>
        <option value='Volvo'>Volvo</option>
        </select></td>
            <td><input type='text' name='model' placeholder='Car Model'></td>
            <td><input type='text' name='year' placeholder='Car Year'></td>

            <td><button type='submit' class='btn btn-info glyphicon glyphicon-floppy-disk' name='carID' value='66-815-55'></button></td>
        </tr>
    </table>
</form>
  • You cannot have form inputs with the same name, the later one will overwrite the previous one. Give them different name attributes or make it an array: ` – jeroen Oct 05 '17 at 12:20

2 Answers2

3

You can not use same name of multiple fields.

You can use array

<td><input type='text' name='model[]' placeholder='Car Model'></td>
<td><input type='text' name='year[]' placeholder='Car Year'></td>

On PHP side you can get the values:

$_POST['model'] // this will be an array
$_POST['year'] // this will be an array
usman ikram
  • 461
  • 4
  • 10
0

When you have multiple inputs with the same name, the browser will send them all to the server (in order).

PHP, however, will discard all but the last one unless the name ends in [] in which case it will insert an array of values into $_POST.

Change the names to include [] and your logic to loop over the arrays.

<input type='text' name='model[]' placeholder='Car Model'>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335