Hoping someone can assist with this, been looking at it since the weekend. I have a form which contains 3 fields (Name, Email, Quarter) and a table with multiple questions (I reduced the code to just 2 questions for illustration purposes.
I want the 3 fields (Name, Email, Number) to be written to the database with question 1 in row 1 and with question 2 in row 2.
Problem is my code only writes the 3 fields (Name, Email, Number) to row 1 (question 1) and writes blanks to row 2 for the 3 fields (Name, Email, Number), the table fields write successfully. What am i doing wrong?
Database View
Name Email Number KPICode Response Compliant Comments Joe Joe@test.com 555 k21 yes yes test 554 k45 no no test
HTML Code:
<div class="item form-group">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="hfname">Name</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="name" name="name[]" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<Select type="email" id="email" name="email[]" class="form-control col-md-7 col-xs-12"></select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="quarter">Quarter:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select id="quarter" name="quarter[]" class="form-control">
<option>Choose option</option>
<option>Q1</option>
<option>Q2</option>
<option>Q3</option>
<option>Q4</option>
</select>
</div>
</div>
</div>
<table class="table table-bordered">
<thead class="headings">
<tr>
<th>Code</th>
<th>Category</th>
<th>Performance Indicator</th>
<th>Response</th>
<th>Compliance</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td id="env1" name ="code[]" type="text" scope="row">ENV1</td>
<td>1.1 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response[]">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance[]">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes[]" role="row" Type="text"></td>
</tr>
<tr>
<th id="env2" name="code[]" scope="row">ENV2</th>
<td>1.2 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response[]">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance[]">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes[]" role="row" Type="text"></td>
</tr>
</tbody>
</table>
My php code to write the info to the db:
foreach($_POST['response'] as $index => $val)
{
$response = $val;
$rfname = $_POST['name'][$index];
$HospitalName = $_POST['email'][$index];
$quarter = $_POST['quarter'][$index];
$kpicode = $_POST['code'][$index];
$compliance = $_POST['compliance'][$index];
$notes = $_POST['notes'][$index];
$sql = "INSERT INTO InfoForm (Name, Email, Quarter, KPICode, Response,
Compliance, Notes)
VALUES ('$name', '$email', '$quarter', '$kpicode', '$response',
'$compliance', '$notes')";
$result = mysqli_query($conn, $sql);
}