Sabyasachi Gupta Sabyasachi Gupta - 3 months ago 10
MySQL Question

How to display a message adjoining a text box in a form?

I have a form named test.php, which contains 2 buttons, each for entering an email id and userid ; and a submit button . When the user clicks on submit button , my code simply checks if the email id exists and displays a message to the user . I would like to display this message adjoining the text box for email id. But when I echo this message just next to the textbox , I get an error message that the variable is not defined ,since it doesn't find the variable each time the form loads . How do I change my code to display the message adjoining the textbox for entering the email id? Is it necessary for me to use sessions?

Here is my code :

<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";

}

}


?>

Answer

Thanks for your inputs . Sessions can definitely address the problem above . However , I tried the same using ajax,php, mysql . I displayed a message in a div tag which i placed just next to the textbox where the email id is entered : In the onchange event of the textbox for email id , I am using a function showuser that accepts the value of the textbox as a parameter and checks whether the email id exists through ajax and a php file :welcome_user.php . Therefore , everytime a user enters an email id that already exists and moves on to the next textbox input , a message is displayed in the div tag just adjoining the textbox that the "email id exists" .

Here's what I have done :

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";
    }   

}
?>