我需要使用php显示错误消息。我正在解释下面的代码。
addcomplain.php:
<div style="color:#F00; text-align:center;"><?php echo $error?></div>
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
</form>
假设我有一个上面的表格,当我提交下面的文件时会被调用。
complain.php:
$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
if(mysqli_num_rows($selres ) > 0)
{
$error = "Email Id already exists!";
}
当上述错误发生时,我需要它应该显示在表单上方。在我的情况下,我没有得到任何东西。请帮我解决这个问题。
答案 0 :(得分:1)
试试这个
addcomplain.php:
<?php
if(isset($_SESSION["error"])){
echo '<div style="color:#F00; text-align:center;">'. $_SESSION["error"] .'</div>';;
unset($_SESSION["error"]);
} ?>
complain.php:
session_start();
$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
if(mysqli_num_rows($selres ) > 0)
{
$_SESSION["error"] = "Email Id already exists!";
}
答案 1 :(得分:1)
您不需要会话。
将代码合并为1个文件。如果他不为,则仅显示$error
,如下所示:
<?php
$error = null;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
if(mysqli_num_rows($selres ) > 0) {
$error = "Email Id already exists!";
}
}
?>
<div style="color:#F00; text-align:center;"><?= (!is_null($error) ? $error : ''); ?></div>
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
</form>
</div>
答案 2 :(得分:0)
试试这样:
complain.php:
$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
if(mysqli_num_rows($selres ) > 0)
{
$_SESSION["error"] = "Email Id already exists!";
}
在addcomplain.php中:
$error = $_SESSION["error"];
unset($_SESSION["error"]);
<div style="color:#F00; text-align:center;"><?php echo $error?></div>
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
</form>
答案 3 :(得分:0)
您可以编写几个函数来简化:
function setMessage($message){
$_SESSION['msg'] = $message;
}
function showMessage(){
echo $_SESSION['msg'];
unset($_SESSION['msg']);
}
complain.php:
if(mysqli_num_rows($selres ) > 0)
{
$error = "Email Id already exists!";
setMessage($error);
}
addcomplain.php:
<div style="color:#F00; text-align:center;"><?php echo showMessage(); ?></div>
注意:请确保您已使用页面顶部的session_start()
启动了会话。
答案 4 :(得分:0)
使用Session对象。
$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
if(mysqli_num_rows($selres ) > 0)
{
$_SESSION['error'] = "Email Id already exists!";
}
这样的显示。
<?php
$error = "";
if(isset($_SESSION['error']))
{
$error = $_SESSION['error'];
unset($_SESSION['error']);
}
?>
<div style="color:#F00; text-align:center;">
<?php echo $error; ?>
</div>
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
</form>
答案 5 :(得分:0)
<?php
$sql="SELECT * FROM medilink_complain WHERE email='".$email."'";
$selres = mysqli_query($con,$sql);
if(mysqli_num_rows($selres ) > 0)
{
$error = "Email Id already exists!";
} else {
$error = '';
}
include 'addcomplain.php'; // <----
?>
<html>
<body>
<div style="color:#F00; text-align:center;"><?php echo $error?></div>
<form name="billdata" id="billdata" enctype="multipart/form-data" method="POST" onSubmit="javascript:return checkForm();" action="complain.php">
<input value="test" />
</form>
</body>
</html>
......对我有用!不需要会话变量。