dongtanjian9310 2016-08-20 17:53
浏览 41
已采纳

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

I'm trying to validate if a username is already taking or not. This onchange of an input field. I already got other checks but they don't work anymore since I added the ajax call. I'm new to ajax and javascript so the error can be there.

the html form:

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

the javascript that I use:

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

The php script I use this is in a different file:

<?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;  
    }  
?>

The script and the html form is in the same html-file and the php is in a seperate PHP-file. I just want to check if the name is already in the database or not.

</div>
  • 写回答

1条回答 默认 最新

  • doulian5857 2016-08-20 18:05
    关注

    I assume your database connection is perfect.

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

    change above code to

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

    because in your ajax you're doing like

    $.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";
                            }
                        }
                    });
    

    You have not specified request type and you're fetching value using $_POST with different variable name username which is actually value You should use $_REQUEST['action'] And make sure you've added jquery.js file in your html.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀