simple calculator code

Discussion in 'HTML & Website Design' started by swollenpickles, Nov 1, 2006.

  1. #1
    Hi
    I'm trying to figure out how to do a simple calculator code. What I want to end up with is something where users enter two figures hit a button and then the result is displayed as out put.

    For example, the calculation I want it to do is:

    x divided by y mulitplied by 1000

    How do I do this??:confused:
     
    swollenpickles, Nov 1, 2006 IP
  2. Corey Bryant

    Corey Bryant Texan at Heart

    Messages:
    1,126
    Likes Received:
    51
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might check out Calculate for an example.
     
    Corey Bryant, Nov 2, 2006 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    If you have php on your server you can do it very easily.

    
    <form action="" method="POST">
    Enter X: <input type="text" name="x">
    Enter Y: <input type="text" name="y">
    <input type="submit" name="submit" value="Calculate">
    </form>
    
    <?PHP 
    if(isset($_POST['submit'])){
    $x = $_POST['x'];
    $y = $_POST['y'];
    $final = ($x/$y)*1000;
    echo $final;
    }
    ?>
    
    Code (markup):
     
    jestep, Nov 2, 2006 IP
  4. swollenpickles

    swollenpickles Active Member

    Messages:
    1,271
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Cool, that all works until I hit 'submit' and then I don't see the result?:confused:
     
    swollenpickles, Nov 2, 2006 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Try changing the main form to:
    
    <form action="<?PHP echo $PHP_SELF; ?>" method="POST">
    
    Code (markup):
     
    jestep, Nov 2, 2006 IP
  6. swollenpickles

    swollenpickles Active Member

    Messages:
    1,271
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    80
    #6
    When i click calculate now it goes to load a new page and then says The page cannot be displayed?
    This is what I have so far:

    <form action="<?PHP echo $PHP_SELF; ?>" method="POST">
    Enter X: <input type="text" name="x">
    Enter Y: <input type="text" name="y">
    <input type="submit" name="submit" value="Calculate">
    </form>

    <?PHP
    if(isset($_POST['submit'])){
    $x = $_POST['x'];
    $y = $_POST['y'];
    $final = ($x/$y)*1000;
    echo $final;
    }
    ?>
     
    swollenpickles, Nov 2, 2006 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Hmm. I just tested it and it works perfectly on my server.

    Can you make a phpinfo file?

    Make a file and put this as the only content in it. Upload it and then load that page.

    
    <?PHP
    phpinfo();
    ?>
    
    Code (markup):
     
    jestep, Nov 2, 2006 IP
  8. swollenpickles

    swollenpickles Active Member

    Messages:
    1,271
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    80
    #8
    Ok, I think I know why it's not working for me. I just did it in notepad and was opening it in explorer from my hard drive. I haven't put it on a server or anything. :eek:
     
    swollenpickles, Nov 2, 2006 IP
  9. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #9
    Oh, that would probably do it. Let me know if it works on the server.
     
    jestep, Nov 2, 2006 IP
  10. appraisal57

    appraisal57 Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Great info! Thanks
     
    appraisal57, Nov 8, 2006 IP