Ajax调用php函数会破坏所有javascript函数和页面

时间:2016-08-20 17:53:18

标签: javascript php jquery html ajax

我试图验证用户名是否已经使用。这是输入字段的更改。我已经有其他检查但是因为我添加了ajax调用它们不再工作了。我是ajax和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>
&#13;
&#13;
&#13;

我使用的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 = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[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文件中。 我只想检查名称是否已存在于数据库中。

1 个答案:

答案 0 :(得分: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的{​​{1}}获取值,这实际上是值 你应该使用username 并确保您已在html中添加了$_REQUEST['action']文件。