Html form passing to php

Soldato
Joined
18 Oct 2002
Posts
9,160
I have spent a couple of hours wading through tutorials trying to find out how to do the following, they all seem to be far too complicated and there seems to be a million and one ways to do it:

I have 3 fields, FirstName, Surname, and Email. When the user hits submit, the data within the textboxes are passed to php, which in turn sends to my email ([email protected] for instance) with the subject "User Details". The user is then directed to "http://www.domain.com/thankyou.php"

Once this has been sent, I want to recieve an email at the above domain nicely formatted (must be in the correct order):

FirstName: Fred
Surname: Brown
Email: [email protected]

Code:
<?
	$recipient = '[email protected]' ;
	$subject = 'User Details' ;

	
	
	$FirstName = $_REQUEST['FirstName] ;
	$Surname = $_REQUEST['Surname'] ;
	$Email = $_REQUEST['Email'] ;
	


mail( "$recipient","$subject",
	"\n First Name: \n $FirstName",
	"\n Surname: \n $Surname",
	"\n Email:\n $Email", ;
	

header( "Location: http://www.domain.com/thankyou.php" );
?>

Why won't this work? :confused:
 
Associate
Joined
1 Dec 2003
Posts
246
Location
High Wycombe
have you tried

Code:
<?
	$recipient = '[email protected]' ;
	$subject = 'User Details' ;

	
	
	$FirstName = $_POST['FirstName] ;
	$Surname = $_POST['Surname'] ;
	$Email = $_POST['Email'] ;
	


mail( "$recipient","$subject",
	"\n First Name: \n $FirstName",
	"\n Surname: \n $Surname",
	"\n Email:\n $Email", ;
	

header( "Location: http://www.domain.com/thankyou.php" );
?>
 
Soldato
OP
Joined
18 Oct 2002
Posts
9,160
Thanks for the advice guys, the above was a basic explanation just to get it working (which it did :)), however, I now have more than 5 data items and get the following error:

Warning: mail() expects at most 5 parameters, 19 given in /path/ on line 41

So how do I get around this? :confused:

Edit: Found a really good example here: http://forums.digitalpoint.com/showthread.php?t=2849
 
Last edited:
Soldato
Joined
26 Dec 2003
Posts
16,522
Location
London
Look at the manual, close strings that you start, do some kind of input validation and cleaning, look at the manual, actually send a message body in the email and not just a subject and an empty message, look at the manual, look at the manual and don't indent like a silly person. Oh, and look at the manual.

Here's a (totally-untested-but-should-be-working) start:

Code:
<?php

function clean( $string ) {
	return preg_replace('#[^a-z0-9 \-_]#i', '', $string);
}

session_start();

$recipient	= '[email protected]';
$subject	= 'User Details';

$firstName	= clean($_REQUEST['firstName']);
$lastName	= clean($_REQUEST['lastName']);
$email		= clean($_REQUEST['email']);
$message	= clean($_REQUEST['message']);

$headers = "From: $firstName $lastName <$email>\r\nReply-To: $firstName $lastName <$email>\r\n";

if( empty($firstName) || empty($surname) || empty($email) )
	die('You need to enter something in all the fields, silly.');

if( $_SESSION['lastMailed'] + 3600 > time() )
	die('Slow down there cowboy, you can only send one message per hour.');

mail($recipient, $subject, $message, $headers);

header('Location: http://www.domain.com/thankyou.php');

?>
 
Soldato
OP
Joined
18 Oct 2002
Posts
9,160
Having not used PHP much prefer I have found it quite difficult to even do the simplest of things. This is just being used as an example, I don't need to enter validation etc as its a bit OTT.

Thanks anyway, all sorted now. Works flawlessly!
 
Man of Honour
Joined
31 Jan 2004
Posts
16,335
Location
Plymouth
Adam said:
I don't need to enter validation etc as its a bit OTT.
If you don't do it when you're learning, you'll forget and not get into the habit :)

It's really, really important to read the manual. http://www.php.net is the single best resource for PHP. You can lookup any function via http://www.php.net/function where function is the function name. Full information, best practices, and a wealth of comments. Sorted :)
 
Associate
Joined
18 Oct 2002
Posts
1,752
Location
Southern England
Beansprout said:
If you don't do it when you're learning, you'll forget and not get into the habit :)

quoted for truth.

When you're learning it's just as important to learn the right "way" of doing things as it is to learn how to do things. Always do the little things right and the big things will take care of themselves.
 
Associate
Joined
21 Oct 2003
Posts
228
'I don't need to enter validation etc as its a bit OTT'

With mail forms especially, validation is vital : One of the favourite spammers techniques at the moment is to find contact email forms with no/little form validation on php sites and use these to send out their emails in the hundreds of thousands. Needless to say, this is not going to do your bandwidth/email allocation any good, and is very likely to get your website shut down by your webhost.
 
Back
Top Bottom