验证POST值

时间:2016-09-27 15:14:58

标签: php html

我用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时,会发现失败消息

我该如何解决?

2 个答案:

答案 0 :(得分:0)

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

例如:

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

答案 1 :(得分:0)

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

用于执行此操作的函数称为isset()

  

isset - 确定变量是否已设置且不是NULL

说明

确定变量是否已设置且不为NULL。

如果使用unset()取消设置变量,则不再设置该变量。如果测试已设置为NULL的变量,则isset()将返回FALSE。另请注意,空字符(&#34; \ 0&#34;)不等同于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.");