0

I have a form with dynamically created input fields - upon submitting it I would like to place all the generated data into an array using php.

An example of the html:-

<fieldset id="usp">
<input type="text" id="usp_1" name="usp_1" />
<input type="text" id="usp_2" name="usp_2" />
</fieldset>
<input type="hidden" id="uspTot" name="uspTot" />

input id=usp_1 and usp_2 were generated by Jquery on the fly - there can be any number of these inputs depending on the user so Jquery keeps a tally in the hidden field id=uspTot

When the form is posted I have tried to put the values of these input elements into a simple array using the following PHP code :-

$uspCount = $_POST["uspTot"];
for ($i=1; $i<=$uspCount; $i++)
 {
  $uspString[$i] = $_POST["usp_" + $i];
 };

It doesn't work of course! But I can't see why not?

Any pointers?

EDIT: this is the error code I receive:-

Notice: Undefined variable: POST in C:\Users\Andrew\Documents\Websites\example\html\submitbrief.php on line 21

EDIT 2:

This is the var_dump as requested by @Michael:-

array(36) { ["co_name"]=> string(11) "Daves autos" ["co_about"]=> string(16) "established 1969" ["co_id"]=> string(10) "Dave Smith" ["co_email"]=> string(21) "[email protected]" ["co_phone"]=> string(11) "34435454545" ["usp_1"]=> string(9) "its great" ["usp_2"]=> string(9) "i love it" ["usp_3"]=> string(15) "its about car4s" ["uspTot"]=> string(1) "3" ["co_keyw_1"]=> string(0) "" ["co_keyw_2"]=> string(0) "" ["co_keyw_3"]=> string(0) "" ["co_keyw_4"]=> string(0) "" ["co_keyw_5"]=> string(0) "" ["co_compet"]=> string(0) "" ["ex_pos"]=> string(0) "" ["ex_neg"]=> string(0) "" ["ex_url"]=> string(0) "" ["pac_basic"]=> string(3) "yes" ["pac_cms"]=> string(2) "no" ["pac_eco"]=> string(2) "no" ["pac_ax"]=> string(4) "base" ["pac_ie6"]=> string(2) "no" ["pac_url"]=> string(0) "" ["pac_dom"]=> string(2) "no" ["pac_ins_1_url"]=> string(0) "" ["pac_ins_1_det"]=> string(0) "" ["pac_keyw_1"]=> string(0) "" ["pac_keyw_2"]=> string(0) "" ["pac_keyw_3"]=> string(0) "" ["pac_keyw_4"]=> string(0) "" ["pac_keyw_5"]=> string(0) "" ["pac_name"]=> string(0) "" ["pac_dem"]=> string(0) "" ["pac_gui"]=> string(0) "" ["pac_tex"]=> string(0) "" } 
6
  • Did you check that your jQuery code is indeed updating uspTot and that all the post variables are indeed being posted? Commented Jul 26, 2011 at 17:30
  • Are you sure uspTot is counting properly and starting the count at 1 and not 0? Commented Jul 26, 2011 at 17:30
  • What doesn't work? What error do you get? Please post the entire form, also the output of var_dump($_POST) Commented Jul 26, 2011 at 17:30
  • @Mat - Yes I did and it is :) Commented Jul 26, 2011 at 17:40
  • @Michael - How remiss of me :) Sorry Notice: Undefined variable: POST in C:\Users\Andrew\Documents\Websites\example\html\submitbrief.php on line 21 Commented Jul 26, 2011 at 17:40

3 Answers 3

3

Why not change your HTML markup so the values get posted as an array, and forget about the hidden tally or the other things?

Change your markup to the following:

<fieldset id="usp">
    <input type="text" id="usp_1" name="usp[1]" />
    <input type="text" id="usp_2" name="usp[2]" />
</fieldset>

Then, access the array of values with this:

$arrValues = $_POST['usp'];
Sign up to request clarification or add additional context in comments.

Comments

1

Try this in your for loop:

$uspString[$i] = $_POST["usp_" . $i];

Comments

1

You should use . instead +
$uspString[$i] = $POST["usp" . $i];

2 Comments

I'm afraid this gives me the same result, thanks for the answer.
Yes of course. Sorry didn't check parser output after sending answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.