PhoneGap的登录表单 [英] PhoneGap login form

查看:132
本文介绍了PhoneGap的登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的PhoneGap的应用程序,一个简单的登录表单。由于PhoneGap的不能在客户端使用任何PHP我必须使用AJAX来将数据发送到服务器。我发现了一个简单的PHP登录教程,但我真的不知道如何得到它使用Ajax的工作。

I'm trying to make a simple login form for my PhoneGap app. Since PhoneGap can't use any PHP on the client side I have to use AJAX to send the data to server. I found a simple PHP login tutorial but I really don't know how to get it to work with ajax.

我知道如何发布值到PHP文件,但如何将用户重定向到网页,如果用户名和密码是正确的,或进入网站,如果信息是无效的否定呢?

I know how to post values to the PHP file, but how can I redirect the user to page if username and password are right or deny them from entering the site if the information is invalid?

这是在code我现在:

This is the code I have right now:

JS

jQuery.ajax({ 
    type: "POST", 
    url: serviceURL + "login.php", 
    data: 'username='+username+'&password='+password,  
    cache: false,
        }

的login.php

login.php

<?php
include 'config.php';

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM users WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
//  redirect 
header("location:settings.html");
}
else {
echo "Wrong Username or Password";
}
?>

那么,如何可以让如果($数== 1){.... 部分采取行动对我的应用程序

So how can I make the if($count==1){.... part to take action on my app

推荐答案

首先你必须设置要检索(比如以HTML)预期的数据类型。 之后,你也必须对检索到的数据指定的函数,你suceeded检索数据之后...

first you have to set the expected dataType you want to retrieve (for example to html). After that you have to also specify a function on the retrieved data after you suceeded retrieving the data...

事情是这样的:

jQuery.ajax({ 
    type: "POST", 
    success: function(data) { // After retrieving the data from the php-file
    alert(data);}
    url: serviceURL + "login.php", 
    data: 'username='+username+'&password='+password,  
    cache: false,
    dataType:'html' // Expected return data type...
}

数据变量包含从PHP文件返回的字符串,所以你必须呼应出来的东西与PHP脚本,如登录,然后你必须过滤器/检查此字符串JavaScript的...

The data Variable contains the return string from the php file, so you have to echo something out with the php script, like "logged in" and then you have to filter / check this string with javascript...

卢西恩

这篇关于PhoneGap的登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆