2015-09-04 107 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); 
});