如何验证按钮点击

时间:2014-01-24 11:37:16

标签: php jquery html mysql

我是HTML,JQuery的新手。所以没有得到如何解决我的问题。

我的问题是:

在我的网站中,我必须提供username验证。这里验证是检查输入的用户名和mysql数据库。

如果数据库包含用户名,那么pop应该说username is already exist请输入alternate。

我正在使用Xampp,php。

我试过的是:

 <label id="showlabel">UserName</label>
         <input type="text"  name="user" id="user" >
        <input type="button" name="checkuser"   id="checkuser" value="Validated" onclick="test();">


function test()

{
    /* check user is already exist in database */

}

我不知道怎么做。

任何人都可以指导我。

3 个答案:

答案 0 :(得分:2)

你可以通过使用像这样的ajax来实现这个目标

<强> HTML

<label id="showlabel">UserName</label>
<input type="text"  name="user" id="user" value="">
<input type="button" name="checkuser"   id="checkuser" value="Validated" onclick="test();">

<div id="user_status"></div>

<强> AJAX

像这样修改你的测试功能

<script>
function test()
{   
    var username = document.getElementById('user').value;
    var url = "check_user.php?username="+username;

   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)
        {

            var result = xmlhttp.responseText;
            if(xmlhttp.responseText!='')
            {
                document.getElementById('user_status').innerHTML =result ;
            }   
        }
    }


    xmlhttp.open("GET",url,true);

    xmlhttp.send();

}
</script>

PHP :check_user.php

<?php
if(!isset($_REQUEST['username'])
{
  echo "please set username";
  exit;
}
$username = $_REQUEST['username'];

$username = trim($username);


if($username=="")
{
    echo "please enter username";
}
else
{
    $res = mysql_query("SELECT * FROM `your_table` WHERE username='$username' LIMIT 1 ") or die(mysql_error());
    if($row = mysql_fetch_array($res))
    {
        echo "Username is exists in database";
    }
    else
    {
        echo "Username is not exists in database";
    }
} 
?>

答案 1 :(得分:0)

## HTML ##
 <label id="showlabel">UserName</label>
             <input type="text"  name="user" id="user" onblur="test(this);" >
            <span id="servermessage"/>

## JQuery ##

function test(sender)
{
 if ($.trim($(sender).val()).length) {
$.ajax({
                type:"POST",
                async: true,
                url: "checkusername.php", //In that php file check the value is unique or not in DB
                data: {username: $(sender).val()},
                success: function(data) {
                    if(data)
                       $('#servermessage').text("This username is already taken! Try another.");
                    else
                       $('#servermessage').text('Username is available.');
                }
            });
        }

}

答案 2 :(得分:0)

每次输入角色时,都会检查您的用户名可用性 你需要两个文件

  1. 的index.html
  2. check.php
  3. html的代码

    1. 的index.html

              <html>
              <head>
              <title>USER CHECK</title>
              <script type="text/javascript" src="js/jquery.js"></script>
              <script type="text/javascript">
      
              $(document).ready(function(){
      
                      $('#fb').load('check.php').show();
      
               $('#username_input').keyup(function(){
                   $('#fb').html('<p>Searching...</p>')
                   var dataString = $("#f1").serialize();
                      //alert (dataString);return false; 
                    $.ajax({  
                          type: "POST",  
                          url: "check.php",
                          data: dataString, 
                          success:function(result)
                          {
                              $('#fb').html(result).show();
                          }
                   });
                       return false;
                 });
               });
              </script>
              </head>
              <body>
                  <form id='f1' name='f1' action="">
                  <label>UserName</label>
                  <input type="text" name="username" id="username_input"><span id="fb"></span>
                  </form>
              </body>
              </html>
      
    2. check.php代码

      <?php 
      $db = new mysqli('HOST NAME','USER NAME','PASSWORD','DATABASE NAME');
      if($db->connect_errno)
      {
          die("DATABASE ERROR");
      }
      if(isset($_POST['username']))
      {
          $uname = $_POST['username'];
      }
      if(!empty($uname))
      {
      $query= ("select * from yourtablename where username='$uname'");
      $result=$db->query($query);
      $count = $result->num_rows;
      if($count == 0 )
      {
          echo "available";
      }
      else if($count == 1)
      {
          echo "not available";
      }
      }
      ?>
      

      如果用户名可用,则显示“可用”,否则“不可用”