【问题标题】:ajax call does not return from php-scriptajax 调用不会从 php-script 返回
【发布时间】:2012-05-09 21:32:03
【问题描述】:

我的问题是我有两个对 php 的 ajax 调用以获取注册表单。首先是用户名可用性检查,它工作得非常好,其次是密码检查,它不起作用,数据库和所有三个文件肯定是连接的,但我找不到问题。如果有人知道,谢谢。
这是html:

<div id="registration_form">
    <table>
         <tr>
            <td>Choose Username:</td>
            <td><input type="text" id="username" autofocus="autofocus"  /><span id="username_status"> </span></td>
         </tr>
        <tr>
            <td>Choose Password:</td>
            <td><input type="password" id="password" /> <span  id="password_status"></span></td>
         </tr>
         <tr>
            <td>Confirm Password:</td>
            <td><input type="password" id="confirmpassword" /> <span  id="pconfirm_status"></span></td>
         </tr>
        <tr>
            <td>Email:</td>
            <td><input type="text" id="email" /><span id="email_status"></span></td>
         </tr> 
         <tr>
            <td>Confirm Email:</td>
            <td><input type="text" id="confirmemail" /><span id="econfirm_status"></span></td>
         </tr>
        <tr>
            <td><input type="submit" value="Register" id="submit" />    </td>
         </tr>
    </table>
        <div id="regform_reply"></div>
</div>

这里是 jquery:

$('#password').keyup(function()
{
var password = $(this).val();
$('#password_status').text('Searching...');
    if (password != '')
    {
        $.post('php/register.php', { password: password }, function(data)
        {
            $('#password_status').text(data);
        });
    }
    else
    {
        $('#password_status').text('');
    }           
});


$('#username').keyup(function()
{
var username = $(this).val();
$('#username_status').text('Searching...');
    if (username != '')
    {
        $.post('php/register.php', { username: username }, function(data)
        {
            $('#username_status').text(data);
        });
    }
    else
    {
        $('#username_status').text('');
    }           
});

这里是 php:

<?php
include '../connect/userdb_connect.php';

if (isset($_POST['password']))
{
$password = mysql_real_escape_string($_POST['password']);
if (!empty($password))
{
    if (strlen($password)>25)
    {
        echo 'Too long';
    }
    else if(strlen($password)<6)
    {
        echo 'Too short';
    }
    else
    {
        echo 'Fine';
    }
}   
}

if (isset($_POST['username']))
{
$username = mysql_real_escape_string($_POST['username']);
if (!empty($username))
{
    $check = mysql_query("SELECT `user_name` FROM `userbase` WHERE `user_name`  = '$username'");
    $result = mysql_num_rows($check);

    if ($result == 0 && strlen($username) <25)
    {
        echo 'Available';
    }
    else if($result == 1 && strlen($username) <25)
    {
        echo 'Already taken';
    }
    else
    {
        echo 'Too long';
    }
}           
}
?>

【问题讨论】:

  • 您是否遇到特定错误?
  • JS 控制台(Firebug / Chrome 的控制台)对 XmlHttpRequest 有什么看法?您是否有一个链接,以便我们可以查看整个控制台和脚本?
  • 另外,究竟什么“不起作用”?您收到的是no回复还是错误回复?

标签: php jquery ajax


【解决方案1】:

我会选择类似的东西:

$('#password').on('keyup', function() {
    //there's no need to do ajax to check input length, just always validate on the server aswell
    var password = this.value;
    if (password != '') {
        $('#password_status').text('Searching...');
        if (password.length>25) {
            $('#password_status').text('Too long');
        }else if (password.length<6) {
            $('#password_status').text('Too short');
        }else{
            $.post('php/register.php', { password: password }, function(data) {
                //do you really need to check this, other then when inserting to DB ?
                $('#password_status').text(data); 
            });
        }
    }else{
        $('#password_status').text('');
    }
});

$('#username').on('keyup', function() {
    var username = this.value;
    if (username != '') {
        $('#username_status').text('Searching...');
        if (username.length>25) {
            $('#username_status').text('Too long'); 
        }else if (username.length<6) {
            $('#username_status').text('Too short'); 
        }else{
            $.post('php/register.php', { username: username }, function(data) {
                $('#username_status').text(data);
            });
    }else{
        $('#username_status').text('');
    }
});

PHP

<?php
    include '../connect/userdb_connect.php';

    if (isset($_POST['password'])) {
        $password = mysql_real_escape_string($_POST['password']);
        if (!empty($password) && strlen($password)<25 && strlen($password)>6) {
            //hash and insert into db
        }else{
            //error
        }
    }else{
       //no POST var
    }

    if (isset($_POST['username'])) {
        $username = mysql_real_escape_string($_POST['username']);
        if (!empty($username) && strlen($username)<25 && strlen($username)>6) {
            $check = mysql_query("SELECT `user_name` FROM `userbase` WHERE `user_name`  = '$username'");
            $result = mysql_num_rows($check);
            if ($result == 0) {
                echo 'Available';
            }else if ($result == 1) {
                echo 'Already taken';
            }
        }else{
            //error
        }
    }else{
       //no POST var
    }
?>

【讨论】:

    【解决方案2】:

    您可能想要删除 mysql_real_escape_string,因为您没有将它传递给 mysql。可能是某些转义序列混淆了您的比较。

    所以$password = mysql_real_escape_string($_POST['password']);

    应该只是$password = $_POST['password'];

    (不是说把它放到数据库里就可以了,但是做一个strlen函数应该没问题)

    【讨论】:

    • 是什么阻止我手动访问该页面并使用它来将恶意代码放入 sql 查询中?
    • 什么SQL查询?这里没有关于密码的 SQL 查询。如果他确实添加了一个 SQL 查询,那么是的,它需要被转义。
    • @ccKep 无论如何,这是在黑暗中拍摄的 :)
    猜你喜欢
    • 1970-01-01
    • 2018-01-02
    • 2012-01-05
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    相关资源
    最近更新 更多