0

I want to validate a password via JavaScript with help of an Ajax function.

If it is successful, I want to pass back the variable (boolean, true or false) and do something in my PHP file depending on the callback.

But this doesn't work. Here is my code:

PHP file: update.php

<input href="javascript:void(0);" role="button" ype="submit" value="Submit" onclick="ValidatePassword()>'

JAVASCRIPT: ValidatePassword()

In my Javascript function I check the password with this ajax call and if it is successfull, it should callback the result to the php function.

 $.ajax({
    type: "POST",
    url: "checkpw.php",
    data: dataString,
    cache: false,
    success: function(response)
    {
        if (result != -1 )
        {
            $("#passwd").val('');

            // RETURN TO PHP FILE update.php -> PW IS VALID
        } else {
            // RETURN TO PHP FILE update.php -> PW IS INVALID
        }
    }
});

PHP file: update.php

Now I want to use the callback in the php function like:

<?php

if (passwordCallback == true)
...
else
...

?>

What should I do in the ajax success function to return the value to my php file?

5
  • you can use json_enode or simply echo 'success' of 'fail' in update.php, use response to get the value Commented Mar 22, 2016 at 9:05
  • 1
    Also, please notice that the parameter in success: function is response while in your condition you're using result. Commented Mar 22, 2016 at 9:06
  • @Devs: Can you explain how to use json or echo in this case? Commented Mar 22, 2016 at 9:18
  • success: function(response) { if (result != -1 ) why are you checking "result"when you have "response" variable? Commented Mar 22, 2016 at 9:41
  • Don't do this. Anyone can call your update.php and pretend the password is valid if you go via JavaScript. You should instead call update.php via checkpw.php and send the response directly from that. Commented Mar 22, 2016 at 10:30

2 Answers 2

1

As I suggested in the comments, if this is not coded correctly it can lead to security issues. If it is coded correctly then it will end up doing the password check twice when it only needs to be done once.

Instead what you could do is:

 $.ajax({
    type: "POST",
    url: "checkandupdate.php", //Combination of both
    data: dataString,
    cache: false,
    success: function(response)
    {
        if (result != -1 ) {
            $("#passwd").val('');    
        }
    }
});

File checkandupdate.php

<?php
require "checkpw.php"; // Or whatever you need to do to validate the password
// At this point "checkpw.php" determined if the password is valid and(ideally) you can check the outcome
//Assume we store the outcome of the check in $passwordIsValid as a boolean indicating success
if ($passwordIsValid) {
    //Do whatever you need to do when the password is valid
    echo "1"
}
else {
   // Do whatever you need to do when the password is invalid
   echo "-1";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to write a JavaScript function like:

function sendReturnToPHP(url, result) {
  $.ajax({
    type: "POST",
    url: url,
    data: JSON.parse(result),
    cache: false,
    success: function(response) {}
  });
}

Now you can easily call it in your request success.

Comments

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.