I have been pounding my head up against the wall trying to figure out why I can't get my form to dynamically post to the table(s). I have a form that dynamically adds fields and (should) generate several arrays to be inserted into the table. I can get the static information to post to the right table, but the arrays just aren't working for me.
My code's a little messy from trying one thing after the other for the past few days, and especially these past few hours. So, I apologize. I'm just having difficulties getting it figured out. Any assistance would be greatly appreciated.
This is the form excerpt:
<head>
<style type="text/css">
div {width: 1024px;}
input { width: 60px; height:20px; margin-bottom: 6px; line-height: 16px; font-size: 10px;}
span { padding: 0px 5px 0px 20px; font-family: "Arial"; font-size: 14px;}
.hide {display:none;}
.head { padding-left: 20px; padding-bottom: 15px; padding-right: 20px; padding-top: 21px; background-color: #d3ffcc;}
.inventory { padding-left: 5px; padding-bottom: 15px; padding-right: 20px; padding-top: 21px; background-color: #fff8cb;}
.entry-table {padding-left: 3px; padding-bottom:15px; padding-right:3px; padding-top: 21px; background-color:#CCFFFF;}
</style>
<script type="text/javascript">
var pn = 1;
var old = 0;
function addInput(selectedForm){
if(document.getElementById(selectedForm).value != 1) {
var divName = "dynamicInput";
var newdiv = document.createElement('div');
ScrollToElement("scroll");
document.getElementById(old).value = 1;
newdiv.innerHTML = '<span onClick="binEdit('+pn+');">Pure Seed:</span><input type="text" name="pureseed[]" id="pureseed-'+pn+'" autocomplete="off"> <span>Description:</span><input type="text" onkeyup="toUpper(\'description-'+pn+'\')" name="description[]" autocomplete="off" id="description-'+pn+'" > <span>Germination:</span><input type="text" name="germination[]" onfocus="addInput('+pn+');" id="germination-'+pn+'" autocomplete="off"> <span>Hard Seed:</span><input type="text" name="hardseed[]" onfocus="addInput('+pn+');" id="hardseed-'+pn+'" autocomplete="off"> <span>Total:</span><input type="text" name="total[]" onfocus="addInput('+pn+');" id="total-'+pn+'" autocomplete="off"> <span>Date:</span><input type="text" name="date[]" onfocus="addInput('+pn+');" id="date-'+pn+'" autocomplete="off"> <input type="text" value="0" id="'+pn+'" disabled="disabled" autocomplete="off" class="hide"><br> ';
document.getElementById(divName).appendChild(newdiv);
pn++;
old++;
}
}
function binEdit(id) {
var el = document.getElementById("bin-"+id);
el.disabled = false;
el.focus();
}
function ScrollToElement(id){
var theElement = document.getElementById(id);
var selectedPosX = 0;
var selectedPosY = 0;
while(theElement != null){
selectedPosX += theElement.offsetLeft;
selectedPosY += theElement.offsetTop;
theElement = theElement.offsetParent;
}
window.scrollTo(selectedPosX,selectedPosY);
}
function toUpper(id) {
var theElement = document.getElementById(id);
theElement.value = theElement.value.toUpperCase();
}
function show(id) {
var theElement = document.getElementById(id);
theElement.className = id;
}
</script>
<title>Receiving Bulk Seed</title>
</head>
<?php
if (!isset($_POST['submit'])) {
?>
<body onLoad="document.getElementById('selectedform').focus()">
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<table align="center">
<tr><td><br>
<div class="head">
<span>Part Number:</span><input type="text" name="pn" autocomplete="off" onKeyUp="toUpper('pn'); show('entry-table');" id="pn">
<span>Description:</span><input type="text" name="partdesc" autocomplete="off" onKeyUp="toUpper('partdesc')" id="partdesc">
<span>Lot Number:</span><input type="text" name="lot" autocomplete="off" onKeyUp="toUpper('lot')" id="lot"><br><br>
<span>Inert Matter:</span><input type="text" name="inert" autocomplete="off" id="inert">
<span>Weed Seed:</span><input type="text" name="weedseed" autocomplete="off" id="weedseed">
<span>Other Crop:</span><input type="text" name="othercrop" autocomplete="off" id="othercrop"><br>
<span>Country/State of Origin:</span><input type="text" name="origin" autocomplete="off" id="origin"><p></p>
<span>Are there any noxious weeds?</span><br>
<span> Yes</span><input type="radio" name="noxious" autocomplete="off" value="Yes" id="noxious"><br>
<span> No </span><input type="radio" name="noxious" autocomplete="off" value="No" id="noxious"><br>
<span>Does the label say, "Inclusive Of Nitro Coat Organic Coating"?</span><br>
<span>Yes</span><input type="radio" name="nitrocoat" autocomplete="off" value="Yes" id="nitrocoat"><br>
<span>No </span><input type="radio" name="nitrocoat" autocomplete="off" value="No" id="nitrocoat">
</div>
<br><br>
<div class="entry-table" id="entry-table">
<p>Please Enter The Data For Each Seed</p>
<div id="dynamicInput">
<span>Pure Seed:</span><input type="text" name="pureseed[]" id="pureseed-0" autocomplete="off" >
<span>Seed Description</span><input type="text" name="description[]" onKeyUp="toUpper('description-0')" onFocus="addInput(0);" id="description-0" autocomplete="off">
<span>Seed Germination:</span><input type="text" name="germination[]" onFocus="addInput(0);" id="germination-0" autocomplete="off">
<span>Hard Seed:</span><input type="text" name="hardseed[]" onFocus="addInput(0);" id="hardseed-0" autocomplete="off">
<span>Total</span><input type="text" name="total[]" onFocus="addInput(0);" id="total-0" autocomplete="off">
<span>Date:</span><input type="text" name="date[]" onFocus="addInput(0);" id="date-0" autocomplete="off">
<input type="text" value="0" id="0" disabled="disabled" autocomplete="off" style="display:none;">
</div><br><input type="submit"></div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><div id="scroll"></div>
</td></tr></table>
</form>
</body>
and this is the code for the database:
<?
$pn=$_POST['pn'];
$partdesc=$_POST['partdesc'];
$lot=$_POST['lot'];
$inert=$_POST['inert'];
$weedseed=$_POST['weedseed'];
$othercrop=$_POST['othercrop'];
$origin=$_POST['origin'];
$noxious=$_POST['noxious'];
$nitrocoat=$_POST['nitrocoat'];
$host="host";
$user="user";
$pass="pass";
$db="db";
$connection=mysql_connect($host, $user, $pass)or die("Unable to connect to SQL Server");
mysql_select_db($db) or die ("Unable to select database");
$query ="INSERT INTO partnumbers_test (Lot_Number, Part_Number, Description, Other_Crop, Inert_Matter, Weed_Seed, Noxious_Weeds, Origin) VALUES ('$lot', '$pn', '$partdesc', '$othercrop', '$inert', '$weedseed', '$noxious', '$origin')";
$result = mysql_query($query, $connection);
mysql_close($connection);
foreach($_POST['pureseed'] as $row=>$pureseed)
{
$pureseed=mysql_real_escape_string($pureseed);
$description=mysql_real_escape_string($_POST['description'][$row]);
$germination=mysql_real_escape_string($_POST['germination'][$row]);
$hardseed=mysql_real_escape_string($_POST['hardseed'][$row]);
$total=mysql_real_escape_string($_POST['total'][$row]);
$date=mysql_real_escape_string($_POST['date'][$row]);
$date=('m-y');
$query = "INSERT INTO ingredients_test (Lot_Number, Pure_Seed, Name, Germination, Hard_Seed, Total, Test_Date) VALUES ('$lot', '$pureseed', '$description', '$germination', '$hardseed', '$total', '$date')";
$result = mysql_query($query, $connection);
mysql_close($connection);
}
}
?>
I don't believe it's seeing the pure_seed as an array, or anything else for that matter. I don't know what exactly I need to do to fix this. Perhaps the way I have it written now is completely wrong!
Anything would be better than where I'm at right now.
Thank you everyone for your time.