我必须显示一个服务器端验证错误消息,如果我在一个页面上编写两个代码(HTML 和 PHP),它工作得很好。
现在我有 index.php 和 process.php 页面。我必须将服务器端验证错误消息从 process.php 传递到 index.php。不使用 Ajax。你会在这方面帮助我吗?
索引.php
<?php
include('../../db/connection.php');
$fname_error="";
$email_error="";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="process.php" method="post">
<input type="text" name="fname" value="<?php if(isset($fname)){echo $fname;} ?>">
<span class="error"><?php echo $fname_error;?></span>
<input type="email" name="email" value="<?php if(isset($email)){echo $email;}?>">
<span class="error"><?php echo $email_error;?></span>
<input type="submit" name="submit">
</form>
</body>
</html>
进程.php
<?php
include('../../db/connection.php');
if (isset($_POST['submit'])) {
$fname=trim($_POST['fname']);
$email=trim($_POST['email']);
if (empty($fname)) {
$fname_error="Name is empty";
}
else
{
if ($fname<3) {
$fname_error="Please enter minimum 3 character";
}
}
if (empty($email)) {
$email_error="Email field is empty";
}
else
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error="Invalid email format";
}
}
// insert code here
}
?>