PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 25th, 2013, 05:29 PM
newbie1234 newbie1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 newbie1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 39 sec
Reputation 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!

Reply With Quote
  #2  
Old February 25th, 2013, 05:43 PM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 192 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 7 m 18 sec
Reputation Power: 7
Send a message via Skype to Nanomech
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.
__________________
"WERE NOT WORTHY!"
"WERE NOT WORTHY!"

Last edited by Nanomech : February 25th, 2013 at 05:50 PM.

Reply With Quote
  #3  
Old February 25th, 2013, 06:03 PM
newbie1234 newbie1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 newbie1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 39 sec
Reputation Power: 0
IT did not work :/

Reply With Quote
  #4  
Old February 25th, 2013, 07:01 PM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 192 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 7 m 18 sec
Reputation Power: 7
Send a message via Skype to Nanomech
What is the error you are getting?

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

Regards,

NM.

Reply With Quote
  #5  
Old February 26th, 2013, 08:36 AM
newbie1234 newbie1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 newbie1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 39 sec
Reputation Power: 0
Never Mind, it works! Thank you so much for your kindness and help.


have a good day!

Reply With Quote
  #6  
Old February 26th, 2013, 09:11 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 5 m 6 sec
Reputation Power: 5
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.

Reply With Quote
  #7  
Old February 26th, 2013, 09:41 AM
newbie1234 newbie1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 newbie1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 39 sec
Reputation Power: 0
Never Mind, it works! Thank you so much for your kindness and help.


have a good day!

Reply With Quote
  #8  
Old February 27th, 2013, 10:39 AM
newbie1234 newbie1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 newbie1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 39 sec
Reputation Power: 0
Quote:
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 -___-

Reply With Quote
  #9  
Old February 27th, 2013, 10:41 AM
newbie1234 newbie1234 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 newbie1234 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 39 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #10  
Old February 27th, 2013, 11:20 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
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.

Reply With Quote
  #11  
Old February 28th, 2013, 09:11 AM
Nanomech's Avatar
Nanomech Nanomech is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: The Pleiades
Posts: 192 Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level)Nanomech User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 23 h 7 m 18 sec
Reputation Power: 7
Send a message via Skype to Nanomech
Hi Dan that's interesting.

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

Kind regards,

NM.

Reply With Quote
  #12  
Old February 28th, 2013, 09:41 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
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

Reply With Quote
  #13  
Old February 28th, 2013, 03:58 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 294 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 5 m 6 sec
Reputation Power: 5
Sorry to be slightly off-topic, but in the echos you defined, ManiacDan, is there reason/use to place the variables within curly brackets? { }

Reply With Quote
  #14  
Old February 28th, 2013, 05:55 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,930 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 39 m 37 sec
Reputation Power: 6991
Quote:
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
How to program a basic, secure login system using PHP

Quote:
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

Reply With Quote
  #15  
Old March 1st, 2013, 07:57 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Need help with coding for a forum that adds numbers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap