Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need Help with PHP Calculate Program.

    I wrote a PHP script to calculate two numbers. The form displays correctly and when it tries to calculate, for some reason it just see's the first number. Attached below is my code. Can anyone tell me what I am doing wrong?

    Thanks,

    Rob



    <HTML>
    <H1><B>The Calculator Application</B></H1>
    </HTML>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <input type="text" name="first_number" value=""/> + <input type="text" name="seccond_number" value="" /> = <?=$number?><br />

    <input type="submit" name="add" value="Calculate" />

    </form>

    <?php

    if (isset ($_POST ['add'])) {
    $first_number = $_POST['first_number'];
    $second_number = $_POST['second_number'];

    $number = $first_number + $second_number;
    echo $_POST['number'];
    echo "<br>";
    echo $number;

    }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by splendid View Post
    I wrote a PHP script to calculate two numbers. The form displays correctly and when it tries to calculate, for some reason it just see's the first number. Attached below is my code. Can anyone tell me what I am doing wrong?

    Thanks,

    Rob



    <HTML>
    <H1><B>The Calculator Application</B></H1>
    </HTML>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    <input type="text" name="first_number" value=""/> + <input type="text" name="seccond_number" value="" /> = <?=$number?><br />

    <input type="submit" name="add" value="Calculate" />

    </form>

    <?php

    if (isset ($_POST ['add'])) {
    $first_number = $_POST['first_number'];
    $second_number = $_POST['second_number'];

    $number = $first_number + $second_number;
    echo $_POST['number'];
    echo "<br>";
    echo $number;

    }
    ?>
    <input type="text" name="seccond_number" value="" /> versus $second_number = $_POST['second_number'];. One of those two has to change (chances are its you're html input you're going to change).

    Also, <?= is not recommended since is uses an echo off of a short_tags directive which is an option that can be disabled. PHP_SELF is also XSS exploitable, so that should be changed. In this case, try using <?php echo $_SERVER['SCRIPT_NAME'];?> instead of <?=$_SERVER['PHP_SELF']?>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    splendid (05-02-2009)

  • #3
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Just browsing through, that typo might be your problem:
    name="seccond_number"

  • Users who have thanked venegal for this post:

    splendid (05-02-2009)

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Folks,

    I can't thank you enough. I did not see the typo because it wrapped to another line. I used all of your suggestions, and again really appreciate your help! Everything works perfectly now!!!


    Rob

  • #5
    New to the CF scene
    Join Date
    May 2009
    Location
    Alabama
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    A soloution to prevent typos

    I find that it is often easier to use the extract method, as it prevents typos when using super global arrays.


    rather than doing this....

    $first_number = $_POST['first_number'];
    $second_number = $_POST['second_number'];

    you can replace with
    extract($_POST) ;

    By extracting the $_POST superglobal array, you can simply write $first_number, and $second_number , and do not need to define them on a seperate line of code, as $first_name is defined from the form's first_name field....

    It's less typing too...

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Thats a great way to have you're variables overwritten.

    Default handling of extract is to replace current variables. Extract may be simplier to use, but it is only slightly better than using register_globals. Extract should NEVER be used on any user submitted data. Ever.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    sea4me (05-02-2009)

  • #7
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Quote Originally Posted by Fou-Lu View Post
    Thats a great way to have you're variables overwritten.

    Default handling of extract is to replace current variables. Extract may be simplier to use, but it is only slightly better than using register_globals. Extract should NEVER be used on any user submitted data. Ever.
    I learned something today...

    I didn't think of overwritting the vars intill you mentioned it...

    (I was gonna use extract forever without you telling how bad it was)

    Nicely said!!!


  •  

    Posting Permissions

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