2016-08-20 133 views
0

我想验证用户名是否已经在使用。这是一个输入字段的变化。我已经有了其他支票,但是他们不再工作了,因为我添加了ajax调用。我是新来的Ajax和JavaScript,所以错误可以在那里。Ajax调用php函数打破所有javascript函数和页面

HTML表单:

<form action="test" method="post"> 
 
    <input id="username" type="text" placeholder="Gebruikersnaam" name="username" required onchange="checkUserName()"> 
 
    <br> 
 
    <input id="email" type="text" placeholder="Email" name="email" required onchange="validateEmail()"> 
 
    <br> 
 
    <input id="pass1" type="password" placeholder="Type wachtwoord" name="password1" required> 
 
    <br> 
 
    <input id="pass2" type="password" placeholder="Bevestig wachtwoord" name="password2" required onchange="passwordCheck()"> 
 
    <br> 
 
    <select name="typeAccount"> 
 
    <option value="bedrijf">Bedrijf</option> 
 
    <option value="recruiter">Recruiter</option> 
 
    <option value="werkzoekende">Talent zoekt job</option> 
 
    </select> 
 
    <p id="demo1"> 
 
    </P> 
 
    <p id="demo2"> 
 
    </P> 
 
    <button type="submit">Scrijf mij in!</button> 
 
</form>

,我使用的JavaScript:

<script src="jquery.js"> 
     function passwordCheck(){ 
      var password1 = document.getElementById('pass1').value; 
      var password2 = document.getElementById('pass2').value; 

      if(password1 !== password2){ 
       document.getElementById("pass1").style.borderColor = "#ff3333"; 
       document.getElementById("pass2").style.borderColor = "#ff3333"; 
      }else{ 
       document.getElementById("pass1").style.borderColor = "#1aff1a"; 
       document.getElementById("pass2").style.borderColor = "#1aff1a"; 
      } 
     } 
     function validate(email){ 
      var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
      return re.test(email); 
     } 
     function validateEmail(){ 
      var email = document.getElementById('email').value; 
      if(validate(email)){ 
       document.getElementById("email").style.borderColor = "#1aff1a"; 
      }else{ 
       document.getElementById("email").style.borderColor = "#ff3333"; 
      } 
     } 
     function checkUserName(){ 
      var username = document.getElementById('username').value; 
      if(username === ""){ 
       document.getElementById("username").style.borderColor = "#ff3333"; 
      }else{ 
       $.ajax({ 
        url: "userCheck.php", 
        data: { action : username }, 
        succes: function(result){ 
         if(result === 1){ 
          document.getElementById("username").style.borderColor = "#1aff1a"; 
         }else{ 
          document.getElementById("username").style.borderColor = "#ff3333"; 
         } 
        } 
       }); 
      } 
     } 
    </script> 

我用这个PHP脚本是在不同的文件中:

<?php 
    include("connect.php"); 
    $connect = new Connect(); 
    $username = mysql_real_escape_string($_POST['username']); 
    $result = mysql_query('select username from usermaindata where username = "'. $username .'"'); 
    if(mysql_num_rows($result)>0){ 
     echo 0; 
    }else{ 
     echo 1; 
    } 
?> 

脚本和html表单位于同一个html文件中,php位于单独的PHP文件中。 我只想检查名称是否已经在数据库中。

+2

mysql_ *函数警告正在提交...您的浏览器的控制台说什么? – jaro1989

+0

这是什么意思 - “不再工作”? –

+0

请检查您的浏览器控制台中的错误.. – Lal

回答

0

我假设你的数据库连接是完美的。

$username = mysql_real_escape_string($_POST['username']);

改变上面的代码

$username = mysqli_real_escape_string($db_connection,$_REQUEST['action']);

因为在你的Ajax你做喜欢

$.ajax({ 
        url: "userCheck.php", 
        data: { action : username }, 
        succes: function(result){ 
         if(result === 1){ 
          document.getElementById("username").style.borderColor = "#1aff1a"; 
         }else{ 
          document.getElementById("username").style.borderColor = "#ff3333"; 
         } 
        } 
       }); 

您还没有指定请求类型,你就取值使用$_POST与不同的变量名称username这实际上是值 您应该使用$_REQUEST['action'] 并确保您已在您的html中添加jquery.js文件。

+0

@ jaro1989没问题。 :) –

+0

我检查了铬控制台,我得到以下错误:Uncaught ReferenceError:$未定义 – stevedc

+0

你添加了'jquery.min.js'文件吗? –