php - 如何在表单中的文本框旁边显示消息?

标签 php html mysql

我有一个名为 test.php 的表单,其中包含 2 个按钮,每个按钮用于输入电子邮件 ID 和用户 ID ;和一个提交按钮。当用户单击“提交”按钮时,我的代码只是检查电子邮件 ID 是否存在并向用户显示一条消息。 我想在电子邮件 ID 文本框旁边显示此消息。但是,当我在文本框旁边回显此消息时,我收到一条错误消息,指出该变量未定义,因为每次加载表单时都找不到该变量。如何更改代码以在用于输入电子邮件 ID 的文本框旁边显示消息?我有必要使用 session 吗?

这是我的代码:

<body>
<h3>Registration Form</h3>
<form action ="" method="POST">
<table align="center" cellpadding="10">
<tr>
<td>Email Id</td>
<td><input type="text" maxlength='100' name="emailid" id="emailid" required>  
<?php 
echo $msgemail;
?>
</td>
</tr> 
<tr>
<td>User Id</td>
<td><input type="text" maxlength='100' name="userid" id="userid" required ></td>
</tr> 
<tr>
<td>
<input type="submit" name="login" value="Login">
</td>
</tr>
</table>
</form>
</body>
<?php                   

//create a connection 
$conn = mysqli_connect('localhost', 'root', '', 'attendance');

if (isset($_POST['login'])) {

    //capture the $_POST value  
    $email  = $_POST['emailid'];
    $email  = trim($email);
    $userid = $_POST['userid'];
    $userid = trim($userid);


    if ($email=="") {
        echo "Please enter a valid email id";
    }

    if ($userid=="") {
        echo "Please enter a valid User Id";
    }

    //check if the email id exists
    $sql_check_email = "select * from employee where emp_email='$email';";

    mysqli_query($conn, $sql_check_email);

    $aff_email = mysqli_affected_rows($conn);

    // if email id exists ..display message 
    if ($aff_email==1) {

        $msgemail = "the email id exists";


    } else if ($aff_email>1) { 

        $msgemail = "there are multiple employees with the same email";        

    //display error message if there is an error 
    } else if ($aff_email<0) {

        echo "There is an error ..Try again";

    }   

}

?>

最佳答案

尝试将您的 $msgemail 设置为 session ,然后只需添加对要在其中显示 $msgemail 的 session 的检查。

请记住删除 session ,否则它将继续显示。

侧边栏:尝试使用bootstrap用于布局。比使用表格更好。

关于php - 如何在表单中的文本框旁边显示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200851/

相关文章:

mysql - 在单个查询中从多个表中获取数据

javascript - 出现错误 : "Can' t bind to 'ngModal' since it isn't a known property of 'input' "

javascript - jQuery 单击不触发按钮

php - 这个函数我写错了吗?有人可以告诉我我在哪里犯了错误吗?

php - 防止不相关的mysql数据

html - 手机下拉菜单

php - 下拉列表仅显示单个值我的数据库中有多个值

php - 防止用户在页面刷新时重新单击 "Like"按钮

php - 赫里多克不工作

javascript - 当我禁用标准浏览器使用 javascript 提交时,为什么 Ckeditor 值返回空?