duanhuai7532 2011-07-26 17:26
浏览 70
已采纳

PHP读取提交表单并将结果放入数组中

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) "dave@davesautos.co.uk" ["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) "" } 
  • 写回答

3条回答 默认 最新

  • douji0108 2011-07-26 17:34
    关注

    Try this in your for loop:

    $uspString[$i] = $_POST["usp_" . $i];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?