Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Need help with coding for a forum that adds numbers


    I need help with coding a page that has two fields so when you put a number in each one and press a button that says add, the output is the answer. almost like its a calculator


    This is the code I have so far:

    <?php

    /*


    */
    ?>

    <!doctype html>
    <html lang="eng">
    <head>
    <meta charset="utf-8">
    <title>Add</title>
    </head>

    <body>
    <?php
    $value1 = $_POST['value1'];
    $value2 = $_POST['value2'];
    ?>

    <body style="background-color:lightgrey;">
    <div id="header">
    <h1>Add</h1>
    <hr>
    </div>

    <div id="content">

    <form name="frmInput" action="add.php" method="post">
    <p>Number 1: <input type="text" name="strUserText" value="" maxlength="100" size="50"></p>
    <p>Number 2: <input type="text" name="strUserText" value="" maxlength="100" size="50"></p>
    <p><input type='submit' name='btnSubmit' value='Add'></p>
    </form>
    </div>



    </body>

    </html>





    I am totally lost please help. much appreciated thnks in advance!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    Hi there,

    Try this:
    PHP Code:
    <?php
    /*
    Name: Brittany Hafer

    */

    if(isset($_POST['value1']))
    {
        
    $num1 intval($_POST['value1']);
        
    $num2 intval($_POST['value2']);

        
    $answer $num1 $num2;
    }
    ?>
    <!doctype html>
    <html lang="eng">
    <head>
    <meta charset="utf-8">
    <title>Add</title>
    </head>

    <body style="background-color:lightgrey;">
    <div id="header">
        <h1>Add</h1>
        <hr />
    </div>

    <div id="content">
        <form name="frmInput" action="add.php" method="post">
            <p>Number 1: <input type="text" name="value1" maxlength="100" size="50"></p>
            <p>Number 2: <input type="text" name="value2" maxlength="100" size="50"></p>
            <p><input type='submit' name='btnSubmit' value='Add'></p>
        </form>

    <?php if(isset($answer)){print($answer);} ?>
    </div>

    <div id="footer">
        <hr />
        <cite>Created for MIS3501, Temple University, February 2013</cite>
    </div>

    </body>

    </html>
    When retrieving the values from an <input> in PHP, you need to use the value of the <input>s 'name' attribute within the $_POST superglobal.

    For example, if you have <input type="text" name="number1" />

    You need to retrieve it like: $_POST['number1'];

    Hope this helps you.

    Kind regards,

    NM.
    Last edited by Nanomech; February 25th, 2013 at 05:50 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    IT did not work :/
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    What is the error you are getting?

    Add the code above the doctype to the add.php file.

    Regards,

    NM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Never Mind, it works! Thank you so much for your kindness and help.


    have a good day!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Sorry to toss a note in at the end here, but if you've some free time, you may be more interested in doing this via Javascript. This will avoid submitting the page every single time, and process it live on the client-side.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Never Mind, it works! Thank you so much for your kindness and help.


    have a good day!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Triple_Nothing
    Sorry to toss a note in at the end here, but if you've some free time, you may be more interested in doing this via Javascript. This will avoid submitting the page every single time, and process it live on the client-side.


    Hey Triple_Nothing- perfectly acceptable question. I found my self asking the same thing, and would prefer to do it that way. However-If it is a school assignment and the instructions said we were not allowed to use javascript -___-
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by Nanomech
    What is the error you are getting?

    Add the code above the doctype to the add.php file.

    Regards,

    NM.
    hey sorry to bother you with a bunch of questions but I have one last one. How can I alter the code so the answer output states the answer but with each number. so if the user puts 1 and 2 and clicks add, the output would be 1 plus 2 = 3
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    PHP Code:
    if(isset($_POST['value1'])) 

        
    $num1 intval($_POST['value1']); 
        
    $num2 intval($_POST['value2']); 

        
    $answer "{$num1} + {$num2} = " strval($num1 $num2); 

    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    Hi Dan that's interesting.

    If we concatenate an int value to a string should we always use the strval() function?

    Kind regards,

    NM.
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Eh. It's not really necessary, but has the same effect:

    PHP Code:
        $answer "{$num1} + {$num2} = " strval($num1 $num2);  
        
    $answer "{$num1} + {$num2} = " . ($num1 $num2);  
        
    $answer "{$num1} + {$num2} = " . (string)($num1 $num2); 
    Those are all the same. However this:

    PHP Code:
        $answer "{$num1} + {$num2} = " $num1 $num2
    Produces different output. Run it and try to find out why:

    PHP Code:
    $num1 3
    $num2 7;

    echo 
    "{$num1} + {$num2} = " strval($num1 $num2); 
    echo 
    "\n"
    echo 
    "{$num1} + {$num2} = " . ($num1 $num2);   
    echo 
    "\n";
    echo 
    "{$num1} + {$num2} = " . (string)($num1 $num2); 
    echo 
    "\n";
    echo  
    "{$num1} + {$num2} = " $num1 $num2
    echo 
    "\n"
    The reason I wrote strval is because OP is new (and hopefully trying to actually learn rather than have us do her homework for her all semester) and strval() is the most explicit way of indicating that I wanted the string value of the sum to concatenate onto the string.

    Comments on this post

    • Nanomech agrees : Thank you for explaining that! I've never even looked at that string on php.net and I've browsed the String functions god knows how many times! n1
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Sorry to be slightly off-topic, but in the echos you defined, ManiacDan, is there reason/use to place the variables within curly brackets? { }
  26. #14
  27. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Sorry to be slightly off-topic, but in the echos you defined, ManiacDan, is there reason/use to place the variables within curly brackets? { }
    In this case, no.

    However, if the variables were arrays or objects, then yes.

    Using the curly brackets makes it more clear that substitution is occurring.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  28. #15
  29. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Omitting the curly braces in this particular instance won't break the code, but curly braces are always recommended when you're using variables. Not only does it make your code explicit, but it also allows code editors to highlight the code better and it prevents you from making a mistake like:

    PHP Code:
    $item "hammer";
    $qty 4;
    echo 
    "You purchased $qty $items"
    Further, curly braces are the only way to get certain kinds of variable interpolation to work properly. You can include $object->calls() inside strings, as well as $complex['array'][$access], both of which are best to enclose in curly braces.

    The manual page on complex string parsing has more.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo