如何准确验证PHP上的字段?

时间:2016-08-01 14:05:11

标签: php html5 algorithm function validation

我有这个简单的编程练习,我试图用PHP来做:确定学生是否通过课程。如果学生的三个等级的平均值大于或等于15,则该学生将通过该课程。如果学生通过或未通过,则显示一条消息,并在计算平均值之前验证三个等级。如果错误,请显示错误消息。

我在伪代码和pseint中都有它,所以它很容易验证,因为我使用do-while循环,每当它要求我键入一个数字而我做错了,它验证了等级:

Algorithm Exercise14
Define grade1, grade2, grade3, prom as Real;
Repeat
    print "Type the first grade: ";
    read grade1;
    if (grade1<0 | grade1>20) 
        print "Error! Invalid grade, type a valid grade.";
    endif
Until (grade1>=0 & grade2<=20);
Repeat
    print "Type the second grade: ";
    read grade2;
    if (grade2<0 | grade2>20) 
        print "Error! Invalid grade, type a valid grade.";
    endif
Until (grade2>=0 & grade2<=20);
Repeat
    print "Type the third grade: ";
    read grade3;
    if (grade3<0 | grade3>20) 
        print "Error! Invalid grade, type a valid grade.";
    endif
Until (grade3>=0 & grade3<=20);
prom = (grade1+grade2+grade3)/3;
if (prom>=15) 
    print "The student passed the course!";
    print "The average is: " prom;
else
    print "The student did not pass the course.";
    print "The average is: " prom;
endif
endAlgorithm

现在,我想在PHP中这样做。我的问题是我无法以这种方式进行验证,因为如果我这样做,浏览器只需要花费很多时间来做出反应而且什么都不做,所以我在互联网上看了一下,我需要一些功能来验证但是我需要这样做。我不确定。我正在学习PHP,请原谅我这是一个重复的问题,还是很容易,但我想知道,因为我在学习。

最后一个问题是,如果我使用PHP,显然是HTML5,我如何验证这些字段?

PHP代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Exercise 14</title>
    <meta charset="utf-8">
</head>
<body>
    <h2>PHP Exercises</h2>
    <h3>Exercise 14</h3>
    <form action="#" method="post" id="constant" name="constant">
        <div>
            <label for="text">Grade 1: </label>
    <input type="text" name="grade1" placeholder="Type grade1">
        </div>
        <div>
            <label for="text">Grade 2: </label>
        <input type="text" name="grade2" placeholder="Type grade 2">
        </div>
        <div>
            <label for="text">Grade 3: </label>
        <input type="text" name="grade3" placeholder="Type grade 3">
        </div>
        <div>
      <input type="submit" name="button" id="sending" value="Send">
        </div>
    </form>
    <?php
        if(isset($_POST['button'])){
            $grade1 = $_POST['grade1'];
            $grade2 = $_POST['grade2'];
            $grade3 = $_POST['grade3'];

            while($grade1<0){
                    echo "Error!";
            }

            do{
                if ($grade2<0 | $grade2>20) {
                    echo "Error!";
                }

            }while($grade2>=0 && $grade2<=20);

            do{
                if ($grade3<0 | $grade3>20) {
                    echo "Error!";
                }

            }while($grade3>=0 && $grade3<=20);

            $prom = ($grade1+$grade2+$grade3/3);

            if ($prom>=15) {
                echo "The averages is: " . $prom;
                echo "The student passed the course!";
            }else{
                echo "The averages is: " . $prom;
                echo "The student did not pass the course.";
            }
        }

    ?>
 </body>
</html>

2 个答案:

答案 0 :(得分:1)

PHP无法以交互方式从命令提示符或其他立即刷新的输入中读取。为Web客户端编写服务器端代码的模型始终是Web客户端(浏览器,HTML)发送一个包含数据的HTTP请求,服务器评估该数据,然后发送响应。在发出另一个HTTP请求之前,数据不会刷新。因此,循环app = angular.module('MyApp', ['underscore']); app.factory('_', ['$window', function($window) { return $window._; }); 值直到它是正确的值是没有意义的;在一次执行代码的过程中,该值不会发生变化。你的逻辑必须是:

  1. 评估每次输入一次。
  2. 如果任何一个输入无效,请拒绝整个请求。
  3. 仅当所有输入都有效时,请执行您想做的任何操作。
  4. 返回HTML回复,说明错误或成功的原因。
  5. Exemptigratia:

    $_POST

答案 1 :(得分:1)

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Exercise 14</title>
    <meta charset="utf-8">
</head>
<body>
    <h2>PHP Exercises</h2>
    <h3>Exercise 14</h3>
    <form name="exer14" action="#" method="post" id="constant" name="constant">
        <div>
            <label for="text">Grade 1: </label>
            <input type="number" name="grade1" placeholder="Type grade 1"  required>
        </div>
        <div>
            <label for="text">Grade 2: </label>
            <input type="number" name="grade2" placeholder="Type grade 2"  required>
        </div>
        <div>
            <label for="text">Grade 3: </label>
            <input type="number" name="grade3" placeholder="Type grade 3"  required>
        </div>
        <div>
            <input type="submit" name="button" id="sending" value="Send">
        </div>
    </form>
    <?php

    if(isset($_POST['button'])){
        $grade1 = $_POST['grade1'];
        $grade2 = $_POST['grade2'];
        $grade3 = $_POST['grade3'];
        $count = 0;
        $errors = array("Error1" => "Error! The grade $grade1 is invalid. Type a grade between 0 and 20.",
                        "Error2" => "Error! The grade $grade2 is invalid. Type a grade between 0 and 20.",
                        "Error3" => "Error! The grade $grade3 is invalid. Type a grade between 0 and 20.");

        if (empty($_POST['grade1']) || empty($_POST['grade2']) || empty($_POST['grade3'])) {
            echo "Complete all inputs.";

        }elseif (is_numeric($grade1)==false || is_numeric($grade2)==false || is_numeric($grade3)==false) {

            if (is_numeric($grade1)==false) {
                echo "Error! Invalid grade: '$grade1' contains letters. Type a grade between 0 and 20." . "<br>";                   
            }
            if (is_numeric($grade2)==false) {
                echo "Error! Invalid grade: '$grade2' contains letters. Type a grade between 0 and 20." . "<br>";                   
            }
            if (is_numeric($grade3)==false) {
                echo "Error! Invalid grade: '$grade3' contains letters. Type a grade between 0 and 20." . "<br>";                   
            }

        }elseif ($grade1<0 || $grade1>20 || $grade2<0 || $grade2>20 || $grade3<0 || $grade3>20) {

            if ($grade1<0 || $grade1>20){
                echo $errors["Error1"] . "<br>";
            }
            if ($grade2<0 || $grade2>20){
                echo $errors["Error2"] . "<br>";
            }
            if ($grade3<0 || $grade3>20){
                echo $errors["Error3"] . "<br>";
            }
                //print_r($errors);
        }elseif (($grade1>=0 || $grade1<=20) && ($grade2>=0 || $grade2<=20) && ($grade3>=0 || $grade3<=20)) {

            $prom = ($grade1+$grade2+$grade3)/3;

            if ($prom>=15) {
                echo "The student average is " . $prom . "<br>";
                echo "The student passed the course!";
            }else{
                echo "The student average is " . $prom . "<br>";
                echo "The student failed the course.";
            }
        }else{
            echo "Enter the right grades.";
        }
    }
    ?>
</body>

我已经用这种方式解决了这个问题。我可以用几种情况,字母和数字范围验证这些输入。这个练习非常简单,但我在Pseint和C ++中做到了,所以这些与PHP非常不同。我现在明白了服务器客户端的用途。