web_master Posted August 25, 2011 Share Posted August 25, 2011 hi, I got a problem, I want to multiply two numbers "on the fly" with ajax. There is a two forms, the firs form is the quantity and the second is the price. Example: <input type="text" id="quantity_01" name="quantity_01" value="" /><input type="text" id="price_01" name="price_01" value="" /> <input type="text" id="quantity_02" name="quantity_02" value="" /><input type="text" id="price_02" name="price_02" value="" /> <input type="text" id="quantity_03" name="quantity_03" value="" /><input type="text" id="price_03" name="price_03" value="" /> ... so, after when I input the quantity_01 and price_01 value, I want to see the multiply summary of those two values (exmpl. 10 * 15,00 = 150,00), without any reload. I'm a newbee in Ajax, so, please, give me some script, how can I do this? In advanced thnxs T Quote Link to comment https://forums.phpfreaks.com/topic/245677-multiply-two-numbers-on-the-fly-with-ajax/ Share on other sites More sharing options...
trq Posted August 25, 2011 Share Posted August 25, 2011 Why would you use Ajax for this? You can multiply numbers client side using JavaScript. Do you know what Ajax is? Quote Link to comment https://forums.phpfreaks.com/topic/245677-multiply-two-numbers-on-the-fly-with-ajax/#findComment-1261815 Share on other sites More sharing options...
web_master Posted August 25, 2011 Author Share Posted August 25, 2011 Why would you use Ajax for this? You can multiply numbers client side using JavaScript. Do you know what Ajax is? Ok, as I know the Ajax is a javascript - but I don't know the real differences... so if I can solve my problem with javascript, than I need the javascript script ... thnxs Quote Link to comment https://forums.phpfreaks.com/topic/245677-multiply-two-numbers-on-the-fly-with-ajax/#findComment-1261821 Share on other sites More sharing options...
web_master Posted August 25, 2011 Author Share Posted August 25, 2011 in meantime, i find a script, but with problem... at first: <head> <script type="text/javascript" src="js/jquery.min.js"></script> </head>... <input type="text" id="quantity 01" name="quantity_01" value="" /> <input type="text" id="price 01" name="price_01" value="" /> <span class="res_01"> </span> <script type="text/javascript"> $('input[id~=quantity], input[id~=price] ').keyup(function() { var id = $(this).attr('id').split(' '); $('.res_'+id[1]).html($('input[id="quantity '+id[1]+'"]').val() * $('input[id="price '+id[1]+'"]').val()); }); </script> the problem is that is not a valid, because of space in id attribute. Is some way to change id attr. to be without space (quantity_01 or quantity-01...)? thnxs Quote Link to comment https://forums.phpfreaks.com/topic/245677-multiply-two-numbers-on-the-fly-with-ajax/#findComment-1261946 Share on other sites More sharing options...
requinix Posted August 25, 2011 Share Posted August 25, 2011 Is some way to change id attr. to be without space (quantity_01 or quantity-01...)? Yes: change the spaces to something else. There are five spaces you need to change in that example you posted. Quote Link to comment https://forums.phpfreaks.com/topic/245677-multiply-two-numbers-on-the-fly-with-ajax/#findComment-1262021 Share on other sites More sharing options...
web_master Posted August 26, 2011 Author Share Posted August 26, 2011 hi, here is a partial solution of my problem: <div class="tarto"> <input type="text" id="kom_1" name="quantity_01" value="" /> <input type="text" id="cena_1" name="price_01" value="" /> <span class="res_01"> </span> </div> <script type="text/javascript"> $('.tarto input').keyup(function() { var id = $(this).attr('id').split('_'); $('.res_'+id[1]).html($('input[id="kom_'+id[1]+'"]').val() * $('input[id="cena_'+id[1]+'"]').val()); }); </script> When I type (keyup) the values in id="kom_1" and id="price_01" I will see the multiple result in <span class="res_01">. - The first problem is that I want to see result only in 2 decimals. - The second problem is: after submit in place <span class="res_01"></span> I want to reload data from database, but if I modify values in "input" form I want to see the typed result... I hope it is clear what I want (my english is not a best). thnxs T Quote Link to comment https://forums.phpfreaks.com/topic/245677-multiply-two-numbers-on-the-fly-with-ajax/#findComment-1262130 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.