Jump to content

Help Sending form data


Schlo_50

Recommended Posts

I am using a form combined with javascript which loads an extra text field when a user clicks 'add another'. This works and i now need code to send the data filled into ALL of the text fields added into my ms acces database (ODBC).

 

I got a rough idea from someone on here, but need help in cleaning it up so it all works.

 

<script language="javascript" type="text/javascript">

function addField() {
var tbody = document.getElementById("tblBody");
var ctr = tbody.getElementsByTagName("input").length + 1;
var input;

if ( ctr > 15 ) {
alert ("15 is the maximum amount of orders you are allowed.");
}else{

if (document.all){ //input.name doesn't work in IE
input = document.createElement('<input name="field_'+ctr+'">');
}else{
input = document.createElement('input');
input.name = "field_"+ctr;
}

input.id = input.name;
input.type = "text";
input.value = "";
input.className = "textfield";
var cell = document.createElement('td');
cell.style.height = '30px';
cell.appendChild(document.createTextNode(ctr+". "));
cell.appendChild(input);
var row = document.createElement('tr');
row.appendChild(cell);
tbody.appendChild(row);

window.document.the_form.count.value = ctr;

}
} 
</script>
</head>

<body>
<p>Testing Database input using a tbody.</p>
<p>Item Number</p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form action="<?php $_SERVER[php_SELF]; ?>" method="post" name="form">
<tbody id="tblBody">
<tr>
<td height="30">
1. <input name="field_1" type="text" class="textfield" id="field_1" />
</td>
</tr>
<tr>
<td height="30">
2. <input name="field_2" type="text" class="textfield" id="field_2" />
</td>
</tr>
<tr>
<td height="30">
3. <input name="field_3" type="text" class="textfield" id="field_3" />
</td>
</tr>
<tr>
<td height="30">
4. <input name="field_4" type="text" class="textfield" id="field_4" />
</td>
</tr>

</tbody>
</table>
<input name="count" type="hidden" id="count" value="4"/> 
<input name="add" type="button" class="button" id="add" value="Add Another" onClick="addField();"/><br /><br />
<input name="submit" type="submit" value="Submit" />
</form>
<?php
if ($_POST[submit] == "Submit") 
{
$conn = odbc_connect('root', 'root', '') or die('Could not Connect to ODBC Database!');

$calc = for($i=0,$<15,++$i)if($_POST['field_'.$i])

$sql = odbc_query("insert into Order1('ProductNotes')values('$calc')");

$rs = @odbc_exec($conn,$sql);
if (!$rs)
{
echo "An error has occured. Please try again";
}
else
{
echo "The record was successfully inserted.";
}
odbc_close($conn);
}
?>
</body>
</html>

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/76377-help-sending-form-data/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.