0

我在下面有一个 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>

4

3 回答 3

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; ?>
于 2013-10-03T01:08:31.580 回答
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 ... />
于 2013-10-03T02:25:47.250 回答
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>
于 2013-10-03T01:19:41.380 回答