2014-02-06 106 views
-1

從PHP得到迴音我有一個formsubmit button 發送一些input值 每POST到PHP文件。 PHP驗證和迴應一個值。 如何處理每個jquery的這個echo值(在jquery函數中)?提交表單和jQuery的

示例代碼:

 <form id="login" method="post" action="/php/login.php"> 
      <input type="text" id="username" name="username" placeholder="Username" autofocus="true"></input> 
      <input type="password" id="password" name="password" placeholder="Password"></input> 
      <button id="loginButton" type="submit" name="submit">Login</button> 
     </form> 

假裝/php/login.php是

<?php 
    echo "OK"; 
?> 
現在

,我想一定有辦法讓某種形式的該回波值在jQuery中提交回調?

+1

這可能幫助:['jQuery.ajax()'](HTTP://api.jquery。 com/jquery.ajax /) – vee

+0

你有你自己的任何代碼來顯示嗎? – thescientist

回答

0

好吧,我發現我不得不躲避實際提出並做了單獨的後

$("#login").submit(function(event) { 
    event.preventDefault(); 
    var myUsername = … 
    var myPassword = … 
    $.post("/php/login.php", { username:myUsername, password:myPassword }, function(data){ 
     alert(data); // which is the php echo. 
    }); 

});