当表单操作是另一个页面(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>
redirect.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');
}
?>
答案 0 :(得分:1)
为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');
}
}
?>
答案 1 :(得分:0)
我建议使用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 PAge中:
if(empty($_POST['name'])){
echo "Name is required";
}
答案 2 :(得分:0)
在重定向之前在会话中设置错误,在视图页面上回显它们然后在redirect.php顶部取消设置错误,通过在顶部取消设置它们,您将确保始终拥有最新的错误消息。
答案 3 :(得分:0)
您的代码中存在严重问题
可以在您的代码中完成微调
您的一些示例
如果您需要继续执行当前的实施
声明一个名为$formavalid
的变量,并将其设为true和false
<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>
希望,这有助于你