【问题标题】:Trying to implement jquery/ajax into html page试图在 html 页面中实现 jquery/ajax
【发布时间】:2012-09-27 15:44:30
【问题描述】:
    echo "<form method='post' action='regprocess.php' id='registerform'>";
        echo '<fieldset class="register">';
        echo"<h2>Register</h2>";
            echo "<ul>";
                    echo '<li><label for="FirstName">First Name: </label> <input type="text" name="FirstName" id="FirstName"></li>';
                    echo '<li><label for="LastName">Last Name: </label> <input type="text" name="LastName" id="LastName"></li>';
                    echo '<li><label for="Email">Email: </label><input type="email" name="Email" id="Email"></li>';
                    echo '<li><label for="Username">Username: </label><input type="text" name="Username" id="Username"></li>';
                    echo '<li><input type="button" id="check_username_availability" value="Check Availability"></li>';  
                    echo '<div id="username_availability_result"></div>'; 
                    echo '<li><label for="Password">Password: </label><input type="password" name="Password" id="Password"></li>';
                    echo '<li><input type="submit" value="Register"></li>';
                    echo "</ul>";
        echo "</fieldset>";
        echo "</form>";
        }
        $username = mysql_real_escape_string($_POST['Username']);  

//mysql query to select field username if it's equal to the username that we check '  
$usernameresult = 'Select Username from User where Username = "'. $username .'"'; 
$uresult = $conn->query($usernameresult); 

//if number of rows fields is bigger them 0 that means it's NOT available '  
if($uresult->num_rows==1) {  
    //and we send 0 to the ajax request  
    echo 0;  
}else{  
    //else if it's not bigger then 0, then it's available '  
    //and we send 1 to the ajax request  
    echo 1;  
}  
?>
<script src="jquery-1.8.1.min.js" type="text/javascript"></script>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
$(document).ready(function() {  

        var checking_html = 'Checking...';  

        //when button is clicked  
        $('#check_username_availability').click(function(){  
            //run the character number check  
            if($('#username').val().length < min_chars){   
                $('#username_availability_result').html(checking_html);  
                check_availability();  
            }  
        });  

  });  

//function to check username availability  
function check_availability(){  

        //get the username  
        var username = $('#username').val();  

        //use ajax to run the check  
        $.post("check_username.php", { username: username },  
            function(result){  
                //if the result is 1  
                if(result == 1){  
                    //show that the username is available  
                    $('#username_availability_result').html(username + ' is Available');  
                }else{  
                    //show that the username is NOT available  
                    $('#username_availability_result').html(username + ' is not Available');  
                }  
        });  

}
</script>

大家好,所以我正在尝试制作一个表单来验证我的数据库中是否有可用的用户名。我以前有点用过 javascript,但我对 Ajax 和 JQuery 做的不多。我在控制台中不断收到此错误消息,上面写着 ReferenceError:找不到变量:$ 响应这行脚本 $(document).ready(function() {
我不太清楚为什么,我只是想知道是否有人可以给我一些关于如何解决它的见解?谢谢。

【问题讨论】:

  • 您确定 jquery-1.8.1.min.js 与您正在运行的脚本位于同一文件夹中吗?
  • 嗯,确实如此。但是我确实收到了另一个错误消息 Failed to load resource: server 从 jquery 文件响应状态为 404(未找到),我不太确定它是关于什么的..
  • 这意味着 jquery-1.8.1.min.js 与您正在运行的脚本不存在于同一文件夹中。仔细检查它的位置。
  • 或者你有一个干扰的重写规则。

标签: php jquery html mysql ajax


【解决方案1】:

代码看起来没问题。唯一的问题我认为您对 jquery 库的引用不正确。查看源代码并单击包含行的 jquery 库以测试路径是否正常。将jquery-1.8.1.min.js 保存在这些 php 文件所在的同一文件夹中,或者使用任何 CDN 库,例如

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

希望对你有帮助!!

【讨论】:

  • 我添加了该代码,它似乎解决了我的问题,谢谢!但后来我每次点击检查可用性按钮 TypeError: 'undefined' is not an object (evalating '$('#username').val().length')
  • courtney -- html id 属性区分大小写。尝试将“#username”更改为“#Username”。
  • 不客气!我认为 joadha 已经提到了上面的解决方案。将“#username”替换为“#Username”..
猜你喜欢
  • 1970-01-01
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多