Josh Kindy
Josh Kindy

Reputation: 33

Why Does my $_Post only appear to Store the Last Value Inputted?

I'm Tring to use Var_dump or print_r to Debug my code before I write the php code to insert it into a Database. When I use these methods I only get the Last value of my Form, I've searched why and can't quite find the Answer, I changed name="something" to name="something[]", that seems to be the most common mistake, but that didn't seem to change anything.

So I have this HTML Code on my form page:

    <form method="post" action="phptestpage.php">

<table>
    <tr>
        <th>Job Number</th>
        <th>Timesheet #</th>
        <th>Work Date</th>
        <th>Shift</th>
    </tr>
    <tr>
        <td><input type="text" name="job_number"></td>
        <td><input type="text" name="timesheet"></td>
        <td><input type="date" name="work_date"></td>
        <td><select>
            <option value="days">Days</option>
            <option value="nights">Nights</option>
            </select></td>
    </tr>
</table>


<table>
    <tr>
        <th id="work_done">Work Done</th>
        <th id="equipment_used">Equipment Used</th>
    </tr>
    <tr>
        <td><textarea type="text" name="work_done"      id="work_done_input"></textarea></td>
        <td><textarea type="text" name="equipment_used" id="equipment_used_input"></textarea></td>
    </tr>
</table>
<table id="time_table">
    <tr>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Rate</th>
        <th class="time">ST</th>
        <th class="time">TH</th>
        <th class="time">DT</th>
        <th class="time">EX-ST</th>
        <th class="time">EX-TH</th>
        <th class="time">EX-DT</th>
    </tr>
    <tr class="blank_row">
        <td><input type="text" name="last_name[]" class="last_name"></td>
        <td><input type="text" name="first_name"></td>
        <td><input type="text" name="rate"></td>
        <td><input type="number" name="st" class="time_input" value="0"></td>
        <td><input type="number" name="th" class="time_input" value="0"></td>
        <td><input type="number" name="dt" class="time_input" value="0"></td>
        <td><input type="number" name="ex_st" class="time_input" value="0"></td>
        <td><input type="number" name="ex_th" class="time_input" value="0"></td>
        <td><input type="number" name="ex_dt" class="time_input" value="0"></td>
    </tr>
    <tr>
        <td><input type="text" name="last_name[]" class="last_name"></td>
        <td><input type="text" name="first_name"></td>
        <td><input type="text" name="rate"></td>
        <td><input type="number" name="st" class="time_input" value="0"></td>
        <td><input type="number" name="th" class="time_input" value="0"></td>
        <td><input type="number" name="dt" class="time_input" value="0"></td>
        <td><input type="number" name="ex_st" class="time_input" value="0"></td>
        <td><input type="number" name="ex_th" class="time_input" value="0"></td>
        <td><input type="number" name="ex_dt" class="time_input" value="0"></td>
    </tr> 
</table>
    
<input type="submit" value="Submit" name="submit">
    
</form>

and I'm using this code to try to See what values it's outputting:

<?php
$names = $_POST['last_name'];
var_dump($names);
?>

I've tried a few variations not knowing what i'm really doing, including putting [] inside the names variable value like $_POST['last_name[]'] to try to match the variable, I've made mistakes like that before.

Upvotes: 2

Views: 33

Answers (1)

Spholt
Spholt

Reputation: 4012

You need to wrap you inputs in a <form> element with the correct action and method.

<form action="/path/to/file.php" method="POST">
    <input type="text" name="last_name[]" class="last_name">
</form>

By doing this you should then be able to return an array using $_POST['last_name']

EDIT

I have tested this locally with a simplified version of the form and the following will work:

<form method="POST">
    <input type="text" name="last_name[]">
    <input type="text" name="last_name[]">
</form>

This will return $_POST['last_name'] = array(0 => 'test1', 1 =>'test2'). I think that your problem is not with what you have named your variables but something else in your script. I would recommend commenting out sections and debugging as you go.

Upvotes: 1

Related Questions