-1

Ok so I'm trying to create a login form for an iOS application using Phonegap. In order to have users sign in, I need to work around the SOP. I've tried a number of tutorials and examples, and I can't get any of them to work. This is what I have right now, I could use some pointers:

<script type="text/javascript">
$(document).ready(function() {

$("#login").click(function() {

    var action = $("#form1").attr('action');
    var form_data = {
        username: $("#username").val(),
        password: $("#password").val(),
        is_ajax: 1
    };

    $.ajax({
        type: "POST",
        url: action,
        data: form_data,
        success: function(response)
        {
            if(response == 'success')
                $("#form1").slideUp('slow', function() {
                    $("#message").html("You have logged in successfully!");
                });
            else
                $("#message").html("Invalid username and/or password.");    
        }
    });

    return false;
});

});
</script>

------------ on doLogin.php -----

<?php

$is_ajax = $_REQUEST['is_ajax'];
if(isset($is_ajax) && $is_ajax)
{
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];

    if($username == 'demo' && $password == 'demo')
    {
        echo "success"; 
    }
}

?>
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
ejn
  • 17
  • 2
  • in what specific regard you are having the problem – Rafay May 15 '12 at 19:41
  • ejn, try using your debugger to find the source of the problem yourself. Then, if you're still stuck, you'll have a much more *specific* question that you can ask here, and you'll likely get better answers and more help once you've narrowed the problem down yourself :) Good luck! – jamesmortensen May 15 '12 at 19:42
  • sorry, i was just wondering if anyone could give me some suggestions as to what I should change. Im stuck with what you see and it does not work. – ejn May 15 '12 at 19:43
  • @ejn: Please be specific in what *specifically* doesn't work. What exactly happens, do you get any error messages, etc. – Colin Brock May 15 '12 at 19:44
  • 2
    That' understandable, but keep in mind we're volunteers, so you should try, if possible, to narrow the question down yourself, or at least tell us where you think the problem is and provide lots of detail. I think you have a great start, just continue to edit if you think of more details. For instance, posting the error messages you're seeing can really go a long way to helping us help you. Hope that helps! :) – jamesmortensen May 15 '12 at 19:45

1 Answers1

-1

First thought: your code is less error prone when using curly braces for your conditionals

if(response == 'success')
    $("#form1").slideUp('slow', function() {
        $("#message").html("You have logged in successfully!");
    });
else
    $("#message").html("Invalid username and/or password.");    

could be

if(response == 'success') {
    $("#form1").slideUp('slow', function() {
        $("#message").html("You have logged in successfully!");
    });
} else {
    $("#message").html("Invalid username and/or password.");    
}

A user here explains why this helps: https://stackoverflow.com/a/2125078/680578

Community
  • 1
  • 1
Kristian
  • 21,204
  • 19
  • 101
  • 176
  • Curly braceless conditionals work for a single *statement*, regardless of the number of lines. – connec May 15 '12 at 19:55
  • interesting, i've confused *statement* with *lines*. I didn't know that until now. I usually operate with this type of understanding about braceless conditionals; http://stackoverflow.com/a/2125078/680578 – Kristian May 15 '12 at 20:00