Rumah >pembangunan bahagian belakang >tutorial php >PHP表单数组的具体使用方法介绍_PHP教程

PHP表单数组的具体使用方法介绍_PHP教程

WBOY
WBOYasal
2016-07-15 13:31:241266semak imbas

今天我们将要为大家介绍的是有关编码的时候遇到一个问题。WP插件的Options表单,可以定义一些表单值变量,WP直接帮我们处理、保存和修改表单提交的内容,无需自己处理任何$_POST。但是如果我要提交的表单内容是一个动态变化的数组怎么办?例如表单是一个动态生成的inputbox列表,如何不用$_POST直接将这些数据打包成一个数组变量传给WP呢?

突然想到了PHP表单数组:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="tag">/></span><span> </span></span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"2"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"3"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li><span>$_POST结果为:  </span></li>
<li class="alt"><span>Array  </span></li>
<li><span>(  </span></li>
<li class="alt">
<span>[a] =</span><span class="tag">></span><span> Array  </span>
</li>
<li><span>(  </span></li>
<li class="alt">
<span>[0] =</span><span class="tag">></span><span> 1  </span>
</li>
<li>
<span>[1] =</span><span class="tag">></span><span> 2  </span>
</li>
<li class="alt">
<span>[2] =</span><span class="tag">></span><span> 3  </span>
</li>
<li><span>)  </span></li>
<li class="alt"><span>) </span></li>
</ol>

那么如果这样命名表单:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[2]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="tag">/></span><span> </span></span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[5]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"2"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[9]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"3"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li><span>$_POST输出结果为:  </span></li>
<li class="alt"><span>Array  </span></li>
<li><span>(  </span></li>
<li class="alt">
<span>[a] =</span><span class="tag">></span><span> Array  </span>
</li>
<li><span>(  </span></li>
<li class="alt">
<span>[2] =</span><span class="tag">></span><span> 1  </span>
</li>
<li>
<span>[5] =</span><span class="tag">></span><span> 2  </span>
</li>
<li class="alt">
<span>[9] =</span><span class="tag">></span><span> 3  </span>
</li>
<li><span>)  </span></li>
<li class="alt"><span>) </span></li>
</ol>


恩恩,继续测试PHP表单数组:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[aa]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="tag">/></span><span> </span></span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[bb]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"2"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"a[cc]"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"3"</span><span> </span><span class="tag">/></span><span> </span></span></li>
<li><span> </span></li>
<li class="alt"><span>$_POST输出结果为:  </span></li>
<li><span>Array  </span></li>
<li class="alt"><span>(  </span></li>
<li>
<span>[a] =</span><span class="tag">></span><span> Array  </span>
</li>
<li class="alt"><span>(  </span></li>
<li>
<span>[aa] =</span><span class="tag">></span><span> 1  </span>
</li>
<li class="alt">
<span>[bb] =</span><span class="tag">></span><span> 2  </span>
</li>
<li>
<span>[cc] =</span><span class="tag">></span><span> 3  </span>
</li>
<li class="alt"><span>)</span></li>
<li class="alt"><span>)</span></li>
</ol>

然后测试多维数组,也成功了。哇,简直太酷了!

一个动态生成的PHP表单数组,不依靠Javascript,就可以将很多数据仅仅以一个数组变量的方式提交上去,解决了开发这个小WordPress插件的*烦。
 


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446207.htmlTechArticle今天我们将要为大家介绍的是有关 编码的时候遇到一个问题。WP插件的Options表单,可以定义一些表单值变量,WP直接帮我们处理、保存和修...
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn