PHP动态表单变量

时间:2015-06-03 18:56:41

标签: php arrays forms variables dynamic

我在wordpress开发网站上发布了这个,但我没有咬人。它仍然是一个php问题,所以我希望有人可以帮助我。

我正在尝试在wordpress管理员中创建一个动态表单,我可以在其中添加一行,并且仍然能够将变量作为post meta处理到数据库中。我做了很多研究并尝试了几种方法,但似乎无法使其发挥作用。我在这里有一个代码示例,以显示我如何处理信息,我只需要弄清楚如何循环动态内容,然后根据需要正确更新或删除它。

<?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">&nbsp;</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');

?>

1 个答案:

答案 0 :(得分:0)

当您提供以[]结尾的输入名称时,PHP会将这些$_POST$_GET参数转换为数组。然后你循环遍历这样的内容:

if (isset_($_POST['product'])) {
    foreach ($_POST['product'] as $i => $product) {
        // Do something with $product, $_POST['description'][$i], $_POST['qty'][$i], etc.
    }
}