2015-09-04 36 views
0

下面是我的html,我需要没有表单格式的输入对话框。Ajax php验证没有功能

<!DOCTYPE> 
<html> 

<head> 
    <script> 
     var username; 

     function myFunction() { 
      //input dialog 
      username = prompt("Please enter your name", ""); 
      if (username != "") { 
       checkNameValidate(); 
      } else { 
       alert("The name is empty, please insert your name"); 
      } 
     } 

     function checkName() { //ajax to validate.php 
      var xmlhttp = new XMLHttpRequest(); 
      xmlhttp.open("GET", "validate.php?username=" + username, false); 
      xmlhttp.send(null); 

      //doFunction(); 
     } 

     function doFunction() { 
      //function 
     } 
    </script> 
</head> 

<body> 
    <input type="button" onclick="myFunction()" value="Name" /> 
</body> 

</html> 
下面

是validate.php代码,我的数据库名是testajax,

<?php 
    $username=$_POST["username"]; 

    mysql_connect("localhost","root",""); 
    mysql_select_db("testajax"); 

    $query1 = mysql_query("select * from user where name='$username'"); 
    if(mysql_num_rows($query1)>0){ 
      echo "<script language=javascript>alert('The name have used');</script>"; 
      } 
    ?> 

这是我的工作,但无法验证用户,什么是我的错,任何人都可以帮助矫正?

+0

但无法验证用户不足以理解发生了什么。你能解释一下你的期望和做什么? – Saif

回答

0

改变你的PHP代码($ _ POST到$ _REQUEST)一样,

<?php 
    $username=$_REQUEST["username"]; 

而在你的JavaScript代码调用函数checkName()checkNameValidate()

而且你需要添加的onreadystatechange功能为您回应尝试下面的代码,

var xmlhttp; 
function myFunction() { 
    username = prompt("Please enter your name", ""); 
    if (username != "") { 
     checkName(username); 
    } else { 
     alert("The name is empty, please insert your name"); 
    } 
} 
function checkName(username) { //ajax to validate.php 

    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) 
     { 
      alert(xmlhttp.responseText); 
     } 
    } 
    xmlhttp.open("GET", "validate.php?username=" + username, false); 
    xmlhttp.send(null); 
} 

如果您正在使用jQuery,然后用$.ajax()

你的PHP代码可以像越好,

<?php 
    // for get method not for post 
    // check username is set or not 
    $username=isset($_GET["username"]) ? $_GET["username"] : ''; 
    if($username){ // check if username is not empty 
     mysql_connect("localhost","root",""); 
     mysql_select_db("testajax"); 

     $query1 = mysql_query("select * from user where name='$username'"); 
     if(mysql_num_rows($query1)>0){ 
      echo "The name have used."; 
     } else { 
      echo "Username not exists, proceed."; 
     } 
    } else { 
     echo "Please enter username."; 
    } 
?> 
+0

谢谢你,我解决了它... 问题是_REQUEST – Ricky

1

您在XmlHTTP请求中使用GETvalidate.php中使用$_POST。 请纠正这一点,让我知道它是如何工作的。

此外您的JavaScript方法不匹配checkNameValidate & checkName。 你需要得到回应,并做你的逻辑(我不知道它是什么)。

请参考:http://www.w3schools.com/ajax/ajax_xmlhttprequest_response.asp了解更多详情。

+0

在评论区发布这篇文章。没有回答 – aldrin27

0

您可以通过下面的XMLHttpRequest.responseText得到它,但它与IE6/7浏览器不兼容。

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState == 4) { 
     alert(xmlhttp.responseText); 
    } 
} 
xmlhttp.open("GET","validate.php?username="+username,false); 
xmlhttp.send(null); 

你可以使用JQUERY。

$.get("validate.php?username="+username, function(responseText) { 
    alert(responseText); 
});