+ 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>
11 Respostas
+ 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.
+ 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
+ 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..đ§
+ 4
And continue to code, never give up, you will be great.
+ 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>
+ 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
+ 3
ipang.. I tried it ND it worked
+ 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.
+ 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!
+ 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.
0
change your input type to "number" not "text"....it will keep concatenating it for you......since you're adding two numbers not string???