How does php obtain the value of the corresponding form by matching some strings of the form name?

Source: Internet
Author: User
How does php obtain the value of the corresponding form by matching some strings of the form name? The problem may be a bit confusing.
That is, the number of forms on my form page is not fixed. because there is a button for adding a form, you can press this button to increase the number of forms, the form name is in the form of "prefix + incrementing number", such as "array_1" and "array_2", but the specific number is not fixed, because it depends on the number of forms added by the user, now I want to put all the values of all forms with the array _ prefix into an array. how can this be achieved? The key issue is that the number of non-forms is not fixed.

I'm a newbie. I don't know how to describe the problem clearly. thank you.


Reply to discussion (solution)

Whether to add a form or a form element

Do you have N forms on one page?

If there are indeed multiple forms, I currently use jquery to dynamically obtain all the form values, and then pass them to the background

Var postData = ""; $. each ($ ("from"), function (I, n) {postData + = $ (n ). serialize () ;}); // upload to the background through ajax.

This post was last edited by xuzuning at 16:32:56 on

There are no multiple forms and only multiple Form variables

foreach(preg_grep('/array_/', array_keys($_POST)) as $k) {  res[$k] = $_POST[$k];}

$ Res is the result you need.

In fact, you do not need to name it array _ number, but an array [].

There are no multiple forms and only multiple Form variables

foreach(preg_grep('/array_/', array_keys($_POST)) as $k) {  res[$k] = $_POST[$k];}

$ Res is the result you need.

In fact, you do not need to name it array _ number, but an array [].

Thanks. what if the name is array?

Whether to add a form or a form element

Do you have N forms on one page?

If there are indeed multiple forms, I currently use jquery to dynamically obtain all the form values, and then pass them to the background

Var postData = ""; $. each ($ ("from"), function (I, n) {postData + = $ (n ). serialize () ;}); // upload to the background through ajax.

Thank you. I have a question. Multiple form elements are multiple input tags, not multiple forms.

Input tags of multiple forms. you can differentiate variables using the name attribute.

Define the name of the same element group as an array, as shown below:


.........

Php receiving is
Print_r ($ _ POST ['array']);

Define the name of the same element group as an array, as shown below:


.........

Php receiving is
Print_r ($ _ POST ['array']);

It turns out this way. it's easy to get taught. thank you!




Print_r ($ _ POST ['array'] ['A1']);
Print_r ($ _ POST ['array'] ['A2 ']);

In this way, the input values can be differentiated.

There are no multiple forms and only multiple Form variables

foreach(preg_grep('/array_/', array_keys($_POST)) as $k) {  res[$k] = $_POST[$k];}

$ Res is the result you need.

In fact, you do not need to name it array _ number, but an array [].
Jordan102 greatly answered this question. thank you very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.