【问题标题】:How to display a message adjoining a text box in a form?如何在表单中显示与文本框相邻的消息?
【发布时间】: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..它不起作用。它仍然给出一个错误..未定义的变量..
  • 什么变量未定义?也许发布一些你在这里遇到的错误。

标签: php html mysql


【解决方案1】:

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

记得删除会话,否则它会继续显示。

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

【讨论】:

    【解决方案2】:

    if (isset($_POST['login'])) { 之前定义$msgemail,因为您将其添加到其中,如果$msgemail 将在关闭if } 后被PHP 杀死。并在html中使用之前将你的PHP代码放在最前面

    【讨论】:

      【解决方案3】:

      感谢您的意见。 Sessions绝对可以解决上述问题。但是,我尝试使用 ajax、php、mysql 进行相同的操作。我在一个 div 标签中显示了一条消息,我将它放在输入电子邮件 ID 的文本框旁边: 在电子邮件 id 的文本框的 onchange 事件中,我使用了一个函数 showuser,它接受文本框的值作为参数,并通过 ajax 和 php 文件:welcome_user.php 检查电子邮件 ID 是否存在。因此,每当用户输入一个已经存在的电子邮件 id 并进入下一个文本框输入时,就会在紧邻“电子邮件 id 存在”的文本框旁边的 div 标记中显示一条消息。

      这是我所做的:

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

      【讨论】:

        猜你喜欢
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多