What we are going to introduce to you today is a problem encountered when is being encoded. The Options form of the WP plug-in can define some form value variables. WP directly helps us process, save and modify the content submitted by the form without having to process any $_POST ourselves. But what if the form content I want to submit is a dynamically changing array? For example, the form is a dynamically generated inputbox list. How to directly package this data into an array variable and pass it to WP without using $_POST?
Suddenly thought of PHP form array:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><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></li> <li> <span class="tag"><</span><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> </li> <li class="alt"> <span class="tag"><</span><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> </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>
Then if you name the form like this:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><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></li> <li> <span class="tag"><</span><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> </li> <li class="alt"> <span class="tag"><</span><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> </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>
Well, continue to test the PHP form array:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><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></li> <li> <span class="tag"><</span><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> </li> <li class="alt"> <span class="tag"><</span><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> </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>
Then test the multi-dimensional array, and it was also successful. Wow, that's so cool!
A dynamically generated PHP form array, without relying on Javascript, can submit a lot of data in the form of an array variable, solving the trouble of developing this small WordPress plug-in.