Jump to content

E MAIL FORM


justapilot

Recommended Posts

I am new to PHP. (that should say it all)

I have a form in html, common questions, first name, last name, address, etc. The method is post and action goes to a results.php page

 

The code on that page is as follows:

<?php

 

$to = "[email protected]";

$subject = "Contact Us";

$email = $_POST['email'] ;

$message = $_POST['firstname'];

$headers = "From: $email";

$sent = mail($to, $subject, $message, $headers) ;

if($sent)

{print "Your mail was sent successfully"; }

else

{print "We encountered an error sending your mail"; }

?>

 

My problem is under then $message variable. It displays the first name just fine, however I have mutltiple other fields, lastname, add, citystate, plus a textarea that needs to be included in the message. I have tried an array an that doesnt seem to work, or I am not doing it right.

 

PLEASE HELP!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/112105-e-mail-form/
Share on other sites

welcome to PHP

 

what will probably work most easily is to think of writing out a few lines for how you want to read the message,

 

ex:

 

Name: JOHN DOE

Address: 123 FAKE ST

 

etc...

 

then go back and fill in variables to replace the names

 

Name: $_POST['firstname'] $_POST['lastname']

Address: $_POST['address']

 

etc...

 

now place all that into the $message variable

 

$message = "Name: $_POST['firstname'] $_POST['lastname']

Address: $_POST['address']

 

etc...";

 

the key there is the " double quote, which will see those $_POST variables as the value your user sent

 

understand?

Link to comment
https://forums.phpfreaks.com/topic/112105-e-mail-form/#findComment-575545
Share on other sites

another option

 

$message='';
foreach($_POST as $key => $var){
    $message.=$key.':  '.$var.'\n';
}

 

That will loop through all the post vars and append them to $message.  If you only want certain ones try this:

 

$message='';
$fields=array('firstname', 'address', 'etc');
foreach($_POST as $key => $var){
    if(in_array($key, $fields)){
        $message.=$key.':  '.$var.'\n';
    }
}

Link to comment
https://forums.phpfreaks.com/topic/112105-e-mail-form/#findComment-575550
Share on other sites

Amites,

I could not get your soultion to work, and I did check that I had the double quotes in place.

 

Chris,

Your did work, however the e mail generated looks like this:firstname: John\nlastname: Doe\naddress: 1234 any street

 

Is there anyway to clean it up a bit.. get rid of the \n perhaps or is that necessary for a separator?

 

Thanks for all your help guys!

Link to comment
https://forums.phpfreaks.com/topic/112105-e-mail-form/#findComment-575668
Share on other sites

I think I needed to put the /n in double quotes.  so...

 

$message='';
$fields=array('firstname', 'address', 'etc');
foreach($_POST as $key => $var){
    if(in_array($key, $fields)){
        $message.=$key.':  '.$var."\n";
    }
}

 

If that doesn't work let me know.  It should though.

Link to comment
https://forums.phpfreaks.com/topic/112105-e-mail-form/#findComment-575731
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.