1

I've been trying to trigger an effect through PHP. Basically, when the user enters an invalid password, I want to make the submit button shake. To do that, I need to go through PHP, and attempt to validate the user in my database, if that fails, the following code is supposed to trigger a jQuery effect.

echo'<script>$(".shake").effect("shake", {times:2, distance:3, direction:"right"}, 45);</script>';

I can see why this might not work, but I don't see another way to do it.

1
  • What else does the page print? Is the JS printed in the head or after the DOM .shake object? Commented Aug 4, 2012 at 23:24

2 Answers 2

1

You need AJAX for that. Client asks the server whether the password is correct by AJAX, then in response to the result of that shakes the button (or not). Something like this:

$.ajax("http://www.example.com/ajax.php", {
  data: {
    username: username,
    password: password
  },
  success: function(data) {
    if (data.okay) {
      loggedIn = true;
    } else {
      $(".shake").effect("shake", {times:2, distance:3, direction:"right"}, 45);    if (data == "OK");
    }
  }
};

and in ajax.cgi:

echo "Content-Type: application/json\n\n"
$username = $_GET("username");
$password = $_GET("password");
if (authenticate($username, $password)) {
  echo "{ \"okay\": true }";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use AJAX for this.

$('#submit').on('click', function() {
    $.ajax({
        url: "your/auth/script.php",
        type: "POST",
        success: function(result) {
            if(result !== 1) {
                $(".shake").effect("shake", {times:2, distance:3, direction:"right"}, 45);
            }
        }
    });
});

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.