javascript - Display a value from database based on Drop down list selection -
i have 1 order form. in it, need have button can add row dynamically, user can add more 1 item in form. need have real time calculation in text field, can calculate sub total , total amount without using calculate button. lastly, need have selection box selects item name database, , displays item price in text field when user selects item name. have tried coding , gives me nothing. 1 more thing: how should save data added row database? order form shown below.
this orderform.php coded
<html> <head> <script language="javascript"> function addrow(tableid) { var table = document.getelementbyid(tableid); var rowcount = table.rows.length; var row = table.insertrow(rowcount); var colcount = table.rows[0].cells.length; for(var i=0; i<colcount; i++) { var newcell = row.insertcell(i); newcell.innerhtml = table.rows[0].cells[i].innerhtml; alert(newcell.childnodes); switch(newcell.childnodes[0].type) { case "text": newcell.childnodes[0].value =""; break; case "checkbox": newcell.childnodes[0].checked = false; break; case "select-one": newcell.childnodes[0].selectedindex = 0; break; } } } function deleterow(tableid) { try { var table = document.getelementbyid(tableid); var rowcount = table.rows.length; for(var i=0; i<rowcount; i++) { var row = table.rows[i]; var chkbox = row.cells[0].childnodes[0]; if(null != chkbox && true == chkbox.checked) { if(rowcount <= 1) { alert("cannot delete rows."); break; } table.deleterow(i); rowcount--; i--; } } }catch(e) { alert(e); } } function domath() { var itemprice=parsefloat(document.getelementbyid('itemprice').value); var itemquantity=parsefloat(document.getelementbyid('itemquantity').value); var deliveryfee=parsefloat(document.getelementbyid('deliveryfee').value); var subtotal=parsefloat(document.getelementbyid('subtotal').value); deliveryfee=5.00; var subtotal=(itemprice +0)*(itemquantity +0); var total=subtotal + deliveryfee; var totalamount= 0 + total; document.getelementbyid('subtotal').value=subtotal; document.getelementbyid('totalamount').value=totalamount; } <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>customer order form</title> </head> <body> <center> <form method="post" action="index.php?a=12" name="frm" id="frm"> <table width="800" cellspacing="0" cellpadding="10" style="border: 1px solid #000;" align="center" > <tr> <td align="center" bgcolor="#0099ff"><h1>order form</h1></td> </tr> <tr> <td> <p> </p><table width="819" align="center" bgcolor="#999999" style="border: 0px solid #000;" id="table1"> <tr> <td colspan="7" bgcolor="#0099ff" align="center">order details<br /></td> </tr> <tr> <td width="91">order no</td> <td width="149"><input type="text" name="orderno" id="order_no" /></td> <td width="168" align="right">date</td> <td width="182"><input type="date" name="order_date" id="date" /></td> </tr> <tr> <td colspan="7" bgcolor="#0099ff" align="center">customer details</td> </tr> <tr> <td>customer name</td> <td><input type="text" name="customer_name" id="customer_name" /></td> <td align="right">contact no </td> <td><input type="text" name="customer_cont" id="contact_no" /></td> </tr> <tr> <td>address</td> <td><textarea rows="5" cols="20" name="customer_add" id="address"></textarea></td> </tr> <tr> <td colspan="7" bgcolor="#0099ff" align="center">item details</td> </tr> <tr> <td></td> <td align="center">item name</td> <td>unit price (rm)</td> <td>quantity</td> <td width="195">sub total (rm)</td> </tr> </table> <table id="datatable" width="819" bgcolor="#999999"> <tr> <td width="87"><input type="checkbox" name="chk"/></td> <td width="153"> <select name="itemname" id="item_name"/> <option value="">--- select ---</option> <?php include("db.php"); $select="cbms"; if(isset($select)&&$select!="") { $select=$_post['itemname']; } $list=mysql_query("select * item order itemname asc"); while($row_list=mysql_fetch_assoc($list)) { ?> <option value="<?php echo $row_list['item_id']; ?><?php echo $row_list['itemprice']; ?>"> <?php echo $row_list['itemname']; ?> </option> <?php } ?> </select> </td> <td width="175"><input type="text" name="itemprice" id="itemprice" onchange="domath();" /></td> <td width="182"><input type="text" name="itemquantity" id="itemquantity" onchange="domath();" /></td> <td width="198"><input type="text" name="subtotal" id="subtotal" onchange="domath()"/></td> </tr> </table> <table width="819" bgcolor="#999999"> <tr> <td colspan="7" bgcolor="#0099ff" align="center"> </td> </tr> <tr> <td width="134"><input type="submit" name="save order" value="save order" /></td> <td width="112"> <input type="button" value="add row" onclick="addrow('datatable')" /></td> <td width="136"> <input type="button" value="delete row" onclick="deleterow('datatable')" /></td> <td width="215" align="right">delivery fee (rm)</td> <td width="198" align="left"><input type="text" name="deliveryfee" value="5.00" id="deliveryfee" onchange="domath();"/></td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td align="right">total amount (rm)</td> <td align="left"><input type="text" name="totalamount" id="totalamount" onblur="domath()"/></td> </tr> <tr> <td colspan="7" bgcolor="#0099ff" align="center"> </td> </tr> </table> <p> </p></td> </tr> </table> </form> <p> </p> </center> </body> </html> i sorry if coding long, need help.
i think problem on domath() function.
when create row give same "id" elements. try create different id every time add row.
Comments
Post a Comment