简体   繁体   English

在没有表单验证的情况下,表单验证无法在php mysql页面上移动到表单数据处理php文件上?

[英]Form Validation not working in php mysql page moved on form data handling php file without form validation?

I made a form in php and I want to insert form entry in database. 我在php中创建了一个表单,我想在数据库中插入表单项。 My form have some validation rules, When I fill the form even wrong entries and hit submit button it moved on next form data handling php file but not validate the entries here is my code. 我的表单有一些验证规则,当我填写表单甚至输入错误并单击Submit按钮时,它都会移至下一个处理php文件的表单数据上,但未验证输入,这是我的代码。

<pre>
<?php
// define variables and set to empty values
$regErr = $nameErr = $misErr = $vbErr = $statErr = $tmErr = "";
$reg = $name = $mis = $vb = $stat = $tm = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["Reg"]))
     {$regErr = "Reg ID is required";}
     else
     {
     $reg = $_POST["Reg"];
}
   if (empty($_POST["Name"]))
     {$nameErr = "Name is required";}
     else
     {
     $name = $_POST["Name"];

          // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
     {
       $nameErr = "Only letters and white space allowed"; 
       }
   }

   if (empty($_POST["Mis"]))
     {$misErr = "Please Enter Marks";}
   else
     {
     $mis = $_POST["Mis"];
     // check if value entered is Numeric
     if(!is_numeric($mis)) {
    $misErr = "Data entered was not numeric";
     }
     }

   if (empty($_POST["Vb"]))
     {$vbErr = "Please Enter Marks";}
   else
     {
     $vb = $_POST["Vb"];
     // check if value entered is Numeric
     if(!is_numeric($vb)) {
    $vbErr = "Data entered was not numeric";
     }
     }

     if (empty($_POST["Stat"]))
     {$statErr = "Please Enter Marks";}
   else
     {
     $stat = $_POST["Stat"];
     // check if value entered is Numeric
     if(!is_numeric($stat)) {
    $statErr = "Data entered was not numeric";
     }
     }

     if (empty($_POST["Tmarks"]))
     {$tmErr = "Please Enter Marks";}
   else
     {
     $tm = $_POST["Tmarks"];
     // check if value entered is Numeric
     if(!is_numeric($tm)) {
    $tmErr = "Data entered was not numeric";
     }
     }     
}
?>


Here is my Form Code..


<form method="post" action="insert.php"> 
<table width=600 cellspacing=1 cellpadding=2 style='color:white;'>
<tr>
<td>Registration#:</td>
<td><input type="text" name="Reg"><span class="error">* <?php echo $regErr;?></span></td>
</tr>
<tr>
<td>Student Name:</td>
<td><input type="text" name="Name"> <span class="error">* <?php echo $nameErr;?></span></td>
</tr>
<tr>
<td>MIS Marks:</td>
<td><input type="text" name="Mis"><span class="error">*<?php echo $misErr;?></span></td>
</tr>
<tr>
<td>VB.NET Marks:</td>
<td><input type="text" name="Vb"><span class="error">*<?php echo $vbErr;?></span></td>
</tr>
<tr>
<td>Stat Marks:</td>
<td><input type="text" name="Stat"><span class="error">*<?php echo $statErr;?></span></td>
</tr>
<tr>
<td>Subject Total Marks:</td>
<td><input type="text" name="Tmarks"><span class="error">*<?php echo $tmErr;?></span></td>
</tr>
</table><br>
<p style='margin-left:300px;'><input type="submit" name="submit" value="Submit"></p>
</form>

</pre>

How to solve this problem? 如何解决这个问题呢?

This is happening because you are submitting the data to another file that is insert.php . 发生这种情况是因为您正在将数据提交到另一个文件insert.php It is not going through the validation in your code. 它没有通过您的代码中的验证。 You have to put this validation code into the insert.php file. 您必须将此验证代码放入insert.php文件中。

For example you have a file which has form like this: 例如,您有一个格式如下的文件:

<form action="insert.php" method="POST">
   <input type="text" name="name">
   <input type="submit" value="submit">
</form>

Now this form will be submitted to insert.php where you will apply the validation. 现在,此表单将提交到insert.php ,您将在其中应用验证。 Something like this: 像这样:

//insert.php
<?php
  $name = $_POST['name'];
  //validate name here
  //then insert into database..

?>

If you want to show the validation in same page in which your form is present then refer to the answer of THIS question . 如果要在显示表单的同一页面上显示验证,请参考此问题答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM