2013-02-27 57 views
0

這裏是我的代碼:

<div id="test"></div> 

<form id="form-lecturer-login"> 

    <table> 
     <tbody> 
     <tr> 
      <td> 
       <label class="label">Username : </label><br> 
       <input name="username" value="" type="text"> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <label class="label">Password : </label><br> 
       <input name="password" value="" type="text"> 
      </td> 
     </tr> 
    </tbody> 
    </table> 
    <input value="Log In" type="submit"> 
</form> 

<script type="text/javascript"> 
$('#form-lecturer-login').submit(function(){ 
    $.post("../php/lecturer_login.php",$(this).serialize(), 
    function(data){ 
    $('#test').html(data[0]).show(); 
    }, "json"); 
}); 
</script> 
在lecturer_login.php

<?php 
$stack = array(); 
array_push($stack,"test"); 
echo json_encode($stack);  
?> 

注:我明白我路過,我不使用的數據。僅用於測試目的。

'test'div不會將文本更改爲「測試」。

我正在學習這個例子,我遵循jQuery文檔。我錯過了什麼? 有沒有包括除外:

<script type="text/javascript" src="../js/jquery-1.4.2.min.js" ></script> 
在文件頭

,我需要?

+3

您不妨礙表單的自然提交,因此您永遠無法看到結果。 – 2013-02-27 20:56:48

+0

使用Google Chrome的開發人員控制檯和/或Firebug從AJAX調用中獲取響應並將其發佈到此處。 - 您的AJAX腳本正在碰到'lecturer_login.php',但是您在代碼片段中引用了'lecturer.php',所以請確保它們匹配。 – 2013-02-27 20:57:33

+0

從lecturer_login.php輸出爲空 – Kam 2013-02-27 20:58:26

回答

0

我會做類似

<?php 
$stack = array(); 
$stack['info']='test'; 
echo json_encode($stack);  
?> 

而且insted的的$。員額我喜歡$就

 $.ajax({ 
       url:'../php/lecturer_login.php', 
       type:'POST', 
       dataType:'json', 
       success:function (result) { 
        $('#test').html(result.info);//since info is the key of the json maybe the same for $.post 
       } 
     }); 

我忘了你需要

event.preventDefault(); 
0

我認爲這個問題是你並沒有阻止回傳。

嘗試這樣做:

<script type="text/javascript"> 
$('#form-lecturer-login').submit(function(event){ 
    event.preventDefault(); 
    ... 

添加參數傳遞給函數調用preventDefault()方法。