Schlo_50 Posted November 7, 2007 Share Posted November 7, 2007 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! Quote Link to comment Share on other sites More sharing options...
trq Posted November 7, 2007 Share Posted November 7, 2007 What is the problem? Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 7, 2007 Author Share Posted November 7, 2007 The following lines are causing a stir: $calc = for($i=0,$<15,++$i)if($_POST['field_'.$i]) $sql = odbc_query("insert into Order1('ProductNotes')values('$calc')"); Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 8, 2007 Author Share Posted November 8, 2007 Can anyone help me? The line originally started as this: for($i=0,$<15,++$i)if($_POST['field_'.$i])obdc_query("insert into table('bar')values('$_POST[field_$i]')"; I just need to know how to implement this into my script so that it works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.