Validating POST values


Validating POST values

我用php做了一个页面,通过输入分数来查找分数(LOL)分别是index.php和php.php当我在索引页输入标记时,一切都很好

但是当我直接跳转到php.php,失败的消息来了,IDK为什么。

我想修复它。

index . php

     <html> 
     <head>
     <title>Know your grade</title>
     <style type="text/css">
     input[type=number]::-webkit-outer-spin-button,
     input[type=number]::-webkit-inner-spin-button {
    -webkit-appearance: none;
     margin: 0;
     }
     input[type=number] {
     -moz-appearance:textfield;
      }
      body{
      height: auto;
      max-width: 100%;
      }
      #abt{
      opacity: 0.3;
      -webkit-transition: opacity 2s;
      transition-timing-function: ease;
      text-shadow: 0.5px 0.5px blue;
      }
      #abt:hover{
      opacity: 1;
      }
      #wn{
      text-decoration: none;
      color: black;
      text-shadow: 0.5px 0.5px navy blue;
     -webkit-transition: color 0.5s;
      }
      #wn:hover{
      color:red;
      }
      #reset{
      -webkit-transition: color 1s , background-color 1.5s;
      }
      #reset:hover{
      color:blue;
      background-color: yellow;
      }
      </style>
      </head>
      <body>
      <h1 style="text-align:center;font-family:MS comic Sans">Whats your      grade?</h1>
       <div style="text-align:center;font-family:Georgia;">
        <form type="text" method="post" action="php.php">
    Enter Marks : <input id="marks" name="marks" min="0" step="0.5" type="number" placeholder="                 marks                 " required="required" oninput="validity.valid||(value='')">
    <input type="submit" value="Submit"><br><br>
    <input type="reset" name="Reset" id="reset">
    </form>
    </div>
    <br>
    <br>
    <br>
    <p style="text-align:center">How to use this : Enter marks of a subject       , Press Enter or Click submit and know your grade.</p>
     <p style="text-align:center"><b>Note:</b> Maximum marks is 100 </p>
     <div id="abt" style="bottom:0px;position:fixed;right:0px;margin-right:10px;margin-bottom:5px">
     <span style="margin:0 auto;display:table;"><u>About</u></span><br>
     Developer : <u>Srijan Mukhrjee</u><br>
     Proposed on : <u>26-09-2016</u><br>
     Completed on : <u>26-09-2016</u><br>
     Version : <u>2.<span style="font-size: 10px">1</span></u> (Latest   version)<br>
     <span style="display: table;margin: 0 auto"><a href="Whatsnew.html" id="wn">(What's New)</a></span>
      </div>
      </body>
      </html>

Php.php

    <?php
    error_reporting(E_ALL ^ E_NOTICE);
    $Mymarks = $_POST['marks'];
    $fail = "<h3>We are sorry to say that you failed with poor marks - work    hard</h3>"."<h4>Improve yourself and try better next time</h4>"."<h4>Your marks    is "."<span style='color:red'>".$Mymarks."</span>"." </h5>";
    if($Mymarks >=92 && $Mymarks <=100){
    echo "You have got A1 "."Your marks is ".$Mymarks;
    }
    elseif($Mymarks >=84 && $Mymarks <=91){
    echo "You have got A2 "."<br>"."And your marks is ".$Mymarks ;
    }
    elseif($Mymarks >=76 && $Mymarks <=83){
    echo "You have got B1 "."<br>"."And your marks is ".$Mymarks;
    }
    elseif($Mymarks >=68 && $Mymarks <=75){
    echo "You have got B2 "."<br>"."And your marks is ".$Mymarks;
    }
    elseif($Mymarks >=60 && $Mymarks <=67){
    echo "You have got C1 "."<br>Try better next time<br>"."And your marks is ".$Mymarks;
    }
    elseif($Mymarks >=52 && $Mymarks <=59){
    echo "You have got C2 "."<br>Improve yourself<br>"."And your marks is  ".$Mymarks;
     }
    elseif($Mymarks >=44 && $Mymarks <=58){
    echo "You have got D1 "."<br>Improve yourself<br>"."And your marks is ".$Mymarks;
     }
     elseif($Mymarks >=36 && $Mymarks <=57){
     echo "You have got E1 "."<br>Improve yourself<br>"."And your marks is ".$Mymarks;
     }
     elseif($Mymarks < 36){
     echo $fail;
     } 
     elseif($Mymarks > 100){
     echo "You Entered Marks greater than 100";
     }
     ?>

当我直接打开php而不经过index.php时,观察到失败消息

我该如何修复它?

当您直接访问PHP时,它不会收到您期望的POST值。您应该包含首先验证$_POST['marks']设置的逻辑,如果没有,则适当处理。

例如:

if(isset($_POST['marks'])){
    //Handle
}
else{
    //Default
}

您必须检查从表单中发布的值是否存在,然后根据它创建条件。

这样做的函数叫做isset()

isset -确定一个变量是否被设置并且不是NULL

描述:

判断一个变量是否被设置并且不为NULL

如果一个变量已经被unset()取消设置,它将不再被设置。如果测试的变量被设置为NULL, isset()将返回FALSE。还要注意,空字符("'0")不等同于PHP的null常量。

如果提供了多个参数,那么isset()只会在设置了所有参数的情况下返回TRUE。从左到右计算,一旦遇到未设置的变量就停止。

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

根据您的示例,您需要执行以下操作。

 if(isset($_POST['marks']))
    $marks= $_POST['marks'];
else
    die("Mark not provided. Please Ensure you provide it.");