> php教程 > php手册 > PHP表单数组的具体使用方法介绍

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

WBOY
풀어 주다: 2016-06-13 11:08:45
원래의
1160명이 탐색했습니다.

今天我们将要为大家介绍的是有关编码的时候遇到一个问题。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插件的*烦。
 


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿