我在下面有一个if else语句,它会查看数据库并显示错误,如果电子邮件不在数据库中。我想在提交按钮旁边回显错误消息。目前,我的错误消息将始终显示在页面顶部。
if (mysql_num_rows($search_user_email) > 0) {
echo "<p style=\"color:red\"><b>Email found!</b></p>";
}
else {
echo "<p style=\"color:red\"><b>Email not found!</b></p>";
}
<tr style="background-color: #FFFFFF; height: 18px">
<td>
<span style="font-size:10pt;">Search by email:</span>
</td>
<td>
<form action="" method="post" name="search_email_form">
<input type="text" style="height:15px; font-size:10pt;" name="search_email_input"></input>
</td>
<td>
<input type="submit" style="height:22px; font-size:10pt;" name="search_email_submit" value="Search"></input>
</form>
答案 0 :(得分:4)
只需将错误消息保存到变量中并根据需要显示:
$errMsg="";
if (mysql_num_rows($search_user_email) > 0) {
$errMsg= "<p style=\"color:red\"><b>Email found!</b></p>";
}
else {
$errMsg= "<p style=\"color:red\"><b>Email not found!</b></p>";
}
<tr style="background-color: #FFFFFF; height: 18px">
<td>
<span style="font-size:10pt;">Search by email:</span>
</td>
<td>
<form action="" method="post" name="search_email_form">
<input type="text" style="height:15px; font-size:10pt;" name="search_email_input"></input>
</td>
<td>
<input type="submit" style="height:22px; font-size:10pt;" name="search_email_submit" value="Search"></input>
</form><?php echo $errMsg; ?>
答案 1 :(得分:0)
如果出现错误,请设置标记
if (mysql_num_rows($search_user_email) > 0) {
$emailError = true;
}
else {
echo "<p style=\"color:red\"><b>Email not found!</b></p>";
}
<tr style="background-color: #FFFFFF; height: 18px">
<td>
<span style="font-size:10pt;">Search by email:</span>
</td>
<td>
<form action="" method="post" name="search_email_form">
<input type="text" style="height:15px; font-size:10pt;" name="search_email_input"></input>
</td>
<td>
<input type="submit" style="height:22px; font-size:10pt;" name="search_email_submit" value="Search"></input>
<?php if($emailError)
echo "<p style=\"color:red\"><b>Email found!</b></p>";
$emailError = false;
?>
</form>
答案 2 :(得分:0)
我就是这样做的:
<?php
if(mysql_num_rows($search_user_email) > 0) {
$message = '<p style="color:red"><b>Email found!</b></p>';
} else {
$message = '<p style="color:red"><b>Email not found!</b></p>';
}
?>
<tr style="background-color: #FFFFFF; height: 18px">
<td>
<span style="font-size:10pt;">Search by email:</span>
</td>
<td>
<form action="" method="post" name="search_email_form">
<input type="text" style="height:15px; font-size:10pt;" name="search_email_input" />
</td>
<td>
<input type="submit" style="height:22px; font-size:10pt;" name="search_email_submit" value="Search" />
</form>
<?php
if(isset($message)) {
echo $message;
}
?>
顺便说一句,PHP不再支持 mysql _ 功能,它们迟早会被删除。*
编辑:
您无需放置</input>
,您可以这样做:
<input ... />