I made a basic HTML/PHP login page, and jQuery mobile login page. I want to make them both submit to the same file called checklogin.php, intended to redirect them to their respective main pages if a login is successful. It works successfully for the basic page, but not the jQuery Mobile page. Trying to submit via the mobile page takes me to a page with the phrase "undefined" on it.
So instead, I tried to write some PHP and jQuery script so that if a mobile post is detected, instead of immediately going to the main page, it would return that it was successful, and the login page itself would redirect the user to the main page. Although clicking submit yields a successful "form is sent" alert for testing purposes, a correct login does not make the page change. What should I add?
This is my PHP login checker.
<?php
$host=// Host name
$username=// Mysql username
$password=// Mysql password
$db_name=// Database name
$tbl_name=// Table name
// 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['myusername'];
$mypassword=$_POST['mypassword'];
// To prevent MySQL injection (a form of internet hacking)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name 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)
{
// Register $myusername, $mypassword and redirect to the main page"
session_register("myusername");
session_register("mypassword");
//Send them back to the page they were at/
if(!$_POST['mobile'])
{
header("location:main.php");
}
else
{
header('HTTP/1.0 401 Unauthorized', true, 401);
}
}
else
{
echo "Wrong Username or Password";
}
?>
And this is my script back on the login page for redirecting the user.
<script language="javascript">
// Global declarations - assignments made in $(document).ready() below
var hdrMainvar = null;
var contentMainVar = null;
var ftrMainVar = null;
var contentTransitionVar = null;
var stateLabelVar = null;
var whatLabelVar = null;
var stateVar = null;
var whatVar = null;
var form1var = null;
var confirmationVar = null;
var contentDialogVar = null;
var hdrConfirmationVar = null;
var contentConfirmationVar = null;
var ftrConfirmationVar = null;
var inputMapVar = null;
// Constants
var MISSING = "missing";
var EMPTY = "";
var NO_STATE = "ZZ";
function getFormValues() {
data = {};
data['username'] = $("#username").val();
data['password'] = $("#password").val();
return data;
}
function transmitPost() {
$.ajax({
url: 'checklogin.php',
type:"post",
data: getFormValues(),
statusCode: {
401: function() {
alert("Wrong Password");
},
200: function() {
window.location = "m/main.php";
alert('Submitted');}
}
error: function(value) {
alert("error");
}
});
}
$('#submitButton').bind('click', function() {
alert('User clicked on "foo."');
});
</script>
I haven't reviewed all the code yet but when you submit a form to a page not jqm you should add data-ajax="false"
to the <form>
tag. Saying jqm it should load the page normally and not interpret it as content for the jqm page. That resolved the 'undefined' thing for me...
header('HTTP/1.0 401 Unauthorized', true, 401);
- Dion 2012-04-04 23:58