Hi i have previously posted a similar problem in php section, so appologies for repeating myself. I have narrowed down my problem to just a few lines of code. In a shopping cart style, I display the quatity of items a customer has ordered before in list . one row at a time, in an HTML, <input> form. By placing an "id" tag with the <input> I am able to call the javascript and recall the data the user has inputed, along with the item_numer, and store the new quatity in the databse, for later use, FOR A SINGLE ITEM. However when the items listed is more than one, I have no way of tellign the javascript which item the customer has changed. I guess putting items in an array, and then recalling them, will do the trick, but I am not sure how to do it. here is the revelvant code, snippets...... <?php while( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) { $totalCost += ($row["qty"] * $row["price_each1"]); ?> <tr> <td width="7%" height="25"> <font face="verdana" color="black" align="center"> <form > <input id="t" type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="t" onchange="UpdateQty2(name)" > .............. and javascript one... function UpdateQty2(s) { var itemId=s; var newQty= document.getElementById("t").value; document.location.href = 'cartnew4.php?action=update_item&id='+itemId+'&qty='+newQty; Many thx for taking the time to look at my problem. jacka
Hi all just thought of something. Why won't this work? I am sure it needs some modifications , but what? ........ <input type="text" name="array[<?php echo $row["itemId"];?>]" value="<?php echo $row["qty"];?>" id="t" onchange="UpdateQty2(name)" > .... javascript... function UpdateQty2(s) { var itemId=s; //newQty=b; var t =s; var newQty= document.getElementById'array[name]'.value; document.location.href = 'cartnew5.php?action=update_item&id='+itemId+'&qty='+newQty; } thx jacka
I think you might be better off using AJAX and returning an XML object from a separate .php file that handles the MySQL query. The following is used to dynamically populate a select list, from a MySQL database, using AJAX. Instead of parsing the XML and populating a select list, you can use the data arrays (nText, nVal) to update form fields. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Any Title</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> var AdminResponse = ""; function parseResponse(){ var nText = AdminResponse.getElementsByTagName('optionText'); var nVal = AdminResponse.getElementsByTagName('optionVal'); document.forms[0]['group'].options.length = 1; for (i=0; i<nText.length; i++) { var nOption = document.createElement('option'); var isText = document.createTextNode(nText[i].firstChild.data); nOption.setAttribute('value',nVal[i].firstChild.data); nOption.appendChild(isText); document.forms[0]['group'].appendChild(nOption); } } function update(nVal){ var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); AdminRequest.onreadystatechange = function() { if (AdminRequest.readyState == 4) { if (AdminRequest.status == 200) { AdminResponse = AdminRequest.responseXML; parseResponse(); } else { alert('Error Update.php File '+ AdminRequest.statusText); } } } var infoStr = "?choice="+nVal; AdminRequest.open("GET", "Update.php"+infoStr, true); AdminRequest.send(null); } </script> <style type="text/css"> body {background-color:#eae3c6;margin-top:60px} form {width:430px;margin:auto} fieldset {width:410px;background-color:#f0fff0;border:1px solid #87ceeb} legend {font-family:times;font-size:14pt;color:#00008b;background-color:#87ceeb;padding-left:3px;padding-right:3px;margin-bottom:5px} select {font-family;tahoma;font-size:10pt;width:160px;margin-left:35px;margin-bottom:10px} </style> </head> <body> <form action=""> <fieldset> <legend>Form</legend> <select name="foods" onchange="update(this.value)"> <option value=""> Choose a Food </option> <option value="fruit"> Fruit </option> <option value="vegetable"> Vegetable </option> </select> <select name="group" onchange="alert(this.value)"> <option value=""> Make a selection </option> </select> </fieldset> </form> </body> </html> Code (markup): Update.php <?php $choice = $_GET['choice']; $xml = "<?xml version='1.0' ?><options>"; require_once('mySQL_connect.php'); $query = "SELECT * FROM categories WHERE food = '$choice'"; $result = @mysql_query($query); $num = @mysql_num_rows($result); if ($result && $num > 0) { while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $xml .= "<optionText>" . $row['optText'] . "</optionText><optionVal>" . $row['optVal'] . "</optionVal>"; } $xml .= "</options>"; } @mysql_free_result($result); @mysql_close(); header("Content-Type: text/xml"); echo $xml; ?> Code (markup):
Hi there Many thanks for yoru reply. I can imagine you have put alot of time and effort in this, but the code I need is small part of a big shopping cart coding that I have adopted for myself and is far far too complicated to chnage now when I am so close. I am sure you can slove the problem, its so simple an idea. I have one user input that i successfully transer to javascript. Now when I have more than one input, I need to know which one has data been inputed and pass the new quantity field to javascript, and leave the other ones untouched thanks jacka
Hi Mike The way the program works is that it first gets the quantity of the items that the user has chosen from previous visit, using sessions, from a database. It then lists, for each item, qty price each, price break, part number and the product discription. e.g. QTY price product break/discountpart Number Product discription 22 £6.20 (1-5,3%;6-9.10, etc.etc.) S25-8 sprocket-bossed ..... ..... ..... So that if a user had previously bought , say 20, items, all details of teh products are listed in 20 rows. Now if the user decides to change the quantity of just one item. I wish the new quantity of that particular item to be send to teh javascript for it to be recorded in the databse, total recalculated and the new quantity displayed. Now at this stage all I want to do it send the new quantity with the item_number ( which is taken staright from database) to be sent to the function UpdateQty2(s) javascript. Hope that makes sense. I can update the list only if there is just one row of data but not more than one row. Here is more complete code: </script> </head> <body bgcolor="#ffffff" > <h1><font color="#255699">Your Shopping Cart</font></h1> <h1><font color="#255699"> <?php //$newpb=($_GET["newpb"]); $startnumber=1; $endnumber=5; ?> </font></h1> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tr bgcolor="#255699"> <td width="7%" height="25" > <font face="verdana" size="1" color="white"> <b>Qty</b> </font> </td> <td width="7%" height="25" > <font face="verdana" size="1" color="white"> <b>Update</b> </font> </td> <td width="10%" height="25" > <font face="verdana" size="1" color="white"> <b>Price Each</b> </font> </td> <td width="23%" height="25" > <font face="verdana" size="1" color="white"> <b>Price Break, % Discount </b> </font> </td> <td width="11%" height="25" bgcolor="#255699"> <font face="verdana" size="1" color="white"> <b>Part Number </b> </font> </td> <td width="38%" height="25" bgcolor="#255699"> <font face="verdana" size="1" color="white"> <b>Product Discription </b> </font> </td> <td width="11%" height="25""> <font face="verdana" size="1" color="white"> <b>Remove?</b> </font> </td> </tr> <?php while ($row = mysql_fetch_array($result)) { // Increment the total cost of all items $totalCost += ($row["qty"] * $row["price_each1"]); ?> <tr> <td width="7%" height="25"> <font face="verdana" color="black" align="center"> <form name="getdata" method="GET" action="<?php echo $PHP_SELF;?>"> <input type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="t" onchange="UpdateQty2(name)" > </form> </td> <td> <input type="button"value="Done"> <td width="10%" height="25"> <font face="verdana" size="1" color="black"> £<?php echo number_format($row["price_each1"], 2, ".", ","); ?> </font> </td> <td width="23%" height="25" > <font face="verdana" size="1" color="red"> <select name="pb" onChange="alertselected(this)"> <?php $c=$row["price_each1"];// price for single item for($i = 1; $i <= 8; $i++) { $b=$row["price_each".$i]; $d=(($c-$b)/$c)*100; $complete=$row[("price_break".$i)]. " ," .round($d)."%"; echo "<option "; if(($newpb) == $i) { echo " SELECTED "; } echo ">" . $complete . "</option>"; } ?> </select> </font> </td> <td width="11%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["part_number"]; ?> </font> </td> <td width="38%" height="25"> <font face="verdana" size="1" color="black"> <?php echo $row["disc"]; ?> </font> </td> <td width="11%" height="25"> <font face="verdana" size="1" color="black"> <a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a> </font> </td> </tr> <?php } // Display the total ?> <tr> <td colspan="6"> <hr size="1" color="red" NOSHADE> </td> </tr> <tr> <td colspan="2"> <font face="verdana" size="1" color="black"> <a href="orders.php"><strong><< Keep Shopping</strong></a> </font> </td> <td colspan="2"> <font face="verdana" size="2" color="black"> <b>Total: £<?php echo number_format($totalCost, 2, ".", ","); ?></b> </font> </td> <td align="center" colspan="4"> <a href="../HiQFM2-0/test.php?totalCost=<?php echo number_format($totalCost, 2, ".", ","); ?> "> <strong>Finished Ordering >></strong></a> </td> </tr> </table> </body> </html> <?php } ?> ------------- and the javascript: function UpdateQty2(s) { var itemId=s; //newQty=b; // var t =s; var newQty= document.getElementById("t").value; document.location.href = 'cartnew5.php?action=update_item&id='+itemId+'&qty='+newQty; } There are some more javascript functions and codes, but they r not very relevant. Hope this helps. looking forward to your reply. Thx jacka
Jacka: Could you please use the code tags when posting code? It's the # on the toolbar. Highlite the code and click # What I'm looking for, and what I don't see is where you create all the table rows, if as in your example, there were 25 items in the cart. I see this: <input type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="t" onchange="UpdateQty2(name)" > Code (markup): But I don't see the php going through this array while( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) Code (markup): and creating new <tr><td> tags. If you could show me where in your php you create a 25 row table, I can solve this for you.
Hi My appologies I am very new to forums and the pactices. here is the code: <?php while ($row = mysql_fetch_array($result)) { // Increment the total cost of all items $totalCost += ($row["qty"] * $row["price_each1"]); ?> <tr> <td width="7%" height="25"> <font face="verdana" color="black" align="center"> <form name="getdata" method="GET" action="<?php echo $PHP_SELF;?>"> <input type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="t" onchange="UpdateQty2(name)" > </form> </td> <td> <input type="button"value="Done"> <td width="10%" height="25"> <font face="verdana" size="1" color="black"> £<?php echo number_format($row["price_each1"], 2, ".", ","); ?> </font> </td> <td width="23%" height="25" > <font face="verdana" size="1" color="red"> <select name="pb" onChange="alertselected(this)"> <?php $c=$row["price_each1"];// price for single item for($i = 1; $i <= 8; $i++) { $b=$row["price_each".$i]; $d=(($c-$b)/$c)*100; $complete=$row[("price_break".$i)]. " ," .round($d)."%"; echo "<option "; if(($newpb) == $i) { echo " SELECTED "; } echo ">" . $complete . "</option>"; } ?> Code (markup): The while loop takes the data from a database . thx jacka
I understand what the "while" does in the MySQL, but I don't see where you create more than one table row. I see this: <input type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="t" onchange="UpdateQty2(name)" > and that's it. Where would 25 rows come from???
Could it be that all you are trying to do IS GET THE NAME of that text box? <input type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="t" onblur="UpdateQty2(this.name)" > Code (markup): You can use this.name Is that all you want to do? onchange probably won't work though. It won't work until the page is closed. I'd use onblur.
Hi Mike You obviously can not see the content of teh database. Just suppose there are more than one pice of data in the database. I say 20 just as an argument to highlight the fact that there are more than one row of data in the database. Every time the code returns to get the new data anew table row is created by the html code, until no more data is present. <form name="getdata" method="GET" action="<?php echo $PHP_SELF;?>"> while ($row = mysql_fetch_array($result)) { // Increment the total cost of all items $totalCost += ($row["qty"] * $row["price_each1"]); ?> <tr> <td width="7%" height="25"> <font face="verdana" color="black" align="center"> <input type="text" name="<?php echo $row["itemId"];?>" value="<?php echo $row["qty"];?>" id="<?php echo $row["itemId"];?>" onchange="UpdateQty2(name)" > Code (markup): hope this helps
Hi No sorry I posted my previous message before noticing your last post. You got it. Thats one thing that I couldn't figure out how to do, although it may be very obvious to you. You last posting game me the solution of sending both the variables that I needed, like so.. onblur="UpdateQty2(this.name,this.value)" > Code (markup): That way I got both variables in one go. What I couldn't understand was how to get the value part of the user input to the javascript. Once again, many thanks for taking the time and investigating my problem. You were the only one who bothered to take the time, and thanks for not giving up on me. Cheers mate.