Schlo_50 Posted November 5, 2007 Share Posted November 5, 2007 Hello, I am currently working on a site which is run with PHP using ODBC. On one of the pages i have products stored in my MS Access db for sale displayed in a list. At the bottom of that list I have a form for the user to fill out containing 3 text fields, one called 'customer number', the second called 'item number' and the third 'quantity'. By default the 'item number' field shows 4 fields but using javascript a user can click 'add another' and another renamed unique field opens up. My problem is i have no idea how to get the form to upload the users data to my MS Access database. I can quite easily get the customer number to upload but im not sure what code to use to say, when more than one 'item number' field is generated upload that information too! My other problem is storing the order information in table nicely. My table structure is as follows: CustomerId | Item Number | Quantity | Date | As a customer can order various items in one order so to speak it would be good if the data entered into the database was all stored within one row but seperated by commas. E.g. CustomerId | Item Number | Quantity | Date | Customber1 | It1, It2, It3 | 3, 6, 1 | 05/11/07| Is this possible? Could someone take a loo at my code to see what i mean and give me some tips? My brain hurts from trying to find the best solution! <head> <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> <form name="the_form" id="the_form" method="post" action="?id=inc/products"> <?php include('odbc/***.php'); $sql = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg()); $prevCat=''; while($row = odbc_fetch_array($sql)) { $Category = $row["CategoryName"]; $ProductId = $row["ProductId"]; $ProductName = $row["ProductName"]; $Price = $row["Price"]; // has category changed // if so, print it if ($Category != $prevCat) { echo "<h2>$Category</h2>"; } echo 'Item Code: ', $ProductId, '<br/>Item Name: ', $ProductName, '<br/><br/>Price: £', $Price, '<br/><br/>'; $prevCat = $Category; } ?> <hr /> Order Form<br /><br /> <table width="593" border="0" cellspacing="0" cellpadding="5"> <tr> <td width="91">Item Number: </td> <td width="176"><input name="a" type="text" id="a" /></td> <td width="79">Quantity:</td> <td width="238"><input name="1" type="text" id="1" /></td> </tr> </table> <p><table width="100%" border="0" cellspacing="0" cellpadding="0"> <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();"/> </p> <br /> <br /> <input name="submit" type="Submit" value="Submit" /> </p> </form> Thank-you so much for any help! Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 5, 2007 Author Share Posted November 5, 2007 Ok guys, I have kind of double posted but i think i have needed to re-state my problem in a way that is simpler to get an answer. I have a form, which has some javascript added to it in order to allow a user to click a button to add more fields. I need some PHP code which will take ALL information entered into the text fields (even generated ones) and plonk them into my access databse as i am using ODBC + PHP. Here is the form: <head> <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> Order Form<br /><br /> <p><table width="100%" border="0" cellspacing="0" cellpadding="0"> <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();"/> </p> <br /> <br /> <input name="submit" type="Submit" value="Submit" /> </p> </form> Thank-you very much for any help, code examples or pointers! Thanks Quote Link to comment Share on other sites More sharing options...
Azu Posted November 6, 2007 Share Posted November 6, 2007 Maybe something like this? for($i=0,$<15,++$i)if($_POST['field_'.$i])obdc_query("insert into table('foo','bar')values('blah','$_POST[field_$i]')"; (obviously just an example ^^) Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted November 7, 2007 Author Share Posted November 7, 2007 Thanks, thats just the sort of thing im looking for! I will try to make that work. 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.