duanguochong0397 2012-08-04 15:20
浏览 182
已采纳

使用PHP(echo)触发jQuery事件?

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.

  • 写回答

2条回答 默认 最新

  • dongpeng8994 2012-08-04 15:23
    关注

    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
    
    "
    $username = $_GET("username");
    $password = $_GET("password");
    if (authenticate($username, $password)) {
      echo "{ \"okay\": true }";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?