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.