FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP load array from $_POST





DAEMONFOX
EDIT: Made a bit of progress on the below by removing the $$key = portion and replacing with an echo and line break after $value. Lose variable assignment but gain easy printing!

Code:


<?php

foreach ($_POST as $key => $value)
echo "$value <br>";

?>



Hello all!

Read some wonderful posts here that helped GREATLY!

I have one issue that I have not been able to derive a working solution for and haven't seen anything on the boards that did exactly what I am looking for (or I missed it).

I have an HTML form with 145 input fields that passes off to a PHP page to send an email with all the completed data.

I have managed to load all the fields into variables in an array via the below:

Code:

<?php

foreach ($_POST as $key => $value)

$$key = $value;

print_r($_POST);
?>


My issue now is I don't need any fancy formatting or anything, just to dump these values into an email with line breaks and send it. I just don't want to list 145 variables in echo statements... way overkill.

So I thought my best bet would be to call them from the array in a while loop to echo each record set.

Problem is I have yno idea how to reference the above array.

Here is the print_r if it helps. Most of the variables are empty save the first few:

Array ( [hireappposition] => TOP SALES MANAGER/MANAGEMENT [hireappfirstname] => Sam [hireapplastname] => Johnson [hireappmi] => [hireappnameother] => [hireappaddress] => 12345 hand road [hireappcity] => somewhere [hireappstate] => FL [hireappzipcode] => 99999 [hireappphone] => 19009009000 [hireappphone2] => [hireappemail] => blah@blah.blah [hireappprevappdate] => [hireappprevempdate] => [hireappfriendsfamily] => [hireappavaildate] => [hireappwillnotworkshifts] => [hireappcriminalpendingdetail] => [hireappskills] => [hireapplanguages] => [hireappdriversl#] => [hireappdriverlclass] => Select a class [hireappdlstate] => [hireappdlexpdate] => [hireappschool1] => [hireappschool1yrs] => [hireappschool1degree] => [hireappschool1study] => [hireappschool2] => [hireappschool2yrs] => [hireappschool2degree] => [hireappschool2study] => [hireappschool3] => [hireappschool3yrs] => [hireappschool3degree] => [hireappschool3study] => [hireappcollege1] => [hireappcollege1yrs] => [hireappcollege1degree] => [hireappcollege1study] => [hireappcollege2] => [hireappcollege2yrs] => [hireappcollege2degree] => [hireappcollege2study] => [hireappcollege3] => [hireappcollege3yrs] => [hireappcollege3degree] => [hireappcollege3study] => [hireappgrad1] => [hireappgrad1yrs] => [hireappgrad1degree] => [hireappgrad1study] => [hireappgrad2] => [hireappgrad2yrs] => [hireappgrad2degree] => [hireappgrad2study] => [hireappgrad3] => [hireappgrad3yrs] => [hireappgrad3degree] => [hireappgrad3study] => [hireappothertraining1] => [hireappothertraining1yrs] => [hireappothertraining1degree] => [hireappothertraining1study] => [hireappothertraining2] => [hireappothertraining2yrs] => [hireappothertraining2degree] => [hireappothertraining2study] => [hireappothertraining3] => [hireappothertraining3yrs] => [hireappothertraining3degree] => [hireappothertraining3study] => [hireapphonors] => [hireappref1name] => [hireappref1phone] => [hireappref1yrs] => [hireappref2name] => [hireappref2phone] => [hireappref2yrs] => [hireappref3name] => [hireappref3phone] => [hireappref3yrs] => [hireappprevemp1] => [hireappemp1from] => [hireappemp1to] => [hireappprevemp1super] => [hireappprevemp1address] => [hireappprevemp1title] => [hireappemp1startpay] => [hireappemp1endpay] => [hireappprevemp1phone] => [hireappemp1workperf] => [hireappemp1leave] => [hireappprevemp2] => [hireappemp2from] => [hireappemp2to] => [hireappprevemp2super] => [hireappprevemp2address] => [hireappprevemp2title] => [hireappemp2startpay] => [hireappemp2endpay] => [hireappprevemp2phone] => [hireappemp2workperf] => [hireappemp2leave] => [hireappprevemp3] => [hireappemp3from] => [hireappemp3to] => [hireappprevemp3super] => [hireappprevemp3address] => [hireappprevemp3title] => [hireappemp3startpay] => [hireappemp3endpay] => [hireappprevemp3phone] => [hireappemp3workperf] => [hireappemp3leave] => [hireappprevemp4] => [hireappemp4from] => [hireappemp4to] => [hireappprevemp4super] => [hireappprevemp4address] => [hireappprevemp4title] => [hireappemp4startpay] => [hireappemp4endpay] => [hireappprevemp4phone] => [hireappemp4workperf] => [hireappemp4leave] => [hireappunempdetail] => [hireappforceresigndetail] => [hireappsigagree] => YES [hireappsig] => [hireappsubmit] => SUBMIT )
Code:
Code:
imagefree
If you are not satisfied with the solution you mention in Edited part of your post, consider the following:


Code:
<?php
foreach ($_POST as $key => $value)
{
     if( strlen( $value ) )
     {
          echo "$value <br />";
     }
}
?>


Tell me if this is what you need, or be more specific what you actually need.
jmraker
I think the statement foreach ($_POST as $key => $value)$$key = $value; is unsafe because it assumes the $key values are the same as on the field names in the form. I think it's the same as using "register globals" which is unsafe. Somebody could send extra $_POST values that could replace other globals, so basically any global variable values could be replaced in that loop.

http://php.net/manual/en/security.globals.php

If you want to move the form values to global variables you can use
Code:
import_request_variables('p', 'form_');

where all the field names start with form_ so
$hireappposition=Bob becomes $form_hireappposition=Bob
and other global variables stay the same unless their name starts with form_

Code:
$message = '';
foreach ($_POST as $k=>$v){
  if($v != '' && $v != 'Select a class')
     $message .= $k . ': ' . $v . "\n";
}
mail($to, $subject, $message, $headers);
[/i]
Related topics
PB PHP, Iframe and Javascript
PHP IP Ban
PHP: Feedback form issues.. help =P
How To : Improve Your PHP Programming
Tutorial: PHP Installed Modules Dynamic Reference Tool
PHP Tutorials
[PhP] News Posting Tutorial (code, actually ^^')
array check box on php script using $_POST
handling the $_POST array
PHP and cookies. Simple problem.
Php while array problem
PHP login script problem
Php $_POST
I need help with a very simple php code :(
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.