0

我是 jquery 和 phonegap 的新手,我无法在任何地方找到我的问题的答案。

这是我的 index.html

    <!DOCTYPE HTML>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">    
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Auth Demo 2</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css" />
    <script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
    <script src="jquery.mobile/jquery-1.7.2.min.js"></script>
    <script src="jquery.mobile/jquery.mobile-1.1.0.min.js"></script>
    <script src="main.js"></script>
</head>

<body onload="init()">

<div id="launcherPage" data-role="page">
    <!-- I'm just here waiting for deviceReady -->
</div>

<div id="loginPage" data-role="page">

    <div data-role="header">
        <h1>Auth Demo</h1>
    </div>

    <div data-role="content">    

        <form id="loginForm">
        <div data-role="fieldcontain" class="ui-hide-label">
            <label for="username">Username:</label>
            <input type="text" name="username" id="username" value="" placeholder="Username" />
        </div>

        <div data-role="fieldcontain" class="ui-hide-label">
            <label for="password">Password:</label>
            <input type="password" name="password" id="password" value="" placeholder="Password" />
        </div>

        <input type="submit" value="Login" id="submitButton">
        </form>

    </div>

    <div data-role="footer">
        <h4>&copy; Camden Enterprises</h4>
    </div>

</div>

</body>
</html>

还有我的 Js。

function init() {
    document.addEventListener("deviceready", deviceReady, true);
    delete init;
}

function checkPreAuth() {
    console.log("checkPreAuth");
    var form = $("#loginForm");
    if(window.localStorage["username"] != undefined && window.localStorage["password"] != undefined) {
        $("#username", form).val(window.localStorage["username"]);
        $("#password", form).val(window.localStorage["password"]);
        handleLogin();
    }
}


function handleLogin(){

    var form = $("#loginForm");
    var u = $("#username", form).val();
    var p = $("#password", form).val();


                                              //remove all the class add the messagebox classes and start fading

    if(u != '' && p!= '') {
        $.post("http://www.myaddress.com/loginlogin.php",{ user_name:$('#username', form).val(),password:$('#password', form).val(),rand:Math.random() } ,function(data)

                                                     {
                                                     if(data=='yes') //if correct login detail
                                                     {
                                                     //store
                                                     window.localStorage["username"] = u;
                                                     window.localStorage["password"] = p;
                                                    // $.mobile.changePage("some.html");
                                                    $.mobile.changePage( "some.html", { transition: "slideup"} );

                                                     }
                                                     else
                                                     {
                                                     navigator.notification.alert("Your login failed", function() {});
                                                     }
                                                     });

    } else {
                //Thanks Igor!
                navigator.notification.alert("You must enter a username and password", function() {});
                $("#submitButton").removeAttr("disabled");
           }
                                              return false;//not to post the  form physically



}



function deviceReady() {
    console.log("deviceReady");
    $("#loginPage").on("pageinit",function() {
        console.log("pageinit run");
        $("#loginForm").on("submit",handleLogin);
        checkPreAuth();
    });
    $.mobile.changePage("#loginPage");
}

这不是我自己的工作,但从这里 http://www.raymondcamden.com/index.cfm/2011/11/10/Example-of-serverbased-login-with-PhoneGap

我将示例更改为使用 php。这非常简单,仅用于测试目的

php在这里

<?//get the posted values

    require_once("backend/functions.php");
    dbconn(true);

    $username = $_POST['user_name'];

    if ($username=='Steven'){
                    echo "yes";
    }else{

        echo "no";
    }



    ?>

现在这一切正常,当满足条件时,页面 some.html 将打开。

现在我的问题是。我如何将登录人的用户名发送到 some.html 页面?一旦从 php 文件中确认。

4

1 回答 1

1

您应该可以访问

window.localStorage["username"]

在您的 some.html 页面上

于 2012-11-14T17:07:33.427 回答