2013-01-20 34 views
0

我有一個頁面的形式與action="/profile",我想做一個Ajax代碼,使其更新,而無需刷新頁面,無需轉到該頁面。任何想法,我如何去做這件事,或者如果我真的做對了或什麼?使用ajax從其他頁面提交表單?

$(function() { 
    $('.error').hide(); 
    $("#submit").click(function() { 
// validate and process form here 
$('.error').hide(); 
    var stat = $("#profile_field_13_5").val(); 
    if (stat == "") { 
    $("#name_error").show(); 
    $("#profile_field_13_5").focus(); 
    return false; 
} 
var dataString = 'stat='+ stat ; 
$.ajax({ 
      type: "POST", 
      url: "/profile.php", 
      data: dataString, 
      success: function() { 
    $('#statsucces').show(); 
     window.setTimeout("closeSuccess();", 10000); 

     function closeSuccess(){ 
     document.getElementById("statsuccess").style.display=" none"; 
    } 
     }); 
    }); 
return false; 
    }); 
}); 

這是我試圖用做一個狀態更新

#profile_field_13_5輸入字段和表單的動作又是/profile.php

感謝任何人幫助我

+0

有沒有人有任何線索如何做到這一點? – EasyBB

回答

0

如果你是提交表格然後這樣做:

$("form").submit(function (e) { 
    // validate and process form here 
    $('.error').hide(); 
    var stat = $("#profile_field_13_5").val(); 
    if (stat == "") { 
     $("#name_error").show(); 
     $("#profile_field_13_5").focus(); 
     return false; 
    } 
    var dataString = 'stat=' + stat; 
    $.ajax({ 
     type: "POST", 
     url: "/profile.php", 
     data: {data:dataString}, //<---pass it this way 
     success: function (data) { 
      alert(data); // <----this will show the passed value 
      } 
     }); 

e.preventDefault(); //return false; <--preventDefault() is as same as return false; 
}); 

試試看看是否有幫助。

+0

好吧我現在要試試這個謝謝:) – EasyBB

+0

我得到一個意想不到的令牌),並找不到錯誤 – EasyBB

+0

我看了,它的最後一行是什麼是拋出錯誤... '} ); e.preventDefault(); });' – EasyBB