如何在表单中显示与文本框相邻的消息?

时间:2016-08-29 07:37:12

标签: php html mysql

我有一个名为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";

    }   

}

&GT;

3 个答案:

答案 0 :(得分:0)

尝试将$msgemail设置为会话,只需添加要显示$msgemail的会话的检查。

请记住删除会话,否则会继续显示。

补充工具栏:尝试使用bootstrap进行布局。比使用表更好。

答案 1 :(得分:0)

if (isset($_POST['login'])) {之前定义$msgemail,因为如果在}结束后html将被PHP杀死,则将其添加到内部{{1}}。在{{1}}

中使用之前,将PHP代码置于顶部

答案 2 :(得分:0)

感谢您的投入。会话肯定可以解决上面的问题。但是,我尝试使用ajax,php,mysql。我在div标签中显示了一条消息,我将其放置在输入电子邮件ID的文本框旁边: 在电子邮件ID的文本框的onchange事件中,我使用的函数showuser接受文本框的值作为参数,并通过ajax和php文件检查电子邮件ID是否存在:welcome_user.php。因此,每当用户输入已存在的电子邮件ID并转到下一个文本框输入时,就会在div标签中显示一条消息,该文件框旁边是&#34; email id存在&#34;

这就是我所做的:

和registration.php:

<!DOCTYPE html>
<html>
<head>
<title>
Registration Form
</title>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","welcome_user.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>
<h3>Registration Form</h3>
<form action ="test-reg.php" method="POST" enctype="multipart/form-data">
<table align="center" cellpadding="20">
<tr>
<td>Name</td>
<td><input type="text" maxlength='100' name="empname" id="empname" required></td>
</tr>
<tr>
<td>Email Id</td>
<td><input type="text" maxlength='100' name="emailid" id="emailid" onchange="showUser(this.value)" required></td>
<td align="left"><div id="txtHint"></div></td></tr>
</tr>
<tr>
<td>User Id</td>
<td><input type="text" maxlength='100' name="userid" id="userid" required ></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" maxlength='100' name="pwd" id="pwd" required ></td>
</tr>

welcome_user.php:

<?php

if (isset($_GET['q'])) {

    $q = $_GET['q'];

    $conn = mysqli_connect('localhost','root','','attendance');

    $sql_get_email = "select * from employee where emp_email='$q'";

    mysqli_query($conn, $sql_get_email);

    $aff_email = mysqli_affected_rows($conn);

    if ($aff_email>0) {

        echo "Email id exists";
    }   

}
?>