2015-11-04 63 views
1

我在一個頁面中創建了兩個表單,每個表單都有自己的按鈕。當我點擊第一個表單按鈕時,應該同時刷新兩個表單。如何刷新同一頁上的一個按鈕中的兩個表格

我的第一種形式是這個樣子

<form id="search_form" method="post" action="do_searchID.php"> 
     <label>Enter Employee Number</label> 
     <input type="text" name="emp_id" id="emp_id"> 
     <button type="submit" name="search">Search</button> 
</form> 

我的第二種形式是像這樣在同一頁面

<form method="post" action="do_searchID.php"> 
<label>Full Name</label> 
<input type="text" id="full_name" value=""> 
<button type="submit" name="submit_update">Update Now </button> 
</form> 

從這個怎麼能寫的jQuery做上述任務。請幫助我我不知道做這個任務我必須從數據庫檢索數據。

+0

你需要使用'$ .ajax()'http://api.jquery.com/jquery.ajax/ –

+3

你想要提交兩種形式或休息(清除)兩種形式在一個按鈕上單擊? – coolguy

+0

@coolguy,同樣的問題...... !! – Suyog

回答

1

你可以使用ajax來做到這一點。我想你正試圖搜索員工身份,並以第二種形式顯示員工姓名。對於這裏是Ajax代碼

$(function()){ 
var emp_id = $('#emp_id').val(); 
$.ajax({ 
      url: filename.php, 
      data: {'emp_id':emp_id}, 
      dataType: 'json', 
      method:'POST', 
      success:function (data) 
      { 
       var emp_name = data.empname; 
       /* If its receiving json in data then parse like JSON.parse(data). Then get employee name*/ 
       $("#fullname").val('emp_name'); 
      } 
     }); 
}); 

在PHP中你可以得到由$ _ POST [「EMP_ID」] emp_id和由EMP_ID搜索的員工姓名並返回員工姓名。

1
$("#SearchButton").click(function(){ 
    // Trigger your submit of both form 
    $("#search_form,#ID_form_2").submit(); 
}) 

您可以使用上面的代碼。如果您希望在首次提交按鈕調用時完成此操作。

FORM1

<form id="search_form" method="post" action="do_searchID.php"> 
    <label>Enter Employee Number</label> 
    <input type="text" name="emp_id" id="emp_id"> 
    <button type="submit" id='SearchButton' name="search">Search</button> 
</form> 

FORM2

<form method="post" id='ID_form_2' action="do_searchID.php"> 
    <label>Full Name</label> 
    <input type="text" id="full_name" value=""> 
    <button type="submit" name="submit_update">Update Now </button> 
</form> 
1

最好的方法是使用一個通用類要刷新各個領域..和創造出這樣做的功能。

像這樣。

function refreshForm() 
    { 
    $(".commonClass").val(''); 
    } 

那就是它。

相關問題