Sum the value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    Sum the value

    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?

    Code:
    <td><b> Total</b></td><td>
    Quantity #1 <Input type = 'text' Value ='0' Name ='qty1' >
    Quantity #2 <Input type = 'text' Value ='0' Name ='qty2'>
    Grand Total <input type = 'text' value ='<?php qty1 + qty2 ?>' name ='Total' >
    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.
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Originally posted by ddtpmyra
    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?

    Or should I use a java script?
    Can somebody show me how its done on JAVA technically no knowlege at all.
    It will need JavaScript (not Java) dear.
    Code:
    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 = "";'>
    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 = "";'>
    Grand Total <input type = 'text' value ='' name ='Total' >

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      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:

      Code:
      <html>
      <head><title>Calculator</title></head>
      <script language="JavaScript" type="text/JavaScript">
      function calSum()
      {
      	var num1 = parseInt(document.getElementById('firstNum').value); 
      	var num2 = parseInt(document.getElementById('secNum').value); 
      	document.getElementById('result').innerHTML = (num1 + num2);
      	return;
      }
      </script>
      <body>
      First Number: <input type="text" value="" name="firstNum" id="firstNum" /><br/>
      Second Number: <input type="text" value="" name="secNum" id="secNum" /><br/>
      <input type="button" value="Calculate Sum" onclick="calSum();" /><br/>
      Answer: <span id="result"></span>
      </body>
      <html>
      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

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        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:

        Code:
        <form name="myForm" >
        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 = "";'> 
        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 = "";'> 
        Grand Total <input type = 'text' value ='' name ='Total' > 
        </form>

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by dlite922
          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:
          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 ;)]

          Comment

          • ddtpmyra
            Contributor
            • Jun 2008
            • 333

            #6
            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!

            Originally posted by dlite922
            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:

            Code:
            <form name="myForm" >
            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 = "";'> 
            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 = "";'> 
            Grand Total <input type = 'text' value ='' name ='Total' > 
            </form>

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              Originally posted by ddtpmyra
              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!
              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

              Comment

              • ddtpmyra
                Contributor
                • Jun 2008
                • 333

                #8
                Dan,

                Thanks for all the help. It works!

                DM

                Comment

                • ddtpmyra
                  Contributor
                  • Jun 2008
                  • 333

                  #9
                  please disregard this post

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by ddtpmyra
                    Is this true you cannot have sub <form> inside PHP please correct me if I'm wrong.
                    this is not a matter of PHP. the HTML standard does not allow <form> inside another <form>.

                    Comment

                    • ddtpmyra
                      Contributor
                      • Jun 2008
                      • 333

                      #11
                      Okay now its getting complicated when I put it inside my PHP. Please tell me what did i wrong and why its not computing?
                      Code:
                      	<?PHP					
                      									$result = mysql_query("select Total from tblEvent_Entity where Cal_ID='{$id}' and Entity ='Organizations' ");
                      									$row = mysql_fetch_assoc($result);
                      									$TotalOrganizations =  $row['Total'];
                      									echo "<tr><td width='50%'>Organizations</td><td width='67%'><Input type = 'text' Name ='totalOrganizations'  id ='totalOrganizations' value='$row[Total]'
                      									onkeyup='if ((document.getElementById('totalOrganizations').match(/^[0-9]+$/) && document.getElementById('totalAgencies').value.match(/^[0-9]+$/)))  
                      									document.getElementById('Total').value =  parseInt(document.getElementById('totalOrganizations').value)) + parseInt(document.getElementById('TotalAgencies').value); 
                      									else document.getElementById('Total').value = '';>  
                      									</td></tr>";
                      									
                      
                      									$result = mysql_query("select Total from tblEvent_Entity where Cal_ID='{$id}' and Entity ='Agencies' ");
                      									$row = mysql_fetch_assoc($result);
                      									$TotalAgencies=  $row['Total'];
                      									echo "<tr><td>Agencies</td><td width='67%'><Input type = 'text' Name ='totalAgencies' id='totalAgencies' value='$row[Total]'
                      									onkeyup='if ((document.getElementById('totalAgencies').match(/^[0-9]+$/) && document.getElementById('totalOrganizations').value.match(/^[0-9]+$/)))  
                      									document.getElementById('Total').value =(document.getElementById('totalOrganizations').value) + parseInt(document.getElementById('totalAgencies').value); 
                      									else document.getElementById('Total').value = '';>  
                      									</td></tr>";	
                      
                      	 								echo "Grand Total <input type = 'text' value ='' name ='Total' id='Total' >  ";?>

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by hsriat
                        [EDIT: And I have the right for making this excuse as we are in PHP forum ;)]
                        What's your excuse now? ;)

                        Comment

                        • ddtpmyra
                          Contributor
                          • Jun 2008
                          • 333

                          #13
                          Sum not calculating

                          I have two input box that I wanted to calculate the Total while the user inputing thier numbers on the text box. On my HTML format its works perfectly but when I applied it inside my PHP code it doesn't work anymore. I appreciate your time looking at this.

                          Code:
                              <?PHP                     
                                                              $result = mysql_query("select Total from tblEvent_Entity where Cal_ID='{$id}' and Entity ='Organizations' "); 
                                                              $row = mysql_fetch_assoc($result); 
                                                              $TotalOrganizations =  $row['Total']; 
                                                              echo "<tr><td width='50%'>Organizations</td><td width='67%'><Input type = 'text' Name ='totalOrganizations'  id ='totalOrganizations' value='$row[Total]' 
                                                              onkeyup='if ((document.getElementById('totalOrganizations').match(/^[0-9]+$/) && document.getElementById('totalAgencies').value.match(/^[0-9]+$/)))   
                                                              document.getElementById('Total').value =  parseInt(document.getElementById('totalOrganizations').value)) + parseInt(document.getElementById('TotalAgencies').value);  
                                                              else document.getElementById('Total').value = '';>   
                                                              </td></tr>"; 
                            
                            
                                                              $result = mysql_query("select Total from tblEvent_Entity where Cal_ID='{$id}' and Entity ='Agencies' "); 
                                                              $row = mysql_fetch_assoc($result); 
                                                              $TotalAgencies=  $row['Total']; 
                                                              echo "<tr><td>Agencies</td><td width='67%'><Input type = 'text' Name ='totalAgencies' id='totalAgencies' value='$row[Total]' 
                                                              onkeyup='if ((document.getElementById('totalAgencies').match(/^[0-9]+$/) && document.getElementById('totalOrganizations').value.match(/^[0-9]+$/)))   
                                                              document.getElementById('Total').value =(document.getElementById('totalOrganizations').value) + parseInt(document.getElementById('totalAgencies').value);  
                                                              else document.getElementById('Total').value = '';>   
                                                              </td></tr>";     
                            
                                                               echo "Grand Total <input type = 'text' value ='' name ='Total' id='Total' >  ";?>

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            Post the client-side generated (HTML) code. Also, remove the unnecessary indents to make the code easier to follow.

                            Do you get any JavaScript errors?

                            Comment

                            • ddtpmyra
                              Contributor
                              • Jun 2008
                              • 333

                              #15
                              Can you show me from my sample code what do you mean? Thanks for your help
                              Originally posted by acoder
                              Post the client-side generated (HTML) code. Also, remove the unnecessary indents to make the code easier to follow.

                              Do you get any JavaScript errors?

                              Comment

                              Working...