Username_null
Username_null

Reputation: 1315

PHP reading submitted form and placing results in array

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) "" } 

Upvotes: 0

Views: 132

Answers (3)

ngen
ngen

Reputation: 8966

Try this in your for loop:

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

Upvotes: 1

Shef
Shef

Reputation: 45589

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'];

Upvotes: 3

hadvig
hadvig

Reputation: 1106

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

Upvotes: 1

Related Questions