Form validation in php when form action is another page (php code). How do I validate the form and display error message in view page?
view page
<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');
}
?>
create simple demo for name field required in php using session
you can try this way
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');
}
}
?>
There are many ways to do that the simple way I would suggest to use 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>
In PHP PAge :
if(empty($_POST['name'])){
echo "Name is required";
}
在重定向之前在会话中设置错误,在视图页面上回显它们,然后在 redirect.php 顶部取消设置错误,通过在顶部取消设置,您将确保始终拥有最新的错误消息。
Severe issue in your code
Fine tuning that can be done in your code
Some examples for you
If you need to proceed in your current implementation
Declare a variable called as $formavalid
and make it true and false accordingly
<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>
Hope, this helps you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.