James Ham
James Ham

Reputation: 119

Inserting Dynamic amount of rows from Form into Table

Form:

<table width="100%" cellpadding="4" cellspacing="0" id="px">
<thead>
<tr class="tab_head">
<td width="25px" style="text-align:center; color:#FFFFFF; font-size:11px;">#</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">First Name</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Last Name</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Wgt</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Address</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Phone</td>
<td class="form_label" style="color:#FFFFFF; font-size:11px;">Email</td>
<td></td>
</tr>
</thead>
<tbody id="tbl1">
<tr id="pass">
<td style="vertical-align:middle; text-align:center"><input type="text" name="pass_num[]" readonly="readonly" value="1" class="pass" style="font:Verdana; font-size:11px; border:none; background-color:transparent; width:25px; text-align:center; vertical-align:middle;" tabindex="-1"></td>
<td><input type="text" id="cfn1" name="cust_fname[]" class="form_g" /></td>
<td><input type="text" id="cln1" name="cust_lname[]" class="form_g" /></td>
<td><input type="text" name="cust_kg[]" class="form_a" /></td>
<td><input type="text" name="cust_addr[]" class="form_c" /></td>
<td><input type="text" name="cust_phone[]" class="form_g" /></td>
<td><input type="text" name="cust_email[]" class="form_b" /></td>
<td style="vertical-align:middle"><a class="removePax"><img src="images/remove_pax.png" /></a></td></tr>

</tbody>
</table>

Table Columns:

cust_id
cust_fname
cust_lname
cust_kg
cust_addr
cust_phone
cust_email

There is a snippet of JQuery that essentially clones the above table as required to allow the user to add another passenger etc. On submittinh, the data is posted to the next page fine, and I've tested numerous times that the data is coming through in Arrays (eg. $_POST[cust_fname] etc.).

The problem I'm having now is turning these into sets that I can insert into the table.

I tried a foreach loop and iterating but so far, I'm failing to come up with a solution. I have tried sorting the data into per passenger, but so far keeps clumping the data together.

JSFiddle

Upvotes: 0

Views: 73

Answers (1)

Adam Cherti
Adam Cherti

Reputation: 982

In your php code, you have to iterate with one for loop all the arrays in the same time and print them. Example :

cust_fname = $_POST["cust_fname"];
cust_lname = $_POST["cust_lname"];
for($i = 0; $i < count($cust_fname); $i++)
{
    echo "Complete name : ".$cust_fname[$i]." ".$cust_lname[$i];
    echo "<br/>";
}

I just put an example for two fields, you can do the rest :)

EDIT : I saw that you want to show them in a table, so you can change the code i gave you to :

cust_fname = $_POST["cust_fname"];
cust_lname = $_POST["cust_lname"];
echo "<table><tr><th>First name</th><th>Last Name</th></tr><tbody>";
for($i = 0; $i < count($cust_fname); $i++)
{
    echo "<tr>";
    echo "<td>".$cust_fname[$i]."</td><td> ".$cust_lname[$i]."</td>";
    echo "</tr>";
}
echo "</tbody></table">;

Upvotes: 1

Related Questions