簡體   English   中英

單擊提交按鈕時無法發布/錯誤

[英]Cannot POST / error when clicking Submit button

我正在使用Phone gap為我們的移動應用程序開發一個登錄頁面。 但是,每當我單擊“提交”按鈕時,都會出現錯誤“無法發布/”。 為什么會這樣? 請在下面查看我的代碼。

index.html

<body>
    <div class="main-info2">
        <h3>Sign In</h3>
            <div class="in-form">
                <form id="login_form" method="post"">
                    <input type="text" placeholder="Username" required=" " id="email" />
                    <input type="password" placeholder="Password" required=" " id="password" />
                    <div class="check-sub">
                        <input type="submit" value="Login" id="login">
                    </div>
                </form>
            </div>
        </div>
        <div class="copy-right">
            <p>Design by <a href="http://w3layouts.com">W3layouts</a></p>
        </div>
    </div>
    <!-- //main -->
</body>

login.js

$(document).ready(function(){
    do_login();
});

function do_login() {
    $("#login").click(function () {
        var email = $('#email').val();
        var password = $('#password').val();
        var dataString="email="+email+"&password="+password+"&login=";

        if($.trim(email).length>0 & $.trim(password).length>0){
            $.ajax({
                type: 'POST',
                url: "http://localhost:1234/cleverpro/login.php",
                dataType: 'json',
                data: dataString,
                crossDomain: true,
                cache: false,
                beforeSend: function(){ $("#login").html('Connecting...');},
                success: function(data){
                    if(data == "success"){
                        console.log("hay nako!");
                        alert("hala");
                    }else if(data == "failed"){
                        $("#login").html('Login');
                        console.log("hastang sayupa");
                        alert("halajud");
                    }   
                }
            });
        }else{
            return false;
            console.log("walay sulod uy");
        }
    });
}

login.php

<?php
include "db.php";

if(isset($_POST['login'])){
    $email=mysql_real_escape_string(htmlspecialchars(trim($_POST['email'])));
    $password=mysql_real_escape_string(htmlspecialchars(trim($_POST['password'])));
    $login=mysql_num_rows(mysql_query("select * from `user` where `email`='$email' and `password`='$password'"));

    if($login!=0){
        echo "success";
    }else{
        echo "failed";
    }
}

?>

我不知道我在哪里出錯了。 請幫我。

首先,最好在單擊時使用on('submit',handler)。 (請參閱onclick和onsubmit有什么區別?

當您單擊“登錄”時,默認的表單發送操作將在您的JavaScript代碼完成后發生。 這意味着表單POST發生在表單的“操作”中,表單的“操作”未在表單中設置,默認情況下為“ /”。

您需要阻止Default操作,像這樣

$('#login_form').on('submit', function(e) {
    e.preventDefault();
    //Your code
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM