weixin_33722405 2013-05-16 16:14 采纳率: 0%
浏览 39

jQuery Ajax-没有成功

success in my AJAX call doesn't trigger at all, and I can't figure out why. None of the alerts specified in the AJAX file are popping up.

The form:

<form onsubmit="check_reg();return false;" method="post" name="reg_form" id="reg">
    <label class="error" id="reg_error"></label>
    <label for="login_reg">Login</label>
    <input type="text" name="login" id="login_reg" placeholder="Login">
    <label for="email_reg">Email</label>
    <input type="text" name="email" id="email_reg" placeholder="Email">
    <label for="haslo_reg">Password</label>
    <input type="password" name="password" id="pass_reg" placeholder="Password">
    <button type="submit">Register</button>  
</form>

reg_script.php:

if (!empty($_POST['login']) && !empty($_POST['password']) && !empty($_POST['email'])) {
    $login = $mysqli->real_escape_string($_POST['login']);
    $password = $mysqli->real_escape_string($_POST['password']);
    $email = $mysqli->real_escape_string($_POST['email']);

    if ($mysqli->query("INSERT INTO users (login, password, email) VALUES('$login', '$password', '$email')")) {
        echo "ok";
    } 
    else {
        echo "error";
    }
} 
else {
    echo "empty";
}

AJAX

function check_reg() {
    $.ajax({
        url: 'reg_script.php',
        type: "POST",
        data: "login=" + $('#login_reg').val() + "&password=" + $('#pass_reg').val() + "&email=" + $('#email_reg').val(),
        success: function(result) {
            if (result == 'ok') {
                alert('ok');
            }
            else if (result == 'error') {
                alert('error');
            }
            else if (result == 'empty') {
                alert('empty');
            }
        },
    });
}

Any ideas what the problem might be?

EDIT: I should've added that I used this tutorial: http://developertips.net/post/display/web/3/dynamic-login-form-with-ajax-php/ and managed to get the login form working just fine, it's just the registration form that's acting up.

  • 写回答

2条回答 默认 最新

  • weixin_33719619 2013-05-16 16:45
    关注

    Change

    <form onsubmit="check_reg();return false;" method="post" name="reg_form" id="reg">
    <label class="error" id="reg_error"></label>
    <label for="login_reg">Login</label>
    <input type="text" name="login" id="login_reg" placeholder="Login">
    <label for="email_reg">Email</label>
    <input type="text" name="email" id="email_reg" placeholder="Email">
    <label for="haslo_reg">Password</label>
    <input type="password" name="password" id="pass_reg" placeholder="Password">
    <button type="submit">Register</button>  
    </form>
    

    to:

    <form>
    <label class="error" id="reg_error"></label>
    <label for="login_reg">Login</label>
    <input type="text" name="login" id="login_reg" placeholder="Login">
    <label for="email_reg">Email</label>
    <input type="text" name="email" id="email_reg" placeholder="Email">
    <label for="haslo_reg">Password</label>
    <input type="password" name="password" id="pass_reg" placeholder="Password">
    <input type="button" onclick="check_reg()">Register</button>  
    </form>
    

    That way, your HTML file will call your check_reg() which then calls your php-file. You don't need method=post and so on in HTML-file. All this information is already in your function.

    Btw, your data-attribute in check_reg() function, I recommend that you change that to serializeArray() instead.

    One more thing I just realized:

    success: function(result) {
    

    The result-variable contains the error-messages form the serverside (php). so all you have to do is:

    success: function(result) {
                 alert(result);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services