2013-07-24 54 views
2

我想加入wordpress主題選項頁面使用jQuery動態行..如何使用jquery php保存動態添加的行?

我的主題選項頁上的HTML

<a href="#" title="" class="add-author">Add Author</a> 

<table class="authors-list" id="tablebody" border="1" bordercolor="#ddd" 
style="background-color:#F5F5F5" width="100%" cellpadding="3" cellspacing="3"> 
    <tr><td>Designation</td><td>StartDate</td><td>EndDate</td></tr> 
    <tr> 
     <td><input style="width:200px" type="text" name="designation"/></td> 
     <td><input style="width:200px" type="text" id="start_date" name="start_date"/></td> 
     <td><input style="width:200px" type="text" id="end_date" name="end_date"/></td> 
    </tr> 
</table> 

我的jQuery代碼添加行

var counter = 1; 
jQuery('a.add-author').click(function(event){ 
alert("asdas"); 
    event.preventDefault(); 
    counter++; 
    var newRow = jQuery('<tr><td><input style="width:200px" type="text" name="designation' + 
counter + '"/></td><td><input style="width:200px" type="text" id="start_date'+ counter +'" name="start_date' + 
     counter + '"/></td><td><input style="width:200px" id="end_date'+ counter +'" type="text" name="end_date' + 
     counter + '"/></td></tr>'); 
    jQuery('table.authors-list').append(newRow); 
    jQuery("#start_date"+ counter).datepicker(); 
    jQuery("#end_date"+ counter).datepicker(); 

}); 

Working DEMO of addition of rows

我如何保存的任何行的櫃檯在WordPress數據庫...我如何可以檢索沒有行添加..Plz幫我IM卡......我不是做wordpress數據庫中的任何新表。我喜歡WUD通過update_optionadd_option所以我必須使用ajax request也試圖在主題選項數據庫來保存這一點,但不知道如何將這些網頁上使用update_option ..多謝

+1

在窗體上使用隱藏字段來存儲行數。 –

+0

@u_mulder沒有得到你的意思..你可以更具體.. –

+1

創建一個服務器端點,允許一個職位和一個get,當它是一個職位,更新數據庫,當它是一個get, DB。然後通過jQuery AJAX調用來調用該端點。 – Idiot211

回答

1

我真的不喜歡使用計數器對於這一點,即使壽它可能是最輕的方式,我相信jQuery中最安全的方法是簡單地做一個Ajax請求您加入後,你行這樣(你追加後右):

//The first table row is the header so you remove this row from the count 
    rowNumber = ($('#myTable tr').length - 1); 

然後,你只需在數據庫中使用ajax這樣保存這個數字:

jQuery.ajax({ 
    type: 'post', 
    url: 'save.php' //In this file, you'll do a php/mysql request that will do this SQL request *** UPDATE your_table SET your_table_column_where_you_want_to_set_it = $_POST['rownumber'] WHERE current_user_id = unique_user_id *** Of course, you might also save the date in this row but I let you take care of the request update now that you understand the basics 
    , 
    data: { 
     rownumber: rowNomber // This will be the $_POST['rownumber'] on the save.php file 
     }, 
    success: function() { 
     // Action if it works (popup saying new field saved ? or something like this) 
    }, 
    error: function() { 
     //If it doesn't work, do an action 
    } 
    }); 
+0

感謝我完成了同樣的事情...使用我的數據變量以及我的行計數器.. –

相關問題