[RESOLVED] Multiple $_POST in variable?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
124112
Forum Newbie
Posts: 3
Joined: Wed Jul 14, 2010 5:58 am

[RESOLVED] Multiple $_POST in variable?

Post by 124112 »

I am trying to put a script on my site, that has 5 textboxes, and a submit button.

When the user presses submit, I want to be emailed the information they put, with linebreaks, like this:

Info1: txt1
Info2: txt2
Info3: txt3
Info4: txt4
Info5: txt5

The code I have that grabs the data from the textbox is

Code: Select all

$message = $_POST['whatever'];
And the line of code that sends the email is

Code: Select all

mail($to,$subject,$message,$headers);
Is there a way I can, in the $message variable, have multiple POST requests that gets all the fields the user put, and puts inline linebreaks between each of them?

Tell me I haven't explained my problem clearly.
Last edited by 124112 on Wed Jul 14, 2010 9:48 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Multiple $_POST in variable?

Post by VladSun »

implode() the $_POST array :)
There are 10 types of people in this world, those who understand binary and those who don't
steel_rose
Forum Newbie
Posts: 15
Joined: Thu May 13, 2010 9:17 am

Re: Multiple $_POST in variable?

Post by steel_rose »

Hi,

I think your best soution would be to use a foreach loop.

Code: Select all

       foreach ($_POST as $key=>$val){
        echo "$key: $val <br />";
       }
Let me know if this is what you looking for, good luck with your code!

SR
124112
Forum Newbie
Posts: 3
Joined: Wed Jul 14, 2010 5:58 am

Re: Multiple $_POST in variable?

Post by 124112 »

steel_rose wrote:Hi,

I think your best soution would be to use a foreach loop.

Code: Select all

       foreach ($_POST as $key=>$val){
        echo "$key: $val <br />";
       }
Let me know if this is what you looking for, good luck with your code!

SR
Thanks to both of you guys, but I am getting an error.

This is my code:

Code: Select all

$message = foreach ($_POST as $key=>$val){ echo "$key: $val <br />"; };
And I get this error:

Parse error: syntax error, unexpected T_FOREACH in /home/test/public_html/send.php on line 4

I'm prettty sure I am doing something horribly wrong. How should I do this? What should go after "$message =" ?
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

Re: Multiple $_POST in variable?

Post by buckit »

This should work (untested)

Code: Select all

foreach ($_POST as $key=>$val){ $message .= "$key: $val <br />"; };
.= will append to the existing value of $message. So it keeps adding to it as it loops.
124112
Forum Newbie
Posts: 3
Joined: Wed Jul 14, 2010 5:58 am

Re: Multiple $_POST in variable?

Post by 124112 »

buckit wrote:This should work (untested)

Code: Select all

foreach ($_POST as $key=>$val){ $message .= "$key: $val <br />"; };
.= will append to the existing value of $message. So it keeps adding to it as it loops.
Thanks to all 3 of you who helped me with this problem.

I finally got it working with this:

Code: Select all

foreach  ($_POST as  $key=>$val){ $message .= "$key: $val \n"; };
I had to use \n instead of <br>
Post Reply