2

我目前正在使用 Kendo UI 制作一个 iPhone 应用程序,我正在通过电话间隙在我的 iPhone 上进行测试。

设计都很好地绘制出来,我开始掌握 Kendo 框架。我正在尝试制作一些功能,让他们登录帐户。

我的外部 PHP 文件运行查询并返回 JSON:

<?php

   $arr = array();

//Takes the username and password from the login form and queries the database to find out if they have access to the site.


        //Cleanse inputs
        $username = $_GET['username'];
        $password = md5_base64($_GET['password']);

        $stmt = $memberMysqli->prepare("SELECT id, firstname, dob, sex, recordingWeight, blocked, enabled FROM member WHERE email = ? AND password = ?");
        $stmt->bind_param('ss', $username, $password);
        $stmt->execute();
        $stmt->bind_result($memberid, $firstname, $dob, $sex, $recordingWeight, $blocked, $enabled);
        $stmt->store_result();  

        session_start();

        while ($stmt->fetch())
        {
            $userIsBlocked = $blocked;
            $enabled = $enabled;
        }   

        if(($numRows = $stmt->num_rows) > 0)  //If num rows is  1 the combination exists therefore it is a succesful login
        {   
            if($userIsBlocked)
            {
                $arr['status'] = "error";
                $arr['message'] = "Sorry, your account isnt active. Please contact us to re-activate it.";
            }
            else if(!$enabled)
            {
                $arr['status'] = "error";
                $arr['message'] = "Sorry, your account isn't enabled. Please contact us.";
            }

            else
            {
                    $_SESSION['memberid'] = $memberid;
                    $_SESSION['memberFirstname'] = $firstname;
                    $_SESSION['dob'] = $dob;
                    $_SESSION['sex'] = $sex;
                    $_SESSION['recordingWeight'] = $recordingWeight;

                    $arr['status'] = "success";
                    $arr['message'] = "Logged in";
            }
        }
        else
        {
            $arr['status'] = "error";
            $arr['message'] = "Sorry, Wrong Username/Password Combination";                 
        }
header("Content-type: application/json");   
echo json_encode($arr);
/* close connection */
function md5_base64 ( $data ) 
{ 
    return preg_replace('/=+$/','',base64_encode(md5($data,true))); 
} 


?>

所以这会返回成功,登录或抱歉错误的用户名/密码组合..

这是我的表单代码:

<form>

            <fieldset>

                <p><label style="color:white;" for="email">E-mail address</label></p>
                <p><input type="email" id="email" value=""></p> 

                <p><label style="color:white; font" for="password">Password</label></p>
                <p><input type="password" id="password" value=""></p> 

                <p><input type="submit" value="Sign In"></p>

            </fieldset>

和JS:

<script>

        $("form").on("submit", function() {

        var username = document.getElementById('email').value;
        var password = document.getElementById('password').value;

        var dataSource = new kendo.data.DataSource({
          transport: {
            read:  {
             url: 'http://myurl.co.uk/appqueries/login.php?username='+username+'&password='+password,
             dataType: "json" 
          }
          }
        });

        //alert("Your username is "+username+" and your password is: "+password);

        });
    </script>

任何人都可以帮我获取 PHP 文件返回的 JSON 内容,然后在登录成功时让用户进入应用程序,或者在登录失败时显示消息。

4

2 回答 2

2

我不认为你想要一个 DataSource (它可以完成,但 DataSource 需要一个来自读取操作的对象数组),除非有额外的要求。

如果这是您的 HTML:

<input id='username' type='text' value='user'></input>
<input id='password' type='text' value='password'></input>
<button id='loginbutton'>Login</button>
<div id='loginresult'></div>

然后你可以使用 jQuery(我假设你正在使用它,因为它是 Kendo UI 的要求):

function loginClick() {
    var username = $("#username").val();
    var password = $("#password").val();
    var loginUrl = 'http://myurl.co.uk/appqueries/login.php?username='+username+'&password='+password;

    $.ajax({
        url: loginUrl,
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            handleLoginResult(data);
        }
    });
}

$(document).on("click", "#loginbutton", loginClick);

function handleLoginResult(data) {
    var status = data.status;
    var message = data.message;

    if (status === "success") {
        // do whatever needs to be done after successful login
        $("#loginresult").html(message);
    }
}

在这里查看一个工作示例(有一些差异,因为这是使用 jsfiddle 的 echo api):http: //jsfiddle.net/lhoeppner/9TGJd/

这对 Kendo Mobile 几乎是一样的,除了你会使用移动按钮和数据点击绑定:

  <a id="loginbutton" data-role="button" data-click="loginClick">Login!</a>
于 2013-10-14T14:53:59.167 回答
1

您不应该在 Kendo 移动应用程序中使用表单提交,因为 Kendo 移动应用程序基本上是一个单页应用程序。您需要做的是拥有一个 Kendo 按钮并在 click 事件处理程序上触发 JSON 调用。你可以在这里看到剑道按钮点击事件的演示:http ://demos.kendoui.c​​om/mobile/button/events.html#/

于 2013-10-08T01:15:20.460 回答