PDA

View Full Version : Email form from Site



dog
12-14-2006, 01:57 PM
Hi there,

I've been trying to piece together the PHP for a contact form using the other threads as I know there are a few going on but I'm getting stuck at every turn.

I'm new to PHP so I'm just trying to get this practice form to work for now but I have a larger form that I'm really trying to use.

The practice form



<html>
<body>

<form method='post' action='mailform_sender.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>

</body>
</html>

And here's mailform_sender.php:

<?php
function spamcheck($field)
{
//eregi() performs a case insensitive regular expression match
if(eregi("to:",$field) || eregi("cc:",$field))
{
return TRUE;
}
else
{
return FALSE;
}
}

//if "email" is filled out, send email
if (isset($_REQUEST['email']))
{
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==TRUE)
{
echo "Invalid input";
}
else
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("webmaster@torema.com.br", $subject,
$message, "From: $email" );
echo "Thank you for using our mail form";
}
}
else
//if "email" is not filled out, display the form
{
echo "please fill out an email address";
}
?>

Note: The bit at the top is something I learnt from w3school.com to avoid people using the form to send spam.

This form is working fine, and the echo is appearing.

The changes I want to make to start with are:

Return to the page i was on when the form has been sent and write the echo there.
Add more fields to the form and send their values along with the message.

Thanks to anyone that can help and teach me a little about PHP along the way.:)

PS Feel free to start again from scratch if you find it easier to help that way. Thanks.

thetestingsite
12-16-2006, 06:42 AM
You can add more fields to the form easily, just as long as you call them in your PHP code. As far as the redirect, you could redirecting to the original page (lets call it test.php) with the exception that you add something to the end of it. The url would be test.php?send=yes or test.php?send=no.

then in test.php, place this at the beginning.



$send = $_REQUEST['send'];

if ($send == "yes") {

echo 'Your message was sent successfully. You should recieve blahblahblah...';
}

elseif ($send == "no") {

echo 'Your message was NOT sent, due to a problem either with the script or the server. Please try again later!';
}

else {
//echo the contact form
}


This should set you in the right direction. Let me know if you need any further help on this.

dog
12-16-2006, 01:02 PM
Hey man,

Thanks, that worked great.

There's another problem that I'm having:

I'm able to send the form without any name or email. I was using an if else function to check for something in the email field (and the same thing for the name)
if (isset($_REQUEST['email']))
{
//send form
}
else
{
echo 'the email needs to be filled out';
}
but it doesn't seem to be having any effect.

The full size form is going to have a lot of required fields, may be there's a better method all together to doing this. I'm also considering javascript but so far all the methods I've found involve flashing up an alert box if it's invalid. I'd prefer to warn them through something more CSS stylable.

Any suggestions?

thetestingsite
12-16-2006, 04:39 PM
try



if ($_REQUEST['email'] == "" or $_REQUEST['name'] == "") {
//error message

echo 'All form fields are required!';
}

else {

//send the email

mail($to,$subject,$message,$from);

header('Location: test.php?send=yes'); //redirect to success message!
}



That will check to see if email and name fields have anything entered in it.
Hope this helps.

dog
12-16-2006, 05:07 PM
That worked perfectly, thanks again.

It's starting to make sense. When you posted the solusion above I thought, it's so obvious when you know how.

I think I like PHP. :)

I've also been looking at www.php.net. I think you sent me a link to some info on there Testing... it's a good site.

Thanks for all the help!

dog
12-16-2006, 11:12 PM
So I was starting to think I know what I'm doing and I've written this code but there are errors I don't understand.

The PHP takes details from a form on another page, check the required fields aren't empty and then sends a couple of emails.

The problems, I think, start when I try to define the messages of the emails. $message1 = a load of really complicated mixed up code.:o

Here's whole of the code:


<?php

$name = $_REQUEST['name'];
$company = $_REQUEST['company'];
$position = $_REQUEST['position'];
$tel = $_REQUEST['telephone'];
$email = $_REQUEST['email'] ;
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$country = $_REQUEST['country'];
$interest = $_REQUEST['interest'];
$contactType = $_REQUEST['contactType'];
$timeTable = $_REQUEST['timeTable'];
$message = $_REQUEST['message'];
$site = $_REQUEST['site'];

//set country as Brazil is it is not anything else
if ($_REQUEST['country'] == '')
{$country = 'Brasil';}

else
{$country = $_REQUEST['country'];}

// if any of the required fields are blank, redirect to an error message on original page
if ($name =="" or $company =="" or $position=="" or $tel=="" or $email =="" or $city=="" or $state=="" or $interest=="")
{
//...and redirect to an error message.
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'form.php?required=show';
header("Location: http://$host$uri/$extra");
exit;
}

//otherwise send the emails and a redirect to a success note
else
{
//define subjects and messages for each of the emails
$subject1 = 'Fale Conosco - E-mail do Site';
$subject2 = 'Contato com a Torema Brasil';

//here is where the problems start!
//set the message to be sent to the Torema
$message1 = 'E-mail do Site da Pagina: Fale Conosco\r\n\r\n'

'Nome: '.$name.'\r\n'
'Cargo: '.$company.'\r\n'
'Empresa: '.$company.'\r\n'
'Cidade: '.$city.' / '.$state.

//if the country is anything other than Brasil write it
if ($country=='Brasil')
{ echo '\r\n\r\n';}
else
{ echo $country'\r\n\r\n';}

'Intressa: '.$interest.'\r\n'

//if there is a message write it
if ($message=='')
{ echo '\r\n';}
else
{ echo 'Mensagem: '.$message.'\r\n';}

'Deseja Contato Por: '.$contactType.'\r\n'
'Horario de Contato: '.$timeTable.'\r\n\r\n'

'Fone: '.$tel.'\r\n'
'E-mail: '.$email.'\r\n'
'Site: www.'.$site.'\r\n';


//set the thankyou message to be sent to user
$message2 = 'Obrigado por o seu mensagem Torema Brasil \r\n\r\n'
'A gente vai ver blah blah... \r\n\r\n'
'Support, Torema Brasil. \r\n\r\n';

//send the email to Torema
mail("webmaster@torema.com.br", $subject1, $message1, "From: $email" );

//.send the email to the user
mail('<'.$name.'>'. $email, $subject2, $message2, "From: no-reply@torema.com.br"); */

//redirect to the original page and show a success note.
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'form.php?send=yes';
header("Location: http://$host$uri/$extra");
exit;

}

?>


An error turns up for line 49 which is the second of these two:

$message1 = 'E-mail do Site da Pagina: Fale Conosco\r\n\r\n'

'Nome: '.$name.'\r\n'

Yet, I am sure it's not the only fault. If anyone can suggest what's wrong with it and how it could be improved that would awesome.

Peace

dog

thetestingsite
12-16-2006, 11:36 PM
There are a lot of semicolons ( ; ) missing from the ends of the statements, as well as some added single quotes in the variables. I don't have the time to go and correct all of the code right now, but as soon as I get back from my company's Christmas party, I would be more than happy to correct. Unless someone else on the forum does so.

The advise I can give you right now is that you go back and edit the code to add the semicolons to the ends of the statements (echos, variables, etc) and make sure to take out any quotes that should not be there.

Hope this helps!

dog
12-16-2006, 11:56 PM
Thanks teach!

I'll give that a go. Have a good party!

thetestingsite
12-17-2006, 04:34 PM
I went through and cleaned up the code a little bit. I'm not going to place the corrections in red like I usually do, but if you compare the code below to the original, you should be able to tell what I added/edited.



<?php

$name = $_REQUEST['name'];
$company = $_REQUEST['company'];
$position = $_REQUEST['position'];
$tel = $_REQUEST['telephone'];
$email = $_REQUEST['email'] ;
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$country = $_REQUEST['country'];
$interest = $_REQUEST['interest'];
$contactType = $_REQUEST['contactType'];
$timeTable = $_REQUEST['timeTable'];
$message = $_REQUEST['message'];
$site = $_REQUEST['site'];

//set country as Brazil is it is not anything else

if ($_REQUEST['country'] == '') {

$country = 'Brasil';

}


else {

$country = $_REQUEST['country'];

}


// if any of the required fields are blank, redirect to an error message on original page

if ($name =="" or $company =="" or $position=="" or $tel=="" or $email =="" or $city=="" or $state=="" or $interest=="") {
//...and redirect to an error message.

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'form.php?required=show';
header('Location: http://'.$host$uri.'/'.$extra);
exit;
}

//otherwise send the emails and a redirect to a success note
else {
//define subjects and messages for each of the emails

$subject1 = 'Fale Conosco - E-mail do Site';
$subject2 = 'Contato com a Torema Brasil';

//set the message to be sent to the Torema
$message1 = 'E-mail do Site da Pagina: Fale Conosco\r\n\r\n';
$message1 .= 'Nome: '.$name.'\r\n';
$message1 .= 'Cargo: '.$company.'\r\n';
$message1 .= 'Empresa: '.$company.'\r\n';
$message1 .= 'Cidade: '.$city.' / '.$state;

//if the country is anything other than Brasil write it

if ($country=='Brasil') {

echo '\r\n\r\n';

}

else {

echo $country.'\r\n\r\n';

}

$message1 .= 'Intressa: '.$interest.'\r\n';

//if there is a message write it

if ($message=='') {

echo '\r\n';

}

else {

echo 'Mensagem: '.$message.'\r\n';

}

$message1 .= 'Deseja Contato Por: '.$contactType.'\r\n';
$message1 .= 'Horario de Contato: '.$timeTable.'\r\n\r\n';
$message1 .= 'Fone: '.$tel.'\r\n';
$message1 .= 'E-mail: '.$email.'\r\n';
$message1 .= 'Site: www.'.$site.'\r\n';


//set the thankyou message to be sent to user

$message2 = 'Obrigado por o seu mensagem Torema Brasil \r\n\r\n';
$message2 .= 'A gente vai ver blah blah... \r\n\r\n';
$message2 .= 'Support, Torema Brasil. \r\n\r\n';

//send the email to Torema
mail("webmaster@torema.com.br", $subject1, $message1, "From: $email");

//.send the email to the user
mail('<'.$name.'>'. $email, $subject2, $message2, "From: no-reply@torema.com.br");

//redirect to the original page and show a success note.

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'form.php?send=yes';
header('Location: http://'.$host$uri.'/'.$extra);
exit;

}

?>


Let me know if this fixes the problem, or if you need any more help.

dog
12-17-2006, 09:17 PM
Hi thetestingsite (http://thetestingsite.net),

Thanks for the help.

The code didn't work perfectly first time. I've changed a few things, some more necessary than others and now it's working fine. I'll try to summaries the changes I've made. If anyone requests it I'll post the whole of the working code.

I'm using Cream/Vim and changing single (') to double (") quotation marks seems to please it. I can tell because of the way it colours the code.

Also I've copied Mad Professor Mwinter's style of coding and used {$variable} in place of ".$variable." when placing these things, (are they called variables?) within the strings.

In my original code I was creating a lot of if else statements to write one thing if there was something in a field and write another thing if there wasn't. I was using the echo function to write the output but I've changed this a little. Instead of writing one echo or another I've written one definition or another for a new variable (terminology?).

I've made all of the changes more or less at the same time, which was careless as now it's hard to tell what was necessary and what was just different. I'll try to illustrate the changes in the two codes below.

Before (with errors):
$message1 .= 'Cidade: '.$city.' / '.$state;

//if the country is anything other than Brasil write it

if ($country=='Brasil') {

echo '\r\n\r\n';

}

else {

echo $country.'\r\n\r\n';

}

After, (error free):


//if the country is anything other than Brasil write it
if ($country =='Brasil' or $country =='brasil') {

$countryMes = 'Brasil';

}


else {

$countryMes = $country;

}

$message1 .= "Cidade: {$city} / {$state}, {$countryMes}.\r\n\r\n";


Thanks a lot for the help. I still have some more questions, two issues really.

You'll notice I'm using this php code that we've been working on, let's call it form-sender.php, to validate form.php. If someone fills out all of the required fields save one and submits the browser moves to form-sender.php and back to form.php?send=error:required_fields_blank for them to read the echo explaining that not all of the required fields were filled. This way the from has been reloaded and all the fields that were filled out get blanked. Not very convenient! Can anyone suggest another way of using the PHP to check the required fields have content without leaving the page?

The second issue is more urgent but also more simple. When the form is filled out correctly, gets sent and the user arrives on form.php?send=yes and sees and echo telling the it's all good I'd also like to trigger some javascript. Can anyone suggest a trigger?

thetestingsite
12-18-2006, 07:33 PM
Can anyone suggest another way of using the PHP to check the required fields have content without leaving the page?

You could utilize javascript to check the required fields. I think there are some on this website, as well as http://dhtmlgoodies.com. If you still wanted to use the PHP to check it, and have the info saved, you could utilize setcookie(); to save the entered items in the form and when it comes back to the form.php page, the values are already in the fields.


The second issue is more urgent but also more simple. When the form is filled out correctly, gets sent and the user arrives on form.php?send=yes and sees and echo telling the it's all good I'd also like to trigger some javascript. Can anyone suggest a trigger?

What kind of javascript are you trying to trigger? You could do the following in form.php:



if ($send == "yes") {
?>

<html>
<head>
<title>Message Sent</title>
<script language="Javascript">
/*Add JS here*/
</script>
</head>
<?
}

elseif ($send == "no") {
?>

<html>
<head>
<title>Message Not Sent</title>
<script language="Javascript">
/*Add JS here*/
</script>
</head>

<?php
}

else {
?>

<html>
<head>
<title>Send A Message</title>
</head>

<?php
}

?>


Hope this helps out

dog
12-18-2006, 10:39 PM
Hey man thanks for the help.

I found a good javascript form-validator thanks to your links.



What kind of javascript are you trying to trigger?

I'm trying to set a cookie. I had it working in javascript before I learnt anything about PHP so I thought it would be easier to just trigger this script.

Now I'm thinking it might be an idea to rewrite everything in PHP because somethings gone wrong.

Everything is behaving properly in Firefox, including your idea for triggering the javascript. But in IE it's not paying any attention to the cookies at all.

I'll have to check that they.....:D DOH!!! The cookies we set to being blocked in IE. It's all working fine now.

:) Man that makes me happy!

thetestingsite
12-18-2006, 10:45 PM
Well, at least everything is working out for you. If you need any more help, let me know and I'll try to help out as much as I can.