473,773 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning current row index and applying to string javascript problem

129 New Member
Here is the situation i am currently trying to update a sales order using PHP and javascript. When you click on the edit button it brings you a new site page with existing sales order details within. I am wanting the order lines to be within option boxes so they can modify the order if they wish. I created some javascript for the create sale site page which automatically assigned a price to a particular product once selected. Here is the code for this;

Expand|Select|Wrap|Line Numbers
  1.  $(document).ready(function() {
  2.  
  3.      $("#product_select > option").click(function () {
  4.         var selectvalue = $('#product_select').val();
  5.         $.post("<?php echo SITE_URL; ?>sale/ajax",
  6.         { id: selectvalue },
  7.         function(return_id)
  8.         {
  9.             var return_array = return_id.split(",");
  10.             $("#price").val(return_array[0]);
  11.             if (return_array[1] == 1) {
  12.                 $('#duration').removeAttr("disabled"); 
  13.                 $('#duration').attr('enabled', 'enabled');
  14.                 $('#duration').val('0');
  15.             } else {
  16.                 $('#duration').removeAttr("enabled"); 
  17.                 $('#duration').attr('disabled', 'disabled');
  18.                 $('#duration').val('0');
  19.             }
  20.         });
  21.     });
  22.  
  23.  });
  24.  
  25. </script>
Here i am pretty much posting the product_id to the site ajax and returning the price into the unit_price input on the site. This works fine. However i am wanting a similar procedure but if they change the order lines on the edit form. The form is setup as follows;

Expand|Select|Wrap|Line Numbers
  1. <div id="main">
  2.  
  3.     <h2><i><u>Order Header Details</u></i></h2><br />
  4.  
  5.     <table id="sale" border='1' style="border-style: outset; empty-cells: show">
  6.         <tr>
  7.             <th>Order No</th>
  8.             <th>Date</th>
  9.             <th>Account</th>
  10.             <th>Amount</th>
  11.             <th>Payment Method</th>
  12.             <th>Sales Rep</th>
  13.         </tr>
  14.             <tr>
  15.                 <td><strong><?php echo $order->order_id; ?></strong></td>
  16.                 <td><?php echo $order->date_added; ?></td>
  17.                 <td><?php echo $order->company_name; ?></td>
  18.                 <td>&pound;<?php echo number_format($order->sales_total,2) ?></td>
  19.                 <td><?php echo $order->payment_option; ?></td>
  20.                 <td><?php echo $order->rep_name; ?></td>
  21.             </tr>
  22.     </table>
  23.  
  24.     <h2><i><u>Order Line Details</u></i></h2><br />
  25.  
  26.     <table id="basket" border='1' style="border-style: outset; empty-cells: show">
  27.         <tr>
  28.             <th>Item type</th>
  29.             <th>Price (&pound;)</th>
  30.             <th>per</th>
  31.             <th>Duration</th>
  32.             <th>Update</th>
  33.             <th>Delete</th>
  34.         </tr>
  35.         <?php
  36.         //$i = 0;
  37.         foreach ($basket as $basket) {
  38.         ?>
  39.         <tbody>
  40.         <tr class="line edit_item">
  41.             <td class="product">
  42.             <select class="product_select" id="product_select<?php echo $i; ?>" name="product">
  43.                 <?php foreach ($products as $product) { ?>
  44.                 <option value="<?php echo $product->product_id; ?>" <?php if ($product->product_id == $basket->product_id) { echo 'selected="selected"'; } ?>><?php echo $product->product_name; ?></option>
  45.                 <?php } ?>
  46.             </select>
  47.             </td>
  48.             <td class="price">
  49.                 <input name="price" class="price" id="price<?php echo $i; ?>" type="text" value="<?php echo $basket->unit_price; ?>" size="5" />
  50.             </td>
  51.             <td class="per">&nbsp;</td>
  52.             <td class="duration">
  53.                 <select name="duration" class="duration id="duration<?php echo $i; ?>">
  54.                     <option value="0">(Select)</option>
  55.                     <option value="1">1 Month</option>
  56.                     <option value="3">3 Months</option>
  57.                     <option value="6">6 Months</option>
  58.                     <option value="12">1 Year</option>
  59.                 </select>
  60.             </td>
  61.             <td class="controls"><button type="button" id="update">Update</button></td>
  62.             <td class="controls"><button type="button" id="delete">Delete</button></td>
  63.         </tr>
  64.         <?php
  65.         //$i++;
  66.         } ?>
  67.         </tbody>
  68.     </table>
  69. </div>
As you can see i am using PHP syntax for various objects including the product_select option, price input and duration options. On this form page you can have many order lines. Using the create sales javascript does work for the editing of order lines but for the first record only, i realise it is due to my poor javascripting skills. I have only been learning it for 3 days.

My theory for the code is that i require the row.index of each order line and passing that value through to the javascript however i have tried many times to find and get the current row.index syntax to work. I don't know what i am doing wrong. Here is the code i have been looking at;

Expand|Select|Wrap|Line Numbers
  1. onload = function() {
  2.     if (!document.getElementsByTagName || !document.createTextNode) return;
  3.     var rows = document.getElementById('my_table').getElementsByTagName('tbody')[0].getElementsByTagName('tr');
  4.     for (i = 0; i < rows.length; i++) {
  5.         rows[i].onclick = function() {
  6.             alert(this.rowIndex + 1);
  7.         }
  8.     }
  9. }
  10.  
All i am wanting is if users change the order line details via the product_select or duration options then to update the price field via the javascript code.

Any advice or views on how or what i should do to get this code working would be greatly appreciated. Thanks in advance.
Aug 27 '09 #1
1 5903
gits
5,390 Recognized Expert Moderator Expert
when you assign events with a loop and you need to preserve the index then you would need to use a so called closure ... which basicly is a function that stores the value you want to preserve (closure) in it. let me give you a short example:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <script type="text/javascript">
  4.  
  5.     function attachClickEvents(doc) {
  6.         var ele = doc.getElementsByTagName('div');
  7.  
  8.         for (var i = 0, l = ele.length; i < l; ++i) {
  9.             var n = ele[i];
  10.  
  11.             n.onclick = function(idx) {
  12.                 return function() { alert(idx) }
  13.             }(i);
  14.         }
  15.     }
  16.  
  17.     </script>
  18. </head>
  19. <body onload="attachClickEvents(document);">
  20.     <div>foo</div>
  21.     <div>foo1</div>
  22.     <div>foo2</div>
  23.     <div>foo3</div>
  24.     <div>foo4</div>
  25.     <div>foo5</div>
  26.     <div>foo6</div>
  27.     <div>foo7</div>
  28.     <div>foo8</div>
  29.     <div>foo9</div>
  30.     <div>foo10</div>
  31.     <div>foo11</div>
  32.     <div>foo12</div>
  33.     <div>foo13</div>
  34.     <div>foo14</div>
  35.     <div>foo15</div>
  36.     <div>foo16</div>
  37.     <div>foo17</div>
  38. </body>
  39. </html>
  40.  
here you see that the index i is preserved by using the closure. when you instead use:

Expand|Select|Wrap|Line Numbers
  1. function attachClickEvents(doc) {
  2.     var ele = doc.getElementsByTagName('div');
  3.  
  4.     for (var i = 0, l = ele.length; i < l; ++i) {
  5.         var n = ele[i];
  6.  
  7.         n.onclick = function() {
  8.             alert(i);
  9.         };
  10.     }
  11. }
  12.  
then you will always get the last index returned ... since it just alerts the value that i had assigned after the complete loop.

kind regards
Aug 30 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3626
by: salty_dogs | last post by:
thanks to all who helped steer me away from 'slicing' photoshop images to positioning with css. new question: i have a web page that has a navbar, simple text links. each of these links will eventually load a series of thumbnail images. clicking on the thumbnails will show a larger version in an area to the right. although the page isn't really functional yet, you can view it at www.momeara.com.
3
2668
by: Kevin Gibbons | last post by:
Hello all, I've browsed through past usenet archives, but can't seem to come across quite the javascript I'm looking for. I'm looking for a simple javascript that will display the date as such: May 17 So basically, just displaying the current month and the current date. But I would also like the ability to backdate by one day, two days,
4
2117
by: Sandy Bremmer | last post by:
I am wondering if the following can be accomplished with javascript (and if so, if you think javascript is an appropriate solution). I'm afraid my javascript skills lack. I need to build a hyperlink that includes the current web page URI. When clicked, the current page is then processed by a program. For example, it would look something like this: <a href="http://some.program.com/index.php?current-web-page-URI">text etc.</a>
10
3171
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
7
1612
by: sienaman | last post by:
I have a C# dll with a COM interface that is successfully call by a C++ client. One of the parameters is a string, the method looks like void Goofy(string strUserInput, out string strOutput); I make an assignment in the C# COM object to strOutput like strOutput = some system.string but the string is never returned to the C++ client, it always returns "". I tried setting strOutput to a stack value like strOutput = "Ge"; and the
0
1490
by: GFro | last post by:
I have a calendar page that returns a date to a textbox on the parent page. It is returning the wrong format on the deployment server. On the development server the calendar returns to textbox in the mm/dd/yyyy format. I created a deployment package and installed the program on another server and it also returned the correct mm/dd/yyyy format. Recently I got complaints saying that the format changed to mm-dd-yyyy format. This...
8
2084
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling: "getProductByID(productID)" from inside another method in the same class. See below. This line throws a null ref exception: While dr.Read() Thanks for any insight!
5
19600
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
8
5213
by: Mateusz Viste | last post by:
Hi, I'm not sure if my question is really related to JavaScript, so please excuse me if that's not the case (and maybe you guys would have an idea what's the cause is and where could I ask)... I recently put the following script online: <script type="text/javascript"><!-- function playmedia(mediafile) { newwindow=window.open(); if (window.focus) {newwindow.focus()} newwindow.document.write('<html>');
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.