0

HTML:

<form id="loginForm" data-ajax="false" action="">
            <div data-role="fieldcontain">
                <label for="username">Username:</label>
                <input id="username" type="text" name="usern" placeholder="Username" />
            </div>
            <div data-role="fieldcontain">
                <label for="password">Password:</label>
                <input id="password" type="password" name="passw" placeholder="Password" />
            </div>
            <div data-role="fieldcontain">
                <input type="button" id="login" value="Login" />
            </div>
</form>
<!-- login page closing tags go here -->

PHP:

    <?php
      $data = mysqli_query($conn, "SELECT * FROM users WHERE username='$username' AND password='$password'");

      if($userResult == 0){
        $data_response["status"] = "error";
       }
    ?>

The connection works and database is successfully queried. I am unsure as to why this has not been working, suggestions would be much appreciated.

anon
  • 1
  • 1
  • Your script outputs more than just JSON, so likely it simply goes into the error handler because parsing the response as JSON fails. – CBroe Mar 01 '17 at 13:53
  • put exit; after echo json_encode($data_response); – Naincy Mar 01 '17 at 13:59
  • 1
    remove var_dump($_POST); – Steven Johnston Mar 01 '17 at 14:02
  • Thank you @StevenJohnston, but now the result returned is empty. Possible error within php? – anon Mar 01 '17 at 14:14
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 01 '17 at 15:00
  • @StevenJohnston — Rubbish. The `application/x-www-form-urlencoded` encoding format is the same as used in query strings, which has been the default for POST forms since they were introduced in HTML 2 – Quentin Mar 01 '17 at 15:01
  • try add `encode : true` in ur ajax – Masivuye Cokile Mar 01 '17 at 15:05

1 Answers1

1

remove:

var_dump($_POST);

"username" must be the same in $_POST['username'] and in name="username"

//php
$_POST["username"]

//html
<input id="username" type="text" name="username" placeholder="Username" />
Steven Johnston
  • 1,729
  • 13
  • 16
  • it works, but now if I input credentials that do not exist within my database, the success function is executed? – anon Mar 01 '17 at 14:35
  • To tell the client there has been an error you need to change the response header. http://stackoverflow.com/a/12018482/5348487 – Steven Johnston Mar 01 '17 at 14:36
  • I have made the changes but problem still persists, now the success function is always called. – anon Mar 01 '17 at 14:42
  • The success function in javascript right? Can you log the response Or repost your php. – Steven Johnston Mar 01 '17 at 14:56
  • "Using POST you feed Jquery an array not a query string for the parameters. So rather than using .serialize use .serializeArray. " — This is rubbish. `serialize` is just fine. – Quentin Mar 01 '17 at 15:03
  • Yes the success function is in javascript as seen in code I have posted. I see **Object {status: "success", message: "successful"}** in console log of browser(chrome) and my php has not changed. – anon Mar 01 '17 at 15:10
  • try mysqli_num_rows($userResult ) == 0 instead of $userResult == 0 – Steven Johnston Mar 01 '17 at 15:19
  • When using credentials that do exist ,the user is navigated to the homepage. However, upon adding your code to the php, when entering the wrong info, the success function is executed but the user is not navigated to the homepage, which is what I want. However, am I right in thinking the error function should be called if this happens? – anon Mar 01 '17 at 15:29
  • If you want the error function to be called you must change the response header as i mentioned before. Adding this when the user login failed should work (php): header('HTTP/1.1 401 Unauthorized', true, 401); – Steven Johnston Mar 01 '17 at 15:39
  • Thank you, everything works correctly, your help has been much appreciated. – anon Mar 01 '17 at 15:56