dmschenk Posted June 9, 2008 Share Posted June 9, 2008 Hi Everyone I need some help understanding how to build an order form. All the item information (approx 80 items) is populated from a mysql database (name, price..) but I need to somehow add a few fields in the form that know which item they belong to. I need to programatiaclly add a quantity field and a number of days field but being a newbie I do not know how it should be incorporated into the form. I've added the 2 fields into the loop so that they are repeated for every item but they all have the same name which does me no good at all. eventually I want to have subtotals for each item and a running total. Could someone please help me sort this out and point me in the right direction? below is what I have so far: mysql_select_db($database_dbPC, $dbPC); $query_rsProducts = "SELECT * FROM products"; $rsProducts = mysql_query($query_rsProducts, $dbPC) or die(mysql_error()); $row_rsProducts = mysql_fetch_assoc($rsProducts); $totalRows_rsProducts = mysql_num_rows($rsProducts); <form id="form1" name="form1" method="post" action=""> <table border="0" align="center" cellpadding="4" cellspacing="0"> <tr> <th width="35%"><div align="left">Item:</div></th> <th width="25%"><div align="left">Price:</div></th> <th width="20%">Quantity</th> <th width="20%"># of Days</th> </tr> <?php do { ?> <tr> <td><?php echo $row_rsProducts['prodName']; ?></td> <td><?php echo $row_rsProducts['prodPrice']; ?></td> <td><div align="center"> <input name="Quantity" type="text" id="Quantity"/> </div></td> <td><div align="center"> <input name="NumberOfDays" type="text" id="NumberOfDays"/> </div></td> </tr> <?php } while ($row_rsProducts = mysql_fetch_assoc($rsProducts)); ?> </table> <table border="0" align="center" cellpadding="10" cellspacing="0"> <tr> <td><input type="submit" value="Send Order" /></td> <td><input name="Clear" type="reset" id="Clear" value="Clear Form" /></td> </tr> </table> </form> Thanks Dave Quote Link to comment Share on other sites More sharing options...
craygo Posted June 9, 2008 Share Posted June 9, 2008 to do it you need to use an array for your 2 fields, and to determine which it belongs to you can use the product id as the key for the array. Also I wouldn't use a do while statement, really no need to. <?php mysql_select_db($database_dbPC, $dbPC); $query_rsProducts = "SELECT * FROM products"; $rsProducts = mysql_query($query_rsProducts, $dbPC) or die(mysql_error()); $row_rsProducts = mysql_fetch_assoc($rsProducts); $totalRows_rsProducts = mysql_num_rows($rsProducts); ?> <form id="form1" name="form1" method="post" action=""> <table border="0" align="center" cellpadding="4" cellspacing="0"> <tr> <th width="35%"><div align="left">Item:</div></th> <th width="25%"><div align="left">Price:</div></th> <th width="20%">Quantity</th> <th width="20%"># of Days</th> </tr> <?php while ($row_rsProducts = mysql_fetch_assoc($rsProducts)){ $id = $row_rsProducts['id']; // change this to the field name of your id ?> <tr> <td><?php echo $row_rsProducts['prodName']; ?></td> <td><?php echo $row_rsProducts['prodPrice']; ?></td> <td><div align="center"> <input name="Quantity[<?php echo $id; ?>]" type="text" id="Quantity"/> </div></td> <td><div align="center"> <input name="NumberOfDays[<?php echo $id; ?>]" type="text" id="NumberOfDays"/> </div></td> </tr> <?php } ?> </table> <table border="0" align="center" cellpadding="10" cellspacing="0"> <tr> <td><input type="submit" value="Send Order" /></td> <td><input name="Clear" type="reset" id="Clear" value="Clear Form" /></td> </tr> </table> </form> Ray Quote Link to comment Share on other sites More sharing options...
aseaofflames Posted June 9, 2008 Share Posted June 9, 2008 if you mysql table has an id property <?php $id = $row_rsProducts['id']; ?> <td><div align="center"> <input name="Quantity<?php echo $id; ?>" type="text" id="Quantity<?php echo $id; ?>" value="0" onchange="subtotal();"/> </div></td> <td><div align="center"> <input name="NumberOfDays<?php echo $id; ?>" type="text" id="NumberOfDays<?php echo $id; ?>" value="0" onchange="subtotal();"/> <input type="hidden" name="Price<?php echo $id; ?>" id="Price<?php echo $id; ?>" value="<?php echo $row_rsProducts['prodPrice']; ?>"> </div></td> then for subtotals you could use javascript: add to head: <script type="text/javascript"> <!-- function subtotal() { var subtotal = 0; //set subtotal equal to 0; for(i=0;i<document.form1.elements;i++) { //loop through all elements in form1 if(document.form1.element[i].name.indexOf("Price")!==-1) { //if the element's name contains Price, use it var id = parseFloat(document.form1.element[i].name); //get the elements id var price = parseFloat(document.getElementById('Quantity'+id).value) * parseFloat(document.getElementById('Price' + id).value); //multiply the price by the quantity subtotal = subtotal + price; //add the price to the subtotal } } document.getElementById('subtotal').innerHTML = "Subtotal: "+subtotal; //display the subtotal in <span id="subtotal"></span> } --> </script> add <span id="subtotal"></span> wherever you want to subtotal to appear haven't tested this, hope it works Quote Link to comment Share on other sites More sharing options...
dmschenk Posted June 12, 2008 Author Share Posted June 12, 2008 I'm think I may be all messed up... these methods seem to work fine but now I have two more problems and I'm beginning to think that I am going about this the wrong way. is this salvageable? Problem 1.. how do I get the info from the form to an email? Problem 2.. now that I have a unique quantity and days columns for the rental form, is it possible to insert a subtotal column that updates when someone fills a row or do I need something else to get a subtotal to show? here a link to the form in its current state so you can see where I'm trying to go http://productioncube.net/orderform2.php Dave Quote Link to comment Share on other sites More sharing options...
aseaofflames Posted June 12, 2008 Share Posted June 12, 2008 how does this work for you? http://backup.aseaofflames.com/order.html i don't have your database, so i just used to first 10 items as an example. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 12, 2008 Share Posted June 12, 2008 Also I wouldn't use a do while statement, really no need to. There is a need for "do..while" when you have already fetched the first row Quote Link to comment Share on other sites More sharing options...
dmschenk Posted June 12, 2008 Author Share Posted June 12, 2008 aseaofflames, That is pretty amazing! can I get the code from you. I tried to copy the (order.htm, confirm.php & send.php) files but I'm getting value of $0 on the confirmation page when I try it away from your site. I don't know what I'm missing. can you please send the files to (subscrpt at revelationweb dot com) Thanks so much!! Dave Re: form issues for quantities and more « Reply #4 on: Today at 11:15:46 AM » Quote -------------------------------------------------------------------------------- how does this work for you? http://backup.aseaofflames.com/order.html i don't have your database, so i just used to first 10 items as an example. Quote Link to comment Share on other sites More sharing options...
dmschenk Posted June 13, 2008 Author Share Posted June 13, 2008 Hi Barand Not sure I understand just exactly why the "do while" statement is or isn't required with the first row fetched. any chance you can explain it? Posted by: Barand There is a need for "do..while" when you have already fetched the first row Quote Link to comment Share on other sites More sharing options...
Barand Posted June 13, 2008 Share Posted June 13, 2008 if you have this code the first record is ignored as you fetch the next before printing $row = mysql_fetch_row($res); // read first while ($row = mysql_fetch_row($res)) // read next { echo $row[0]; // print it } whereas this read and prints all $row = mysql_fetch_row($res); // read first do { echo $row[0]; // print it } while ($row = mysql_fetch_row($res)); // read next The first while loop is fine if the first hasn't been read, or you want to ignore it for some reason. Quote Link to comment Share on other sites More sharing options...
aseaofflames Posted June 13, 2008 Share Posted June 13, 2008 files: http://www.draftingdropbox.com/site/?f=1213390298 note, you need to add a mysql connection to each of the files. you might need to debug the files a little, because i can't test the code. Quote Link to comment Share on other sites More sharing options...
dmschenk Posted June 16, 2008 Author Share Posted June 16, 2008 I'm getting closer but I'm running into a debug issue and think it may be due to version differences of PHP or MySQL??? The code to put the subtotals works very well ... Thank you! The totals however always end up "TOTAL = $NaN" on the orderform page When the page it submitted to the confirm page the TOTAL comes out correctly. Finally when the send order button is pressed the TOTAL value goes to $0. I'm guessing that it is all related to the $NaN issue but I'm not familiar with debugging enough to know the truth. Dave $NaE Quote Link to comment Share on other sites More sharing options...
aseaofflames Posted June 22, 2008 Share Posted June 22, 2008 i changed a couple things: http://www.draftingdropbox.com/site/?f=1214099897 test it for me 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.