簡體   English   中英

表單操作是另一個頁面(php代碼)如何驗證表單並在視圖頁面中顯示錯誤消息

[英]form action is another page(php code) how to validate the form and display error message in view page

當表單操作是另一個頁面(php 代碼)時,php 中的表單驗證。 如何驗證表單並在視圖頁面中顯示錯誤消息?

查看頁面

<html>
<body>
<form action="redirect.php" method="POST" enctype="multipart/form-data">  
    <label for="name"><b>Name: </b></label>
    <input type="text"  name="name" id="name" >
<br>
    <label for="email"><b>Email: </b></label>
    <input type="text"  name="email" id="email" >
<br>
    <label for="email"><b>Project Type: </b></label>
    <input type="text"  name="projecttype" id="projecttype">
<br>
    <label for="email"><b>Project Description: </b></label>
    <input type="text"  name="pdescription" id="pdescription" >
<br>
    <label for="file"><b>File Upload: </b></label>
    <input type="file"  name="file"  id="file">
<br>
    <div class="clearfix">
      <input type="submit" name="submit1" id="submit1" value="submit1">
    </div>  
</form>
</body>
</html>

重定向.php

<?php
if(isset($_POST['submit1'])) 
{
    $name=$_POST['name'];
    $email=$_POST['email'];
    $projecttype=$_POST['projecttype'];
    $projectdetails=$_POST['pdescription'];
    $attachment=$_FILES['file']['name'];
    $tmp_name=$_FILES['file']['tmp_name'];
    move_uploaded_file($tmp_name,"upload/".$attachment);
    $query=mysql_query("insert into projectdetails(Customer_name,Customer_email,Customer_attachment,Customer_project_type,Customer_project_details) values('$name','$email','$attachment','$projecttype','$projectdetails')");
    if($query)
    {   
        header('Location: test2.php');      
    }
    ?>

name field required in php using session創建簡單的演示

你可以試試這種方式

form.php

<?php
    session_start();
    if(isset($_SESSION['msg'])){
        $name=$_SESSION['msg']['name'];
    }
?>
<html>
<body>
<form action="redirect.php" method="POST" enctype="multipart/form-data">  
    <label for="name"><b>Name: </b></label>
    <input type="text"  name="name" id="name" > <span style="color:red"><?php echo $name ?></span>
<br>
    <label for="email"><b>Email: </b></label>
    <input type="text"  name="email" id="email" >
<br>
    <label for="email"><b>Project Type: </b></label>
    <input type="text"  name="projecttype" id="projecttype">
<br>
    <label for="email"><b>Project Description: </b></label>
    <input type="text"  name="pdescription" id="pdescription" >
<br>
    <label for="file"><b>File Upload: </b></label>
    <input type="file"  name="file"  id="file">
<br>
    <div class="clearfix">
      <input type="submit" name="submit1" id="submit1" value="submit1">
    </div>  
</form>
</body>
</html>

redirect.php

<?php
session_start();
if(isset($_POST['submit1'])) 
{

    $name=$_POST['name'];

    if(isset($name) && empty($name)){
        $_SESSION['msg']['name']="Name must be required!";
        header('location: form.php');  
        exit;
    }
    $email=$_POST['email'];
    $projecttype=$_POST['projecttype'];
    $projectdetails=$_POST['pdescription'];
    $attachment=$_FILES['file']['name'];
    $tmp_name=$_FILES['file']['tmp_name'];
    move_uploaded_file($tmp_name,"upload/".$attachment);
    $query=mysql_query("insert into projectdetails(Customer_name,Customer_email,Customer_attachment,Customer_project_type,Customer_project_details) values('$name','$email','$attachment','$projecttype','$projectdetails')");
    if($query)
    {   
        header('Location: test2.php');      
    }
}
?>

有很多方法可以做到這一點,我建議使用 HTML5 的簡單方法

 <script src="http://code.jquery.com/jquery-1.9.1.js"></script> $(function () { $('form').bind('click', function (event) { event.preventDefault(); $.ajax({ type: 'POST', url: 'post.php', data: $('form').serialize(), success: function () { alert('form was submitted'); } }); }); });
 <html> <body> <form action="redirect.php" method="POST" enctype="multipart/form-data"> <label for="name"><b>Name: </b></label> <input type="text" name="name" id="name" required> <br> <label for="email"><b>Email: </b></label> <input type="email" name="email" id="email" required> <br> <label for="email"><b>Project Type: </b></label> <input type="text" name="projecttype" id="projecttype" required> <br> <label for="email"><b>Project Description: </b></label> <input type="text" name="pdescription" id="pdescription" required> <br> <label for="file"><b>File Upload: </b></label> <input type="file" name="file" id="file" required> <br> <div class="clearfix"> <input type="submit" name="submit1" id="submit1" value="submit1"> </div> </form> </body> </html>

在 PHP 頁面中:

if(empty($_POST['name'])){
 echo "Name is required";
}

在重定向之前在會話中設置錯誤,在視圖頁面上回顯它們,然后在 redirect.php 頂部取消設置錯誤,通過在頂部取消設置,您將確保始終擁有最新的錯誤消息。

您的代碼中存在嚴重問題

  1. 不要在 PHP 中使用 mysql_* 函數

可以在您的代碼中完成的微調

  1. 您不需要將您的輸入發送到另一個頁面只是為了驗證

一些例子給你

客戶端表單驗證

PHP MySQLi 基本示例

如果您需要繼續當前的實施

聲明一個名為$formavalid的變量並相應地使其為真和假

<html>
<body>
<?php 
$formvalid = true;
?>
<form action="redirect.php" method="POST" enctype="multipart/form-data">  
    <label for="name"><b>Name: </b></label>
    <input type="text"  name="name" id="name" >
<br>
    <label for="email"><b>Email: </b></label>
    <input type="text"  name="email" id="email" >
<?php
//Do like this for all fields
if(isset($_POST['submit1'])) 
{
if(!isset($_POST['email'])) 
{
echo 'Email field is missing';
}else{
$formvalid = false;// Make the $formvalid as false if your input is dirty
}
}
<br>
    <label for="email"><b>Project Type: </b></label>
    <input type="text"  name="projecttype" id="projecttype">
<br>
    <label for="email"><b>Project Description: </b></label>
    <input type="text"  name="pdescription" id="pdescription" >
<br>
    <label for="file"><b>File Upload: </b></label>
    <input type="file"  name="file"  id="file">
<br>
    <div class="clearfix">
      <input type="submit" name="submit1" id="submit1" value="submit1">
    </div>  
</form>
<?php
if($formvalid==true){
//Form is valid so do the action for it
}else{
//form is not valid, so let the user input to make it valid
}
?>
</body>
</html>

希望,這對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM