php HOW To send item on the same page as the form
Good day! I am a newbie in php.. I made a form using html where I can input the grade of the student. Then a php page to know if they pass or not. My problem is how can i send back the reply on the html page. Example how can I send the You Failed status on my html page under the form.
My code is as follows:
<HTML>
开发者_JS百科 <BODY>
Please enter your grade (0-100 only):
<FORM ACTION="grade2.php" METHOD="POST">
<table border = "1">
<tr>
<td>Grade</td>
<td><input type="text" name="grade" /></td>
</tr>
</table>
<INPUT type="submit" name="submit" value="grade">
</BODY>
</HTML>
<?php
$grade = $_POST['grade'];
IF ($grade>=0 && $grade<=50){
print "You failed";
} ELSE IF ($grade<=60){
print "You Passed! But Study Harder!";
} ELSE IF ($grade<=70){
print "Nice";
} ELSE IF ($grade<=80) {
print "Great Job";
} ELSE IF ($grade<=90){
print "Excellent";
} ELSE IF ($grade<=100){
print "God Like!";
} ELSE {
print "Invalid Grade!";
}
?>
Just use one PHP page containing both the form and message. For example (assuming your file is grade.php
)
<form action="grade.php" method="post">
<!-- form elements, etc -->
</form>
<?php
if (isset($_POST['grade'])) {
$grade = (double) $_POST['grade'];
// if statements, print message, etc
}
?>
The only issue I see that may prevent it from showing up now is that the print statements are outside the <body>
tags.
Move
</BODY>
</HTML>
to the very end of the file
There's two ways that you can use.
You can echo the script filename:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Or just leave the action field blank. The page will then re-load on itself.
<form action="" method="post">
At the top of your script, test for the presence of POST variables on the reload:
if (isset($_POST['submit'])) {
And that's it.
If you mean send as in email, use the mail() function.
If you mean just print the result to the screen, use echo.
<?php
$grade = $_POST['grade'];
IF ($grade>=0 && $grade<=50){
echo "You failed";
} ELSE IF ($grade<=60){
echo "You Passed! But Study Harder!";
} ELSE IF ($grade<=70){
echo "Nice";
} ELSE IF ($grade<=80) {
echo "Great Job";
} ELSE IF ($grade<=90){
echo "Excellent";
} ELSE IF ($grade<=100){
echo "God Like!";
} ELSE {
echo "Invalid Grade!";
}
?>
Also, i'm not sure if this is a standard or not (I usually don't see it that way), but having all caps in your IF / ELSE statements really annoys me for some reason.
精彩评论