473,761 Members | 6,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sum the value

ddtpmyra
333 Contributor
There's two text box that has to enter the the numbers from the user and below I want to display the total value the user inputed. How can I sum it up and have it displayed every they enter a number?

Expand|Select|Wrap|Line Numbers
  1. <td><b> Total</b></td><td>
  2. Quantity #1 <Input type = 'text' Value ='0' Name ='qty1' >
  3. Quantity #2 <Input type = 'text' Value ='0' Name ='qty2'>
  4. Grand Total <input type = 'text' value ='<?php qty1 + qty2 ?>' name ='Total' >
  5.  
Or should I use a java script?
Can somebody show me how its done on JAVA technically no knowlege at all. A newbie learning to walk.
May 20 '09 #1
21 2961
hsriat
1,654 Recognized Expert Top Contributor
@ddtpmyra
It will need JavaScript (not Java) dear.
Expand|Select|Wrap|Line Numbers
  1. Quantity #1 <Input type = 'text' Value ='0' Name ='qty1' onkeyup='if ((this.value.match(/^[0-9]+$/) && this.form.qty2.value.match(/^[0-9]+$/)))  this.form.Total.value = this.value + this.form.dty2.value; else this.form.Total.value = "";'>
  2. Quantity #2 <Input type = 'text' Value ='0' Name ='qty2' onkeyup='if ((this.value.match(/^[0-9]+$/) && this.form.qty1.value.match(/^[0-9]+$/)))  this.form.Total.value = this.value + this.form.dty1.value; else this.form.Total.value = "";'>
  3. Grand Total <input type = 'text' value ='' name ='Total' >
  4.  
May 20 '09 #2
dlite922
1,584 Recognized Expert Top Contributor
Hey ddtpmyra,

First you need to understand what PHP, Java, and JavaScript are. Besides languages, they have nothing else in common.

PHP is non-compiled script that is executed and run on the server. The output is plain text (or HTML).

Java is a compiled programming language that stands alone and runs on the computer it is executed on.

JavaScript is a scripting language in a web page that is executed within the user's web browser.

My recommendation for you is to read tutorials. One site that helps you with all of this and much more (and is a site we all basically started with) is www.w3schools.com Start there.

As to your problem, I would do this in JavaScript because it's trivial, quicker (no refresh), and pretty easy.

To get you interested, here's some code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head><title>Calculator</title></head>
  4. <script language="JavaScript" type="text/JavaScript">
  5. function calSum()
  6. {
  7.     var num1 = parseInt(document.getElementById('firstNum').value); 
  8.     var num2 = parseInt(document.getElementById('secNum').value); 
  9.     document.getElementById('result').innerHTML = (num1 + num2);
  10.     return;
  11. }
  12. </script>
  13. <body>
  14. First Number: <input type="text" value="" name="firstNum" id="firstNum" /><br/>
  15. Second Number: <input type="text" value="" name="secNum" id="secNum" /><br/>
  16. <input type="button" value="Calculate Sum" onclick="calSum();" /><br/>
  17. Answer: <span id="result"></span>
  18. </body>
  19. <html>
  20.  
  21.  
put that in a file, name it calculator.html and open it with your web browser.

to change and mess around with it, open it in notepad, change then save it. Refresh your browser to see your changes.

Good Luck!



Dan
May 20 '09 #3
dlite922
1,584 Recognized Expert Top Contributor
If you had trouble getting hsriat's code to work because it concatenated the values instead of summing this, he made the same mistake I did, and you will make by not calling the "parseInt() " function first:

here's the updated, working code:

Expand|Select|Wrap|Line Numbers
  1. <form name="myForm" >
  2. Quantity #1 <Input type = 'text' Value ='0' Name ='qty1' onkeyup='if ((this.value.match(/^[0-9]+$/) && this.form.qty2.value.match(/^[0-9]+$/)))  this.form.Total.value = parseInt(this.value) + parseInt(this.form.qty2.value); else this.form.Total.value = "";'> 
  3. Quantity #2 <Input type = 'text' Value ='0' Name ='qty2' onkeyup='if ((this.value.match(/^[0-9]+$/) && this.form.qty1.value.match(/^[0-9]+$/)))  this.form.Total.value = parseInt(this.value) + parseInt(this.form.qty1.value); else this.form.Total.value = "";'> 
  4. Grand Total <input type = 'text' value ='' name ='Total' > 
  5. </form>
  6.  
  7.  
May 20 '09 #4
hsriat
1,654 Recognized Expert Top Contributor
@dlite922
That usually happens when you think only "." (dot) can be used for concatenation.
Thanks for the rectification. :)

Cheers

[EDIT: And I have the right for making this excuse as we are in PHP forum ;)]
May 20 '09 #5
ddtpmyra
333 Contributor
dlite922,
First thanks for the code it works perfecty fine but I have <form> already on top it all and my button didn't work after adding it? Is this true you cannot have sub <form> inside PHP please correct me if I'm wrong. Or is there a another way to do the automatic computation without using <form>?

thanks!

@dlite922
May 22 '09 #6
dlite922
1,584 Recognized Expert Top Contributor
@ddtpmyra
you don't have to use form. Instead of using "this.form" make sure the element has an "id" attribute and refer to it with "document.getEl ementById("theI Dhere").value"

make sure you spell "getElementById " <-- exactly like that. JavaScript is case sensitive.

Then you're not dependent on <form>

Cheers,






Dan
May 25 '09 #7
ddtpmyra
333 Contributor
Dan,

Thanks for all the help. It works!

DM
May 26 '09 #8
ddtpmyra
333 Contributor
please disregard this post
Sep 24 '09 #9
Dormilich
8,658 Recognized Expert Moderator Expert
@ddtpmyra
this is not a matter of PHP. the HTML standard does not allow <form> inside another <form>.
Sep 25 '09 #10

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

Similar topics

1
14156
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10." The PERL code is as follows:
3
18240
by: otto | last post by:
i need to read a variable in a javascript and translate it to a form in html the javascript variable is: <SCRIPT LANGUAGE='JavaScript'>RF2N('Total');</script> and i need to put that variable as the value in this line <input type="hidden" name="AMT" value="**">
3
11953
by: Eric Chang | last post by:
I was working on this simple form with radio boxes. And when I click on one of the radio box, it tell me the value is "undefined" Why is that ? I did defined the value of each radio box: <input type=radio name='Usetax' value='basic' onClick='document.myform.amount.value=document.myform.Usetax.value'> <input type=radio name='Usetax' value='no' onClick='document.myform.amount.value=document.myform.Usetax.value'> <input type=radio...
16
11501
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not an object... the function is like so: function calc_total() { var x,i,base,margin,total,newmargin,newtotal; base = document.frmKitAmount.txtTotalKitValue.value; margin = document.frmKitAmount.margin.value/100;
4
3019
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email User1@here.whatever and User4@here.whatever but if "Loaded" is chosen it needs to email User2@here.whatever and User3@here.whatever, etc, etc... I'm aware that the only thing that really needs to change is the "Mail.AddAddress" line (at least...
7
5076
by: matthew_carver | last post by:
Hello, I have an ASP page that loops through a SQL Server 2000 table, then downloads an Excel sheet the users can save, etc. Works fine, except, I see that in one particular "comments" field the Excel sheet returns a #VALUE! error in the cell when there is a large amount of text. I've looked through the MSKB, MSDN and many ng posts to see if there is a workaround or solution to this, including looking at the xlWorksheet properties. Is...
13
10145
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the parent table. ----------------------------
0
4096
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL, TYPE VARCHAR(30) NOT NULL, DURATION TIME , YEAR_RELEASE YEAR NOT NULL, DESCRIPTION TEXT,
1
2333
by: cbellew | last post by:
Hi guys, I have a problem with an option group and a two corresponding text boxes. When the user chooses an option value i want the text boxes to populate with text dependent on the choice made. I have written an AfterUpdate procedure on the frame to try and assign the text to the value of the text box, but for some reason it does nothing... Here is the code: Private Sub Frame0_AfterUpdate() If Frame0.Value = 1 Then
275
12370
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
9554
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
10136
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
9988
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9923
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,...
0
9811
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
8813
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
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
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
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.