Form loop db insertion + javascript altering
- by MrStatic
I basically need to check if there is an easier way to do this before I rewrite all the code. I have a fairly large form that I have each input named with []'s on the end so I can loop through via php for easy insertion.
<input type="hidden" name="currentdate[]" value="<?php echo date('mdY'); ?>">
<td><input style="width: 50px" type="text" name="jackname[]" /></td>
<td><input style="width: 20px" type="text" name="jackkey[]" /></td>
<td><input style="width: 50px" type="text" name="jackbeg[]" /></td>
<td><input style="width: 50px" type="text" name="jackend[]" /></td>
<td><input style="width: 50px" type="text" name="jackbegveh" /></td>
<td><input style="width: 50px" type="text" name="jackbegmon[]" /></td>
<td><input style="width: 50px" type="text" name="jackendveh" /></td>
<td><input style="width: 50px" type="text" name="jackendmon[]" /></td>
<td><input style="width: 50px" type="text" name="jacktx" disabled /></td>
There are quite a few more fields but you get the idea. I then use
foreach ($_POST['jackname'] as $row=>$name)
{
$jackname = $name;
$date = $_POST['currentdate'][$row];
$jackkey = $_POST['jackkey'][$row];
$jackbeg = $_POST['jackbeg'][$row];
$jackend = $_POST['jackend'][$row];
$jackbegveh = $_POST['jackbegveh'][$row];
$jackbegmon = $_POST['jackbegmon'][$row];
$jackendveh = $_POST['jackendveh'][$row];
$jackendmon = $_POST['jackendmon'][$row];
$jacktx = $_POST['jacktx'][$row];
if ($jacktx == '') {
$jacktx = '0';
}
if (empty($jackkey)) {
echo 'Skipped empty! <br />';
} else {
mysql_query("INSERT INTO `ticket_counts_jackson` VALUES('', '" . $date . "', '" . $jackname . "', '" . $jackkey . "', '" . $jackbeg . "', '" . $jackend . "', '" . $jackbegveh . "', '" . $jackbegmon . "', '" . $jackendveh . "', '" . $jackendmon . "', '" . $jacktx . "')", $mysql_link) or die(mysql_error());
echo 'Added the info the db! <br />';
}
}
I use the above to loop through the form and add it to the database. Now for my main question. I also want to add in some javascript to do a little math. Basically ($jackendveh - $jackbegveh) - ($jackendmon - $jackbegmon) and have that displayed in jacktx. Currently the only way I know of adding in the math calculations is to rename each input to a unique name and then rewrite my insert from 1 insert to 8 inserts.