Well friends, I am having a table like this ... ![](/images/imgserve.php?url=http://kigoobe.com/offshoring/bugs/javascript/javascriptAddData.gif)
Where, the left most and right most columns have zero values when the page loads. As we click on the + or - sign associated with the left most column, the value of the left most column increases or decreases by 1. The value of the right most column also changes at the same time, and gives the value of the left most column * the value of the third column, prix.
Here are the two functions that I am using for this. -
function addChoc(str,str2,str3) {
-
chocobox = document.getElementById(str);
-
chocobox.value = Number(chocobox.value)+1;
-
chocoPrice = document.getElementById(str2);
-
chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
-
}
-
function subChoc(str,str2,str3) {
-
chocobox = document.getElementById(str);
-
if (chocobox.value != 0) {
-
chocobox.value = Number(chocobox.value)-1;
-
chocoPrice = document.getElementById(str2);
-
chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
-
-
} else {
-
chocobox.value = chocobox.value;
-
}
-
}
-
where,
str = value of first column, qnty.
str2 = value of last column, total
str3 = value of third column, prix
till now, we are dealing with a single row, and so far, everything is fine, working as it should (I have tested only in FF till now).
The rows of the given table is generated by php, using foreach loop, fetched from mySQL.
*************** *************** *************** *************** *************** ***************
The problem starts now. What I want, is as I click on the + or - sign, along with the two values that are already changing, the value of the last row, right most column should also change (grande totale). And here I am totally lost at this moment ... even after spending several hours pulling out my hairs I was unable to find a solution that works.
Is there anybody who can help me out? :)
Thanks a lot for your time.
15 1724
Some 3 hours later (including my dinner) ... here is what I have got ... -
function addChoc(str,str2,str3,chocArray,id) {
-
chocobox = document.getElementById(str);
-
chocobox.value = Number(chocobox.value)+1;
-
chocoPrice = document.getElementById(str2);
-
chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
-
chocArray[id] = chocoPrice.innerHTML;
-
chocoTotal = document.getElementById('gr_totale');
-
chocoTotal.innerHTML = Number(chocArray[id]);
-
}
-
I am now sending the whole thing in an array and dynamically changing the price. As in the above code, the last line displays the total of a given row by calling it with its id as array key. However, since I am passing the whole array here, I could easily fetch their values and add them to get the desired result.
Say, a javascript equivalent of something like ...
[php]
$total = '';
foreach ($array as $v) {
$total = $total + $v;
}
[/php]
If this works, hopefully the issue will be solved. :) EDIT
The following is giving NaN error. -
function addChoc(str,str2,str3,chocArray,str4) {
-
chocobox = document.getElementById(str);
-
chocobox.value = Number(chocobox.value)+1;
-
chocoPrice = document.getElementById(str2);
-
chocoPrice.innerHTML = Number(str3) * Number(chocobox.value);
-
chocArray[str4] = chocoPrice.innerHTML;
-
chocoTotal = document.getElementById('gr_totale');
-
var total = "";
-
for(i=0;i<chocArray.length;i++) {
-
total = Number(total) + Number(chocArray[i]);
-
}
-
chocoTotal.innerHTML = Number(total);
-
}
-
hmmm .... :(:(:( EDIT 2 @ another hour later ...
solved ... js was not recognizing the $key=>$value type of array as array and was treating them as object ... :) solved now. :)
gits 5,390
Recognized Expert Moderator Expert
hi ...
glad to hear you got it working for yourself ... :) and telling so and even the solution. post back to the forum anytime you have more questions ...
kind regards
Thanks gits. That's a great spirit that I have seen among the moderators of this site. :)
Here is my function at present, and it's doing all what it should do, exactly as I want. -
function subChoc(qnty,totale,prix,chocArray,id,action) {
-
chocobox = document.getElementById(qnty);
-
if ( (action == 'plus') || ((action == 'minus') && (chocobox.value != 0)) ) {
-
if (action == 'plus') {
-
chocobox.value = Number(chocobox.value)+1;
-
} else if (action == 'minus') {
-
chocobox.value = Number(chocobox.value)-1;
-
}
-
chocoPrice = document.getElementById(totale);
-
chocoPrice.innerHTML = Number(prix) * Number(chocobox.value);
-
chocArray[id] = chocobox.value + "~" + chocoPrice.innerHTML;
-
chocoTotal = document.getElementById('gr_totale');
-
var total = 0; var price = 0;
-
for(var i=0;i<chocArray.length;i++) {
-
var splitMe = chocArray[i].split("~");
-
if (Number(splitMe[0]) == 0) {
-
price = 0;
-
} else {
-
price = Number(splitMe[1]);
-
}
-
total = Number(total) + Number(price);
-
}
-
chocoTotal.innerHTML = total;
-
} else {
-
chocobox.value = chocobox.value;
-
}
-
}
-
I am sure that this may not be the perfect script, given that I am new in JS, I hope that in the future I will be able to make similar functions in a more 'pure', 'OO' way.
Now, later down the page, I am calling a function like this => -
getAjax('ajax/getStatus.php','getStatus','text','quantity','product','','');
-
Here in this function, I need to pass two arrays, one array consisting all the ids from the above table as product, and one array consisting of all the quantities (chocobox.value ) including zeroes where the quantities were not altered, as quantity. I am a bit lost about how to construct such arrays and pass that to the above function getAjax.
I would be greatful if someone can help me here. If by that time I found a solution myself, I will be post it back. :)
gits 5,390
Recognized Expert Moderator Expert
hi ...
you want to create the arrays from the table?
kind regards
Hi gits. Thanks for your reply. Actually I need these two arrays so that later I can work on data fetched from this table.
I am not sure how to make the two arrays (php | javascript | ?) and importantly, how to pass the two arrays in the function getAjax.
*************** *************** *************** *************** *************** ***************
Actually, whenever I am calling the function subChoc, I am sending the related id and thereby getting the quantity value. Thus, probably I can have a key value pair with these two in the function itself, and probably I can even make an array there.
Probably the bigger question here is how to pass the array in my getAjax function? !!!
:(:(:(
gits 5,390
Recognized Expert Moderator Expert
no problem :) ... we have to retrieve the nodes of the table and store the value in the corresponding arrays ... so could you please post your current table-html-code, so that we could see how to parse the table?
the alternative with the subChoc method is good ... we would have to store the used ids and values and fill the arrays with ids and zeros for the unhandled items ...
kind regards
gits 5,390
Recognized Expert Moderator Expert
Probably the bigger question here is how to pass the array in my getAjax function? !!!
when you have an array:
you may simply pass it to a function like this: - function use_array(arr) {
-
alert(arr);
-
}
kind regards
Oh thanks Gits ... you mean that passing an array to a function is alike passing a normal variable !!! Then it's really great. For making the array, I can simply fo with the subChoc() ... would be simpler that way ... :)
You don't know how much you have helped me just now. Thanks a lot. Thanks again Gits. :)
May be one question more here. The said array will be inside the function subChoc. That would mean, I will have to call the function subChoc somewhere inside the function getAjax to transfer the array properly, isn't it?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: HolaGoogle |
last post by:
Hi all,
Please help me with the following..it's realy urgent and i tried
everything i could and i can't get it work properly!! Thanks in
advance.
Here's what i'm trying to accomplish:
in my form i have a table with 3 rows (3 input text), in which user
can enter values and then clik save. When he clicks the Save button,
i'd like to be able to add the created items to the end of my table
|
by: Aaron Ackerman |
last post by:
I have a bound combobox the appears on a cell within the column of my bound
grid when the user clicks on a cell n(In my vb.net WinForm app).
I am trying to allow the adding of an item to that bound combo by allowing
the user to dynamically type in the new value directly in the combo box
whcih in turn updates the lookup table and then when they have finished
leaves that particular cell on the grid with the correct value.
Seems...
|
by: cjm |
last post by:
I have two problems that I suspect will be bread-and-butter problems for the
more experienced guys on here, but I'm not the greatest with js.
NB: Code snippets at the bottom
The first problem is that after a bit of fiddling I'm getting am 'Object
Expected' error when I click on the Depot dropdown which I can't seem to get
round. The code I had was working OK but then I cut it all out, tidied it
all up, and put it back in and now it...
|
by: Punisher |
last post by:
I'm adding checkbox controls to a table dynamically when the form loads for
the first time. When a postback occurs, the controls are not maintained in
the table. How do I get them to retain without having re-add them again.
|
by: Sileesh |
last post by:
Hi
I have html table and a Button in an Aspx page. I am adding one row with
some textboxes to Html table each time i click on the Button thru Javascript.
Now problem is when when i try to collect the data in the Textboxes which i
added dynamically from server side, i am not able to do . Alos i tried to
bedug, the total no of rows in Html table does not reflect the dynamically
added rows.
| |
by: seanmayhew |
last post by:
I have a form page that that while editing saves the data to an xml doc
before submitting to db. On each page
unload it saves the xmldoc as the user can add multiple items to the
company like product types etc. So for
instance Im adding a fruit company while adding a fruit company I allow
the user to add types of fruit they
carry and display it dynamically using an <asp:table> with image
|
by: Luis Esteban Valencia |
last post by:
Hello
I wrote a program with code behind in C# to add row into table
dynamically and the program worked very well in .Net Framework 1.1. When I
run this program in .Net Framework 2.0 beta version, the program is not
working as in version 1.1. So what is the problem? Microsoft declared that
the version 2.0 is fully backward support, but it is not. Is that the
problem with 2.0 version?
I find out the problem in version 2.0. After...
|
by: Dennis Fazekas |
last post by:
Greetings,
I am creating a web form which will all the user to add an unlimited number
of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to
remove, and a "Save" button. When the user clicks the "Add another email" it
will call a client side JavaScript function, add_email, which will
dynamically add a new set of controls to the webpage using the innerHTML
method. It appears to work perfectly fine in the browser. The...
|
by: srneu71 |
last post by:
I have a project that requires a dynamically generated matrix table.
The table is setup with 4 quadrants (N,S,E,W) with checkboxes to "link"
the data in adjacent quadrants. The table has to be able to grow &
shrink according to the number of items in each quadrant.
I have coded the table as a user control in vb.net. My question is on
how to retrieve the data out of the table on a post back. I have tried
adding check boxes as straight...
|
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...
|
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,...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| | |