SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: PHP Calculator

  1. #1
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Calculator

    Hi

    Im trying to create a form that will add two numbers and input the answer automatically into a text box. Please help.

    Basically, I have created two text boxes called length and width and another called total. So if I entered 5 in width and 50 in Length, I want the answer displayed automatically in Total. How would I do that.

    Appreciate it

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hitman123 View Post
    Hi

    Im trying to create a form that will add two numbers and input the answer automatically into a text box. Please help.

    Basically, I have created two text boxes called length and width and another called total. So if I entered 5 in width and 50 in Length, I want the answer displayed automatically in Total. How would I do that.

    Appreciate it
    I think JavaScript is better for this.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi Bro,

    Use ajax agent for this,
    just google ajax agent and download the zip file ,the code ready to use in it in the examples.

    If you have any problem with this do let me know .

    Noddy

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    989
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Ajax is overkill for this, just use javascript alert(parseInt(box1.value) + parseInt(box2.value));

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't use Ajax. Plain old JavaScript will be just fine.
    Code HTML:
    <html>
    <body>
     
    <form method="post" action="this-fallback-script.php">
    Length <input type="text" name="length" id="length">
    Width <input type="text" name="width" id="width">
    Total <input type="text" readonly="readonly" name="total" id="total">
    <input type="button" name="calculate" id="calculate" value="Calculate">
    </form>
     
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    function calculate() {
      var length = parseFloat(document.getElementById('length').value);
      var width = parseFloat(document.getElementById('width').value);
      document.getElementById('total').value = length + width;
    }
     
    $('#calculate').click(calculate);
    </script> 
     
    </body>
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •