简体   繁体   中英

PHP Assign variables to multiple dynamic form rows

I am having a little trouble getting my head around a script that pulls info from a form and saves to a csv on the server. The issue is, I have a form with multiple tables, in these tables there is a "+" button to add another row. Which I am using jquery to do so. This works fine and also saves the amount of rows into a hidden input and assigns a _0, _1, _2 to the textarea name.

I'm just really confused how I'll use PHP to dynamically assign variables to these depending on how many rows the user adds. This is my form:

<table style="width:100%;" id="directEmployees">
<tbody>
    <tr>
        <td>Name</td>
        <td>Time Start</td>
        <td>Time Finish</td>
        <td>Job Description</td>
        <td>Plant/Machinery Issued/Used</td>
        <td>P.P.E Issued?</td>
        <td>Materials Issued/Used</td>
    </tr>
    <tr>
        <td width="20%"><textarea style="width:100%;" name="directName_0" id="directName_0"></textarea></td>
        <td width="10%"><textarea style="width:100%;" name="directTimeStart_0" id="directTimeStart_0"></textarea></td>
        <td width="10%"><textarea style="width:100%;" name="directTimeEnd_0" id="directTimeEnd_0"></textarea></td>
        <td width="20%"><textarea style="width:100%;" name="directJob_0" id="directJob_0"></textarea></td>
        <td width="15%"><textarea style="width:100%;" name="directPlant_0" id="directPlant_0"></textarea></td>
        <td width="10%"><textarea style="width:100%;" name="directPPE_0" id="directPPE_0"></textarea></td>
        <td width="15%"><textarea style="width:100%;" name="directMaterials_0" id="directMaterials_0"></textarea></td>
    </tr>
</tbody>
</table>
<h2 id="addEmployee" style="cursor:pointer;">+</h2>

<button type="submit">Submit</button>

</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function(){
    var additional_rows = 0;
    $('#addEmployee').click(function() {
        additional_rows = additional_rows + 1;
        $('#rowsEmployee').val(additional_rows);

        var addTable = '<tr><td width="20%"><textarea style="width:100%;" name="directName_' + additional_rows + '" id="directName_' + additional_rows + '"></textarea></td><td width="10%"><textarea style="width:100%;" name="directTimeStart_' + additional_rows + '" id="directTimeStart_' + additional_rows + '"></textarea></td><td width="10%"><textarea style="width:100%;" name="directTimeEnd_' + additional_rows + '" id="directTimeEnd_' + additional_rows + '"></textarea></td><td width="20%"><textarea style="width:100%;" name="directJob_' + additional_rows + '" id="directJob_' + additional_rows + '"></textarea></td><td width="15%"><textarea style="width:100%;" name="directPlant_' + additional_rows + '" id="directPlant_' + additional_rows + '"></textarea></td><td width="10%"><textarea style="width:100%;" name="directPPE_' + additional_rows + '" id="directPPE_' + additional_rows + '"></textarea></td><td width="15%"><textarea style="width:100%;" name="directMaterials_' + additional_rows + '" id="directMaterials_' + additional_rows + '"></textarea></td></tr>';

        $('#directEmployees tbody').append(addTable);
    });
});

</script>

Any help would be greatly appreciated.

once the form is submitted to your php script, either the $_GET or $_POST array will be populated, depending on the form method.. so you will need to work through the array, something like...

foreach($_POST as $key=>value) {
  //process value 
}

PHP is capable of creating an array out of multiple inputs with a single name by using square brackets. I am not entirely sure of what you are trying to accomplish, but if there are a dynamic number of rows, you might want to avoid giving each one a unique name.

You might consider a naming convention similar to this:

<textarea style="width:100%;" name="directTimeStart[]" id="directTimeStart_0"></textarea></td>
<textarea style="width:100%;" name="directTimeEnd[]" id="directTimeEnd_0"></textarea></td>
<textarea style="width:100%;" name="directJob[]" id="directJob_0"></textarea></td>
<textarea style="width:100%;" name="directPlant[]" id="directPlant_0"></textarea></td>
<textarea style="width:100%;" name="directPPE[]" id="directPPE_0"></textarea></td>
<textarea style="width:100%;" name="directMaterials[]" ... ></td>

Which will allow you to completely forget about the numbering on the client side, and shift the task to PHP. In PHP, you will be able to access the first row by using $_GET[directTimeStart][0] , the second row with $_GET[directTimeStart][1] , etc.

$count = count($_POST['directTimeStart']);
for ($i = 1; $i <= $count; $i++) {
 $csv .= $_POST[directTimeStart][$i] . ',';
 $csv .= $_POST[directTimeEnd][$i] . ',';
 $csv .= $_POST[directJob][$i] . ',';
 $csv .= $_POST[directPlan][$i] . ',';
 $csv .= $_POST[directPPE][$i] . ',';
 $csv .= $_POST[directMaterials][$i] . '\n';
}

return $csv;

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