简体   繁体   English

在HTML表单中使用多维数组有什么优势?

[英]What are the advantages of using multidimensional arrays in a HTML form?

Regarding the code below: What is the advantage of using the data[Category][county][Macon] array instead of just making the name 'county' or county[], and the value 'Macon', and how would they process the form data on the server side with each checkbox sharing the same value of 1? 关于以下代码:使用data [Category] ​​[county] [Macon]数组而不是仅仅命名为'county'或county []和值'Macon'有什么好处,以及它们将如何处理服务器端的表单数据,每个复选框共享相同的值1? Also, why does each checkbox have a hidden input with the same name but with a value of 0? 另外,为什么每个复选框都有一个隐藏的输入,但其名称相同但值为0?

Any assistance with helping this make sense would be appreciated. 任何帮助解决此问题的帮助将不胜感激。

<li>
    <div class="input checkbox">
        <input type="hidden" name="data[Category][county][Macon]" id="CategoryCountyMacon_" value="0" />
        <input type="checkbox" name="data[Category][county][Macon]" value="1" id="CategoryCountyMacon" />
        <label for="CategoryCountyMacon">Macon</label>
    </div>
</li>
<li>
    <div class="input checkbox">
        <input type="hidden" name="data[Category][county][Madison]" id="CategoryCountyMadison_" value="0" />
        <input type="checkbox" name="data[Category][county][Madison]" value="1" id="CategoryCountyMadison" />
        <label for="CategoryCountyMadison">Madison</label>
    </div>
</li>

Actually it is quite common in some frameworks. 实际上,这在某些框架中非常普遍。 Imagine that Category is the table or a model, county is the column and Madison is the data for the column, or a related data in another lookup table. 假设Category是表或模型, county是列, Madison是列的数据,或另一个查找表中的相关数据。 Looks a little like CakePHP. 看起来有点像CakePHP。

Let's say you have a form where a user can update something that has multiple related tables. 假设您有一个表单,用户可以在其中更新具有多个相关表的内容。 How do you intelligently and repeatably define what data is bound for what table etc... The framework has logic in the receiving code that looks for the first level keys as the table/model and then the columns and values. 您如何智能地,可重复地定义将哪些数据绑定到哪个表等?该框架在接收代码中具有逻辑,该逻辑将查找第一级键作为表/模型,然后是列和值。

Also, checkboxes are only submitted if they are checked, so if not they won't be in the $_POST array. 同样,仅在选中复选框的情况下才提交复选框,因此如果不选中,它们将不在$_POST数组中。 Defining a hidden input with the same name and value of 0 before the checkbox insures that there will be a value, either 0 if not checked or 1 if checked. 在复选框前定义具有相同名称和值为0的隐藏输入可确保将有一个值,如果未选中,则为0;如果选中,则为1。 It's a way of taking submitted values and using them as opposed to checking if something is present and then doing one thing or the other. 这是一种获取提交的值并使用它们的方法,而不是先检查是否存在某些东西,然后再做一件事情或另一件事情。

I think you may have over complicated this a bit. 我认为您可能对此有点复杂了。

I assume you are showing the user a set of counties and they tick the ones they are interested in. 我假设您正在向用户显示一组县,并且他们在他们感兴趣的县中打勾。

Now first thing to remember is that a checkbox is only sent back to the script if it is checked, if its not you never see it back in your script. 现在要记住的第一件事是,如果选中了复选框,则仅将其发送回脚本,否则,您将永远不会在脚本中看到该复选框。 Also what is sent back to the script is the value="something" 发送回脚本的也是value="something"

So if you are just interested in which counties were selected this is much easier to understand and process back in your script. 因此,如果您只是对选择了哪个县感兴趣,这将更容易理解并在您的脚本中进行处理。 Also I am not sure what function the hidden fields was serving, I am guessing none. 另外,我不确定隐藏字段在提供什么功能,我猜没有。

<li>
    <div class="input checkbox">
        <input type="checkbox" name="counties[]" value="Macon" id="CategoryCountyMacon" />
        <label for="CategoryCountyMacon">Macon</label>
    </div>
</li>
<li>
    <div class="input checkbox">
        <input type="checkbox" name="counties[]" value="Madison" id="CategoryCountyMadison" />
        <label for="CategoryCountyMadison">Madison</label>
    </div>
</li>

So now if Macon and Madison are checked you will get an array called counties returned in the $_POST array like this. 因此,现在如果选中了MaconMadison ,您将在$ _POST数组中返回一个称为counties数组,如下所示。

counties[0] = 'Macon'
counties[1] = 'Madison'

So you can process it easily with a 因此,您可以使用

foreach( $_POST['counties'] as $county ) {
    // $county will = 'Macon' on first iteration
    // $county will = 'Madison' on second iteration
}

Of course it may be better still to put the unique key of the counties row from the database into the value attribute ie value="99 rather than the name value="Macon" . That depends upon what you want to do with it later and how you have designed your database. 当然,最好还是将数据库中县县行的唯一键放入value属性中,即value="99而不是name value="Macon" ,这取决于您以后要使用的功能以及您如何设计数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM