0

I am having troubles again with my ajax and php code. The statement I have not been able to get a successful statement. Here are my codes

            <script type="text/javascript">
            $(document).ready(function(){
                $("#login_a").click(function(){
                    $("#shadow").fadeIn("normal");
                     $("#login_form").fadeIn("normal");
                     $("#Username").focus();
                });
                $("#cancel_hide").click(function(){
                    $("#login_form").fadeOut("normal");
                    $("#shadow").fadeOut();
               });
               $("#login").click(function(){

                    username=$("#Username").val();
                    password=$("#Password").val();
                     $.ajax({
                        type: "POST",
                        url: "1.php",
                        data: "Username="+username+"&Password="+password,
                        success: function(html){
                          if(html=='1')
                          {               $("#login_form").fadeOut("normal");
                            $("#shadow").fadeOut("normal");
                          document.location = '/reward/home.php';
                          }
                          else
                          {
                                $("#add_err").html("Wrong Username or Password");
                          }
                        },
                        beforeSend:function()
                        {
                             $("#add_err").html("Loading...")
                        }
                    });
                     return false;
                });
            });
            </script>
         </head>
         <body>
            <div id="login_form">

                    <form action="" id="loginForm">
                        <label>User Name:</label>
                        <input type="text" id="Username" name="Username" />
                        <label>Password:</label>
                        <input type="password" id="Password" name="Password" />
                        <label></label><br/>
                        <input type="submit" id="login" value="Login" />
                        <input type="button" id="cancel_hide" value="Cancel" />
                    </form>
                <div class="err" id="add_err" style="color:#FF0000; font-weight:bold"></div>
                </div> 

My SQL connect is all located in my function file. When I by pass the ajax and go from the form to the dologin.php it echo s "1" which should mean success and log me in.
Here is my dologin.php file...

            $root = $_SERVER['DOCUMENT_ROOT'];
            include($root."/reward/function.php");


                $Username = mysql_real_escape_string($_REQUEST['Username']);
                $Password = mysql_real_escape_string($_REQUEST['Password']);
                $Password2 = ($Password);
                $sql = mysql_query("SELECT * FROM PERSON WHERE Username = '$Username' AND Password = '$Password2'") or die(mysql_error());
                $sql2 = mysql_query("SELECT * FROM PERSON WHERE Username = '$Username'");
                if(mysql_num_rows($sql)==1){
                    echo "1";
                    while($info = mysql_fetch_assoc($sql)){
                        $FName = $info['FName'];
                        $_SESSION['FName24'] = $FName;
                    }
                }
                elseif (mysql_num_rows($sql)>1){
                    echo "2";
                }
                    elseif (mysql_num_rows($sql)==0){
                    echo "0";
                } ?>
G-Yo
  • 96
  • 1
  • 9
  • What is the output of mysql_error()? And shoud'nt you enclose table name in ``? – Shubham Apr 06 '12 at 14:02
  • There is no mysql error the form returns Wrong Username and Password even though I am typing in correct creditials – G-Yo Apr 06 '12 at 14:04

1 Answers1

0

When you are using Google Chrome, hit F12 and go to tab Console. from there you could see the errors of your pages.

Put this code here

             $.ajax({
                    type: "POST",
                    url: "1.php",
                    data: "Username="+username+"&Password="+password,
                    success: function(html){
                      console.log(html)
                      if(html=='1')
                      {               $("#login_form").fadeOut("normal");
                        $("#shadow").fadeOut("normal");
                      document.location = '/reward/home.php';
                      }
                      else
                      {
                            $("#add_err").html("Wrong Username or Password");
                      }
                    },
                    beforeSend:function()
                    {
                         $("#add_err").html("Loading...")
                    }
                });
                 return false;
            });

You should see your error

Neysor
  • 3,893
  • 11
  • 34
  • 66
Peter Wateber
  • 3,824
  • 4
  • 21
  • 26
  • SyntaxError: Unexpected token ) is the response in Google Console When I refresh the page it says Uncaught TypeError: object is not a function (anonymous function) – G-Yo Apr 06 '12 at 14:07
  • There should be something wrong with your "1.php" I guess. I didn't see anything wrong with your syntax error with the code you posted. – Peter Wateber Apr 06 '12 at 14:10
  • I actually changed the 1.php to dologin.php which the code is above the 1.php is a simple PHP file that just echoes 1 and that gives me a successful return but the dologin does not echo 1 – G-Yo Apr 06 '12 at 14:17
  • oohhh I get it.. change these lines: $Username = mysql_real_escape_string($_REQUEST['Username']); $Password = mysql_real_escape_string($_REQUEST['Password']); to these: $Username = mysql_real_escape_string($_POST['Username']); $Password = mysql_real_escape_string($_POST['Password']); – Peter Wateber Apr 06 '12 at 14:27
  • I changed it but it still doesn't work :( Google Console says Uncaught TypeError: object is not a function – G-Yo Apr 06 '12 at 14:35
  • have you tried echoing the those lines? $_POST["username"]? $_POST["password"]? – Peter Wateber Apr 06 '12 at 14:36
  • ha! try removing mysql_real_escape_string($_POST["Username"]) just use $_POST["username"]... – Peter Wateber Apr 06 '12 at 14:44
  • for more info about what you're doing http://php.net/manual/en/function.mysql-real-escape-string.php by the way just a tip what you're doing about the query is a bad practice try looking at the example on the link – Peter Wateber Apr 06 '12 at 14:55
  • I will hash the password how else can I improve it? – G-Yo Apr 06 '12 at 14:58