久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在發布到 PHP 的 HTML 表單中使用數組格式的輸

How can I use the array-format input field names in my HTML form that posts to PHP?(如何在發布到 PHP 的 HTML 表單中使用數組格式的輸入字段名稱?)
本文介紹了如何在發布到 PHP 的 HTML 表單中使用數組格式的輸入字段名稱?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我了解使用數組格式的 HTML 輸入名稱的基礎知識.如果我有一個包含可變數量的項目"輸入的表單,我可能會為每個輸入做這樣的事情:

I understand the basics of using the array-formatted HTML input names. If I had a form with a variable number of 'item' inputs I might do something like this for each of them:

<input name='item[]' type='text' />

當我從 $_POST 數組中檢索項目時,我可以像這樣迭代它們:

And when I retrieve the items from the $_POST array I could iterate over them like so:

$items = $_POST['item'];
foreach($items as $item) {
}

但我的問題稍微復雜一些.我有一個表單,用戶可以在其中單擊添加一個"按鈕,然后在表單底部的數字處會出現一個新行.每個新行都包含一個名稱"和描述"輸入.

But my question is slightly more complicated. I have a form where users can click a button "add one" and a new row will appear at the bottom number of the form. Each new row contains a 'name' and 'description' input.

所以最初我想我會這樣做:

So initially I thought I would do this:

<input name='item[name][]' type='text' />
<input name='item[description][]' type='text' />

然后像這樣迭代它們:

$items = $_POST['item'];
foreach($items as $item) {
  print $item['name'] . ' ' . $item['description'];
}

但是它沒有像我希望的那樣工作,而是構造了item"數組,這樣我就可以以 $item['name'][0] 而不是 $item['name'][0] 訪問第一個項目名稱代碼>$item[0]['name'].

But instead of working as I hoped, it instead structures the 'item' array such that I would access the first item name as $item['name'][0] rather than as $item[0]['name'].

然后我翻轉它,以便我的輸入被命名為:

So then I flipped it so that my inputs were named as such:

<input name='item[][name]' type='text' />
<input name='item[][description]' type='text' />

但這導致每個名稱"和每個描述"都有一個單獨的項目",而不是將每一對組合成一個項目".

But this resulted in a separate 'item' for each 'name' and for each 'description' rather than grouping each pair in a single 'item'.

我真的不喜歡有名稱"數組和單獨的描述"數組.我更喜歡 'item' 數組,每個數組包含一個 'name' 和一個 'description' 字段.有沒有辦法在不生成我的 javascript 中的索引的情況下完成此操作?由于人們可以動態添加和刪除這些,因此我的 javascript 很難計算下一項的適當索引.一般沒有辦法做到這一點嗎?

I really dislike having arrays of 'name' and separate array of 'description'. I would prefer arrays of 'item' with each array containing a 'name' and a 'description' field. Is there any way to accomplish this without generating the an index in my javascript? Since people can add and remove these dynamically it's very difficult for my javascript to calculate the appropriate index for the next item. Is there no way to do this generically?

推薦答案

不可能做你想做的事,但如果有幫助,這里有一些代碼可以將它拼湊起來,我認為它會起作用(使用 item_name[]item_description[]):

It's not possible to do what you want, but if it helps, here's some code to piece it back together that I think will work (with item_name[] and item_description[]):

$items_desc = $_POST["item_description"];
$items_name = $_POST["item_name"];
$items = array();
for ($i = 0; $i < count($items_name); $i++)
{
    $items[] = array("name" => $items_name[$i], "description" => $items_desc[$i]);
}

這篇關于如何在發布到 PHP 的 HTML 表單中使用數組格式的輸入字段名稱?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 欧美aaaaaa| 黄一区二区三区 | 成人在线精品视频 | 亚洲精品乱码8久久久久久日本 | 欧美午夜剧场 | 中文字幕1区 | 色婷婷综合久久久久中文一区二区 | 欧美一区二区三区在线观看视频 | 在线观看视频你懂得 | 无码一区二区三区视频 | 国产视频1区 | 国产午夜精品视频 | 精品国产一区二区三区在线观看 | 精精国产xxxx视频在线播放 | 一区二区视频在线观看 | 国产1页 | 国产一区久久久 | 国产一区二区三区视频 | 亚洲国产欧美在线 | 天天看天天干 | 情侣av| 中文字幕三区 | 伊人最新网址 | 色婷婷精品 | 亚洲高清视频一区二区 | 99爱国产| 极品久久 | 一区二区蜜桃 | 色一级片| 欧美中文字幕在线 | 九九亚洲| 国产亚洲一区二区三区在线 | 91在线精品视频 | 午夜tv免费观看 | 国产九九精品视频 | 久草电影网 | 极品久久 | 久久久国产一区二区三区四区小说 | 欧美激情综合五月色丁香小说 | 久久婷婷av | 欧美成人二区 |