arrays - PHP dynamic form variables -
i posted on @ wordpress development site getting no bites. still php issue, i'm hoping here can me out.
i trying create dynamic form in wordpress admin can add line , still able process variables database post meta. i've done quite bit of research , have tried several methods cant seem work. have sample of code here show how processing info, need figure out how loop through dynamic content , update or delete necessary.
<?php // set 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 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'); ?>
when give inputs names ending in [], php turns $_post or $_get parameters arrays. loop through content this:
if (isset_($_post['product'])) { foreach ($_post['product'] $i => $product) { // $product, $_post['description'][$i], $_post['qty'][$i], etc. } }
Comments
Post a Comment