Schlo_50 Posted November 12, 2007 Share Posted November 12, 2007 I have on a page 4 text fields shown for a user to enter an amount in. If they have more than 4 amounts to enter in i have a button which, when clicked adds another text field although the max amount of text fields avaliable is 15. I need my php script to take the values entered by the user and send them into my MS Access database and order them into one field within the table with each value separated my commas. I have included this post my php so far, and the javascript code which generates the text fields which are unique in name. PHP <?php if ($_POST[submit] == "Submit") { $conn = odbc_connect('accessname', 'root', '') or die('Could not Connect to ODBC Database!'); for($i=0,$<15,++$i)if($_POST['field_'.$i])); $sql = odbc_query("insert into Order1('ProductNotes')values('$_POST['field_$i'])"); $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); } ?> At present, this line is giving my problems: for($i=0,$<15,++$i)if($_POST['field_'.$i])); 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; } } If anyone can come up with something to make all values POST into my database that would be extremely helpful. Thanks Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 12, 2007 Share Posted November 12, 2007 try this <?php if ($_POST[submit] == "Submit") { $conn = odbc_connect('accessname', 'root', '') or die('Could not Connect to ODBC Database!'); $arrData = array(); for($i=0,$i<15,++$i) { if(isset($_POST['field_'.$i])) { array_push($arrData,$_POST['field_'.$i])); } } $sql = odbc_query("insert into Order1 ('ProductNotes') values ('".implode(",",$arrData)."')"); $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); } ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted November 12, 2007 Share Posted November 12, 2007 As a quick example you don't HAVE to number your text input field boxes like field_1, field_2 etc... Just name them ALL field[] . e.g. <input type="text" name="field[]" /> <input type="text" name="field[]" /> <input type="text" name="field[]" /> <input type="text" name="field[]" /> <input type="text" name="field[]" /> This way you don't have to worry about naming them all differently. Then in your PHP you ALREADY have an array ($_POST['field']), so just do $values = implode(",",$_POST['field'])) And use $values in your SQL statement. 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.