-1

PHP Level = beginner

I am trying to write a simple program that displays the number-times of a particular value when placed in the input box. I have tried to use the post method to do this but each time the program is up running and the submit button is selected, it displays the whole code of the php file 'timescalc.php'. I'll like to know what I am doing wrong, although I know that the calculations with the if statements might be wrong.

Heres the code

<DOCTYPE html>
<html>
<head>
<title>Number Times</title>
</head>

<body>

 <h1>Number Times Table Calculator</h1>

<form method="post" action="timescalc.php">
 Enter Number : <input type="text" name="number"> <br>
 <input type="submit" value="submit"/>
</form>

<?php

$number = $_POST['number'];

if ($number == 2, $number ++2)
{
echo $number . ;
}

else if ($number == 3, $number ++3)
{
echo $number . ;
}

else if ($number == 4, $number ++4)
{
echo $number . ;
}

else if ($number == 5, $number ++5)
{
echo $number . ;
}

else 
{   
echo "pick numbers from 2  to 5 only" ;
}

?>

</body>

</html>
6
  • 2
    Make sure your file extension is .php and if you're running this from your computer, making sure PHP is installed and properly configured. 9 times out of 10, that's what it is. .php files are not like .htm files where the browser properly outputs it. A browser doesn't need a "parser", it's built-in. Commented May 7, 2014 at 0:07
  • 1
    And you are running a web server and not accessing the file directly in your browser Commented May 7, 2014 at 0:07
  • @JohnConde so I can't run this program from my browser unless I'm connected to a web server? Commented May 7, 2014 at 0:10
  • Yes. That web server can be on your computer, though. Commented May 7, 2014 at 0:11
  • You're in good hands here, moving on... Commented May 7, 2014 at 0:13

1 Answer 1

0

You have numerous syntax errors in your php code but they are corrected here, you also need a webserver like WAMP SERVER to run php code. Good luck coding!

<DOCTYPE html>
<html>
<head>
<title>Number Times</title>
</head>
<body>

 <h1>Number Times Table Calculator</h1>

<form method="post" action="test.php">
Enter Number : <input type="text" name="number"> <br>
<input type="submit" value="submit"/>
</form>

<?php

$number = $_POST['number'];

if ($number == 2)
{
$number = $number * 2;
echo $number ."." ;
}

else if ($number == 3)
{
$number = $number * 3;
echo $number ."." ;
}

else if ($number == 4)
{
$number = $number * 4;
echo $number ."." ;
}

else if ($number == 5)
{
$number = $number * 5
echo $number ."." ;
}

else 
{   
echo "pick numbers from 2  to 5 only" ;
}

?>

</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.