2012-08-04 139 views
1

我一直在試圖通過PHP來觸發一個效果。基本上,當用戶輸入無效密碼時,我想讓提交按鈕動搖。要做到這一點,我需要通過PHP,並嘗試驗證我的數據庫中的用戶,如果失敗,下面的代碼應該觸發jQuery效果。使用PHP(回聲)觸發jQuery事件?

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

我可以看到爲什麼這可能無效,但我沒有看到另一種方式來做到這一點。

+0

爲什麼不通過ajax? – 2012-08-04 23:22:57

+0

該頁面還打印了什麼? JS是打印在頭部還是在DOM'.shake'對象之後? – Kurt 2012-08-04 23:24:01

回答

1

你需要AJAX。客戶端通過AJAX詢問服務器密碼是否正確,然後響應按鈕(或不按鈕)的結果。事情是這樣的:

$.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"); 
    } 
    } 
}; 

ajax.cgi

echo "Content-Type: application/json\n\n" 
$username = $_GET("username"); 
$password = $_GET("password"); 
if (authenticate($username, $password)) { 
    echo "{ \"okay\": true }"; 
} 
0

您需要使用AJAX這一點。

$('#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); 
      } 
     } 
    }); 
});