0

I have some code that sends a variable (pin) to php via AJAX the database is then queried and if a result is found the php echo's a value of 1. Everything is working fine, except that the Ajax does not recognise the value returned by the php.

Here is my code

$(document).ready(function () {
    $("form.submit").submit(function () {
        var pin = $(this).find("[name='pin']").val();

        // ...

        $.ajax({
            type: "POST",
            url: "http://www.example.com/pin.php",
            data: {
                pin : pin,
            },
            success: function (response) {
                if (response == "1") {
                    $("#responsecontainer").html(response);

                    window.location.href = "home.html?user=" + user;
                    // Functions
                } else { // Login failed
                    alert("LOGIN FAILED");
                }
            }
        });

        this.reset();

        return false;
    });
});

And here is my PHP code, I know that the code below returns a value of 1. When Ajax is triggered it returns a value that generates a login fail message. Is there a way to see what Ajax is sending, if i swap out the ajax and directly submit the for to the server it also returns a 1 on the php echo.

 $pin = $_GET["pin"];
 $db = new PDO("mysql:host=localhost;dbname=xxxxx;charset=utf8", "xxxx", "xxxx");
 $count = $db->query("SELECT count(1) FROM users WHERE pin='$pin'")->fetchColumn();
 echo $count;
2
  • 1
    You send ajax request in POST, so you must use $_POST instead $_GET in your PHP script to be able to retreive request parameters Commented Apr 24, 2018 at 21:00
  • 1
    Your code is vulnerable to SQL injection attacks. You should use mysqli or PDO prepared statements with bound parameters as described in this post. Commented Apr 25, 2018 at 7:34

1 Answer 1

0

It's recommended to return JSON data as result for an ajax request.

So try this :

Edit: I've updated the php code to make the sql query with PDO prepare() method taking into account @Dominik's commentary

$pin = $_POST['pin'];
$db = new PDO('mysql:host=localhost;dbname=xxxxx;charset=utf8', 'xxxx', 'xxxx');
$stmt = $pdo->prepare('SELECT count(1) FROM users WHERE pin = :pin');
$stmt->execute(array('pin' => $pin));
return json_encode([
  "count" => $stmt->fetchColumn()
]);

And in your ajax success callback :

...
success: function(response) {
  var count = JSON.parse(response).count;
  if (count == "1") {
    $("#responsecontainer").html(response);
    window.location.href = "home.html?user="+ user;
  } else {// Login failed
    alert("LOGIN FAILED");
  }
},
error: function(error) {
  ...
}

Hope it's helps you :)

Sign up to request clarification or add additional context in comments.

5 Comments

do i add the var count section instead of the success section in the AJAX
Thanks for the edit, i am getting the issue of unexpected end of input now on the end of the Javascript code, and can not seem to resolve it
You can see this stackoverflow question to try to resolve your new issue
thanks that helped, i had searched but hadnt' found that one. When i run the code above i am getting a 500 error for the php?
no, it just returns a 500 error, nothing in console, no error reporting, its weird.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.