Phonegap ajax和php

时间:2012-11-14 17:03:11

标签: php jquery ajax cordova

我是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 here

<?//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文件确认。

1 个答案:

答案 0 :(得分:1)

您应该可以访问

window.localStorage["username"]
你的some.html页面上的