Solve the problem of PHP processing form submission of multiple input tags with the same name attribute value

不言
Release: 2023-03-25 08:12:02
Original
2116 people have browsed it

This article mainly introduces how to solve the problem of PHP processing form submission of multiple input tags with the same name attribute value. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

One question

During the development process of the company, we encountered a problem: How to handle multiple input tags with the same name attribute value submitted in the form? The source code is as follows (the source code is in the form):

<!--{loop $address $index $one}-->
<p class="address_item">
   <p>
    <label>
    <input type="hidden" name="express_price" value="{$one[&#39;express_price&#39;]}" />
    <input type="hidden" name="state_fare" value="{$one[&#39;state_fare&#39;]}" />
    <input type="hidden" name="id" value="{$one[&#39;id&#39;]}" />
    <input type="radio" <!--{if $one[ &#39;default&#39;]==&#39;Y&#39; }-->checked<!--{/if}-->name="address" value="{$one[&#39;id&#39;]}" /><span name="mobile">{$one[&#39;mobile&#39;]}</span>
    </label>
   </p>
</p>
<!--{/loop}-->
Copy after login

Second solution

In the above source code, through loop loop, multiple input elements with the same name attribute value are generated in the form. When you click submit, the background php file can only obtain the input in one p element using $_POST. The value passed by the element, however, the value passed by the radio type input element must be the selected one. Accordingly, modify the same name attribute value of some input elements in the above source code to an array, and then use the name attribute value of the selected radio type input element in the background php file to determine the entire p element passed. The modified code is as follows:

<!--{loop $address $index $one}-->
<p class="address_item">
   <p>
    <label>
    <input type="hidden" name="express_price[]" value="{$one[&#39;express_price&#39;]}" />
    <input type="hidden" name="state_fare[]" value="{$one[&#39;state_fare&#39;]}" />
    <input type="hidden" name="id[]" value="{$one[&#39;id&#39;]}" />
    <input type="radio" <!--{if $one[ &#39;default&#39;]==&#39;Y&#39; }-->checked<!--{/if}-->name="address" value="{$one[&#39;id&#39;]}" />
    <span name="mobile">{$one[&#39;mobile&#39;]}</span>
    </label>
   </p>
</p>
<!--{/loop}-->
Copy after login

php file:

$key = 0;
$address_id = intval($_POST["address"]);
foreach ($_POST[&#39;id&#39;] as $k => $v) {
  if ($v == $address_id)
     $key = $k;
}
$_POST[&#39;express_money&#39;] = $_POST[&#39;exporess_price&#39;][$key];
Copy after login

Related recommendations:

Under Linux, solve the problem of inconsistency between php -v and phpinfo versions

How to solve the problem of garbled webpage data captured by php using the file_get_contents method


The above is the detailed content of Solve the problem of PHP processing form submission of multiple input tags with the same name attribute value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!