What's wrong with this code??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What's wrong with this code???

<html> <head> <title>Calculator<title> </head> <body> <?php if(isset('$submit')){ $number1 = $_POST['$number1']; $number2 = $_POST['$number2']; $sum = $number1 + $number2; } echo $sum; ?> <form method = 'POST'> <input type= 'text' name ='number1'>Enter the first Number</input> <input type= 'text' name ='number2'>Enter the second number </input> <button type ='submit' name='submit' value='submit'> Calculate</button> </form>

1st Sep 2017, 5:59 PM
Ifiokobong Akpan
Ifiokobong Akpan - avatar
11 Answers
+ 5
To add to what @Gavin said, I looked at your PHP code, instead of: if(isset('$submit')){ Do it like: if(isset($_POST['submit'])){ (Edit) And these also need to change: $number1 = $_POST['number1']; $number2 = $_POST['number2']; @Gavin, when a <form> element is missing its method attribute the form submits the request to itself, the PHP code will handle the request data, if it sees there's a 'submit' data passed on with the request, in this case the submit button was clicked. The rest of your suggestions were all correct. Thanks @Gavin.
1st Sep 2017, 6:58 PM
Ipang
+ 5
Haha,so funny! One beginner that didn't learn step by step and many one that in many steps given the answers for it! But at last, I learn some knowledge ,thanks a lot for all of you,guys! ;D
6th Sep 2017, 9:06 AM
liuguowang_75
liuguowang_75 - avatar
+ 4
So many wrong things, too many things... -the title tag has no ending tag, instead it's two title tags -there's an isset function that checks for a variable that doesn't exist -there's no action attribute to the form to point where to send the data -the input tags have ending tags with text in between -the button tag has text in between its tags -there's no closing tags for body and html To be honest, when I saw this code, I wanted to kill myself..😧
1st Sep 2017, 6:15 PM
Ghauth Christians
Ghauth Christians - avatar
+ 4
And continue to code, never give up, you will be great.
2nd Sep 2017, 1:31 AM
Edson
Edson - avatar
+ 3
Try this: <html> <head> <title>Calculator</title> </head> <body> <?php if(isset($_POST['number1') && isset($_POST['number2')){ $number1 = $_POST['number1']; $number2 = $_POST['number2']; $sum = $number1 + $number2; echo '<p>The sum of ' . $number1 . ' + ' . $number2 . ' = ' . $sum . '</p>; } ?> <form method = 'POST'> <input type='text' name='number1'/>Enter the first Number <input type='text' name='number2'/>Enter the second number <input type ='submit' value="Calculate" /> </form> </body> </html>
2nd Sep 2017, 1:29 AM
Edson
Edson - avatar
+ 3
Try this: <html> <head> <title>Calculator</title> </head> <body> <?php if(isset($_POST['number1') && isset($_POST['number2')){ $number1 = $_POST['number1']; $number2 = $_POST['number2']; $sum = $number1 + $number2; echo '<p>The sum of ' . $number1 . ' + ' . $number2 . ' = ' . $sum . '</p>; } ?> <form method = 'POST'> <input type='text' name='number1'/>Enter the first Number <input type='text' name='number2'/>Enter the second number <input type ='submit' value="Calculate" /> </form> </body> </html> neither working
5th Sep 2017, 4:54 PM
Ifiokobong Akpan
Ifiokobong Akpan - avatar
+ 3
ipang.. I tried it ND it worked
8th Sep 2017, 9:01 PM
Ifiokobong Akpan
Ifiokobong Akpan - avatar
+ 2
@liuguowang_75, Haha, yeah, funny isn't it? guess it's a helpful community after all :D. So do you have an alternative to help the beginner with? the last one didn't work either, said the asker.
6th Sep 2017, 9:28 AM
Ipang
+ 2
@Ifiokobong Akpan, I think you better practice on a computer rather then on SoloLearn Code Playground, you can install PHP package (contains PHP, Apache, MySql etc.) on your computer. I would recommend you to try: XAMPP https://www.apachefriends.org/download.html If you prefer the alternatives, you can look for similar packages, alternatives to XAMPP in the following links: Alternative To http://alternativeto.net/software/xampp/ Quora https://www.quora.com/What-are-some-XAMPP-alternatives After you decide which one you want, install, and test the PHP code. Good luck!
6th Sep 2017, 10:36 AM
Ipang
+ 2
@Ifiokobong Akpan, did you try on Code Playground or on your computer? well, I'm glad if it worked anyway. Keep learning mate, c u later.
9th Sep 2017, 2:15 AM
Ipang
0
change your input type to "number" not "text"....it will keep concatenating it for you......since you're adding two numbers not string???
11th Oct 2017, 10:21 PM
Awodire Babajide Samuel
Awodire Babajide Samuel - avatar