【发布时间】:2016-08-31 17:25:44
【问题描述】:
我有一个名为 test.php 的表单,其中包含 2 个按钮,每个按钮用于输入电子邮件 ID 和用户 ID;和一个提交按钮。当用户单击提交按钮时,我的代码只是检查电子邮件 ID 是否存在并向用户显示一条消息。 我想在电子邮件 ID 的文本框旁边显示此消息。但是,当我在文本框旁边回显此消息时,我收到一条错误消息,指出该变量未定义,因为每次加载表单时它都找不到该变量。如何更改我的代码以显示与用于输入电子邮件 ID 的文本框相邻的消息?我有必要使用会话吗?
这是我的代码:
<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";
}
}
?>
【问题讨论】:
-
如果您想在同一页面上显示消息,那么您可以将 test.php 设置为
action="test.php"等表单的操作。 -
@claudios..它不起作用。它仍然给出一个错误..未定义的变量..
-
什么变量未定义?也许发布一些你在这里遇到的错误。