Don’t forget to put a unique name in all your input controls together with the form method=”post” because we are going to reference those input controls later on in our php script. The method post will submit all data entered in the form to make it available in other programming language which in our case, we used php.
HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Average Solver | PHP ADDICT</title>
<body>
<form method="post">
<input type="text" name="first" placeholder="First Number"><br>
<input type="text" name="second" placeholder="Second Number"><br>
<input type="text" name="third" placeholder="Third Number"><br>
<input type="text" name="fourth" placeholder="Fourth Number"><br>
<input type="text" name="fifth" placeholder="Fifth Number"><br>
<input type="submit" name="solve" value="Solve">
</form>
</body>
</html>
PHP Code for simple average solver using php
<?php
if (isset($_POST['solve']))
{
$ave=($_POST['first']+$_POST['second']+$_POST['third']+$_POST['fourth']+$_POST['fifth'])/5;
echo "The average of the five numbers is $ave";
}
?>Copy the code above and paste it in your text editor and save it with a file extension of php.
The purpose of the if (isset($_POST['solve'])) is to prevent the browser from executing/reading the php script on form load. Since, we don’t have any entry yet, it will generate an error. SO, to make sure that textboxes have values before submitting, we enclose the code in calculating for the average inside the if statement. if (isset($_POST['solve'])) means that it will execute the code once the button with the solve name is being clicked.
No comments:
Post a Comment