Pages

Thursday, May 5, 2011

Passing an array from a form to another page PHP


Hi,


First, you probably want to use implode, and not explode, to construct your $tovariable ;-)


<code><span class="pln">$ids </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">(</span><span class="lit">a@a.com</span><span class="pun">,</span><span class="pln"> </span><span class="lit"></span></code><code><span class="lit">a@a.com</span><span class="pun"></span></code><code><span class="lit"></span><span class="pun">,</span><span class="pln"> </span><span class="lit"></span></code><code><span class="lit">a@a.com</span><span class="pun"></span></code><code><span class="lit"></span><span class="pun">,</span><span class="pln"> </span><span class="lit"></span></code><code><span class="lit">a@a.com</span><span class="pun"></span></code><code><span class="lit"></span><span class="pun">);</span><span class="pln">
$to </span><span class="pun">=</span><span class="pln"> implode</span><span class="pun">(</span><span class="str">','</span><span class="pun">,</span><span class="pln"> $ids</span><span class="pun">);</span><span class="pln">
var_dump</span><span class="pun">(</span><span class="pln">$to</span><span class="pun">);</span><span class="pln">
</span></code>


Which would give you


<code><span class="kwd">string</span><span class="pln"> </span><span class="str">'</span></code><code><span class="lit">a@a.com</span><span class="pun"></span></code><code><span class="str">,</span></code><code><span class="lit">a@a.com</span><span class="pun"></span></code><code><span class="str">,</span></code><code><span class="lit">a@a.com</span><span class="pun"></span></code><code><span class="str">,</span></code><code><span class="lit">a@a.com</span><span class="pun"></span></code><code><span class="str">'</span><span class="pln"> </span><span class="pun"></span><span class="pun"></span><span class="pln"><br /></span></code>


You then inject this in the form ; something like this would probably do :


<code><span class="pln"><input type="hidden"  value="</span><span class="pun"><?</span><span class="pln">php echo $to</span><span class="pun">;</span><span class="pln"> </span><span class="pun">?></span><span class="pln">"><br /></span></code>






Then, on the PHP script that receives the data from the form when it's been submitted, you'd use explode to extract the data as an array, from the string :


<code><span class="kwd">foreach</span><span class="pln"> </span><span class="pun">(</span><span class="pln">explode</span><span class="pun">(</span><span class="str">','</span><span class="pun">,</span><span class="pln"> $_POST</span><span class="pun">[</span><span class="str">'mail'</span><span class="pun">])</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> $mailid</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br />    print_r(</span><span class="pun"></span><span class="pln">$mailid</span><span class="pun">);</span><span class="pln"><br /></span><span class="pun">}</span><span class="pln"><br /></span></code>


Which will get you each items:



0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.