I am trying to create a dynamic form in the wordpress admin where I can add a line and still be able to process the variables into the database as post meta. I've done quite a bit of research and have tried several methods but cant seem to get it work. I have a sample of my code here to show how I am processing the info, I just need to figure out how to loop through the dynamic content and then properly update or delete it as necessary.
<?php
// Set Up Meta Boxes
function add_invoice_meta_boxes() {
add_meta_box('invoice_meta_box', 'Invoice Summary', 'setup_invoice_meta_box', 'invoice', 'normal', 'high');
}
add_action('add_meta_boxes', 'add_invoice_meta_boxes');
// Invoice Meta Box
function setup_invoice_meta_box($post) {
echo '<input type="hidden" name="invoice_meta_box_nonce" value="'. wp_create_nonce('invoice_meta_box'). '" />';
?>
<script language="javascript">function addRow(tableID){var table=document.getElementById(tableID);var rowCount=table.rows.length;var row=table.insertRow(rowCount);var colCount=table.rows[0].cells.length;for(var i=0;i<colCount;i++){var newcell=row.insertCell(i);newcell.innerHTML=table.rows[0].cells[i].innerHTML;switch(newcell.childNodes[0].type){case"text":newcell.childNodes[0].value="";break;case"checkbox":newcell.childNodes[0].checked=false;break;case"select-one":newcell.childNodes[0].selectedIndex=0;break;}}}
function deleteRow(tableID){try{var table=document.getElementById(tableID);var rowCount=table.rows.length;for(var i=0;i<rowCount;i++){var row=table.rows[i];var chkbox=row.cells[0].childNodes[0];if(null!=chkbox&&true==chkbox.checked){if(rowCount<=1){alert("Cannot delete all the rows.");break;}
table.deleteRow(i);rowCount--;i--;}}}catch(e){alert(e);}}</script>
<?php } ?>
<div class="wrap">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
</tr>
<tr>
<td>
<input type="button" value="Add Row" onclick="addRow('dataTable')">
<input type="button" value="Delete Row" onclick="deleteRow('dataTable')">
</td>
</tr>
<tr>
<td>
<table>
<tr>
<th width="20px"> </th>
<th width="202px">Product</th>
<th width="252px">Description</th>
<th width="52px">QTY</th>
<th width="102px">Price</th>
<th width="102px">Tax</th>
<th width="202px">Amount</th>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table id="dataTable">
<tr>
<td><input type="checkbox" class="gho-chk" name="chk"></td>
<td><input type="text" class="gho-description" name="product[]" value="<?php echo get_post_meta($post_>ID, 'product[]', true); ?>" /></td>
<td><input type="text" class="gho-description" name="description[]" value="<?php echo get_post_meta($post_>ID, 'description[]', true); ?>" /></td>
<td><input type="text" class="gho-qty" name="qty[]" value="<?php echo get_post_meta($post_>ID, 'qty[]', true); ?>" /></td>
<td><input type="text" class="gho-price" name="price[]" value="<?php echo get_post_meta($post_>ID, 'price[]', true); ?>" /></td>
<td><input type="text" class="gho-tax" name="tax[]" value="<?php echo get_post_meta($post_>ID, 'tax[]', true); ?>" /></td>
<td><input type="text" class="gho-amount" name="amount[]" value="<?php echo get_post_meta($post_>ID, 'amount[]', true); ?>" /></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<?php
function save_invoice_meta_box($post_id) {
// check nonce
if (!isset($_POST['invoice_meta_box_nonce']) || !wp_verify_nonce($_POST['invoice_meta_box_nonce'], 'invoice_meta_box')) {
return $post_id;
}
// check capabilities
if ('invoice' == $_POST['post_type']) {
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
// exit on autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
if(isset($_POST['product'])) {
update_post_meta($post_id, 'product', $_POST['product']);
} else {
delete_post_meta($post_id, 'product');
}
}
add_action('save_post', 'save_invoice_meta_box');
?>