Question

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>
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top