Jump to content

Mail Function - Should I/How do I add Form fields to $message?


allotrope

Recommended Posts

Hello there,

 

I'm new to PHP from this morning & it's all very exciting!

 

I was wondering if someone might be able to help me: I have created a form & want to send the results via php to my email address.

 

I've used the mail function & I think I have to define the $message as equal to the all the fields of my form (i.e. name, otherformtextinfield etc.. except email).

 

Can anyone give me advice on how to do this?

 

I had hope that when i had ...... , $message, ......)  i could add my form fields to that i.e.  ........, $message $name $otherformtextinfield, ......

 

but that didn't work! Probably

 

Thanks

 

Allotrope.

There are many ways of doing this. Perhaps the easiest is to use the print_r() to dump the values of the $_POST array into your message. It's not really formatted nicely, but it is quick:

<?php
$message = 'The following information was entered:' . "\n";
$message .= print_r($_POST,true);
mail($to,$subject,$message);
?>

 

Ken

 

Thanks Ken I appreciate your response. I tried what you said...

 


<?php

$message = 'The following information was entered:' . "\n";
$message .= print_r($_POST,true);


  mail( "[email protected]", "Newsletter Request", $message, "From: $Email" );
  header( "Location: http://www.website.co.uk" );


?>

 

However I'm not sure then how to create an array?

 

I had tried this

 

<?php

$message = $_REQUEST['Name'], $_REQUEST['Salon'] ;

  mail( "[email protected]", "Newsletter Request", $message, "From: $Email" );
  header( "Location: http://www.website.co.uk" );
?>

Which I know is wrong!

My solution gives you what you're looking for. Why do you want to create an array?

 

Also, while you're testing, don't use the header() function which will redirect your script to a different site. Why are you doing that?

 

Ken

I started learning PHP this morning to add a join newsletter form to a website this morning. So I learn't how to do it in part from here .. h**p://www.thesitewizard.com/archive/feedbackphp.shtml (to explain why I put the website header).

 

I don't know how to use the $_POST function so I can't quite piece it all together.

 

I will post my form if you have the time to look at it..

 


<div id="form">
<script language="JavaScript" src="form.js" type="text/javascript"></script>
  <form id="Newsletter" name="form1" method="post" action="sendflsmailb.php">
    
    <input 	type="text" id="inputname" value="Name" name="Name" 
						onfocus="inputName(); style.color='#000'; style.borderColor='#000';" 
						onblur="inputNameOff(); style.borderColor='';" /><br />

    <input 	type="text" id="inputemail" value="Email" name="Email" 
						onfocus="inputEmail(); style.color='#000'; style.borderColor='#000';" 
						onblur="inputEmailOff(); style.borderColor='';" /><br />

    <input 	type="text" id="inputsalon" value="Salon" name="Salon" 
						onfocus="inputSalon(); style.color='#000'; style.borderColor='#000';" 
						onblur="inputSalonOff(); style.borderColor='';" /><br />

    <input name="Send" type="submit" value="Join Our Newsletter" />
    
    </form>
</div>

 

Could you recommend anywhere to learn PHP online?

 

Thank you!

 

The $_POST superglobal array will contain all the values passed to your script from the form.

 

A good place to learn PHP is W3 Schools. You can also download Apache/PHP/MySQL to your PC and run a local webserver, so you can test things without uploading to a host. I use xampp

 

Ken

Hi Ken,

 

Just to let you know, I tried the script again just now & it worked, I must have missed smomething! Thank you so much,

 

I'm sure I will want to do other stuff with it soon but I'm really grateful for your help.

 

A

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.