Log in

View Full Version : Mail form



chechu
11-19-2006, 04:45 PM
Below you will see a .php mail form code. When I use a submit button in the contact.html, it all works fine. But when I use an image to submit, it doesn't work. It seems like something isn't correct in the .php.
Can anyone adapt and make the script better, please ?


<?
if ($_POST["Submit"]){

if ($_POST["name"] and $_POST["email"] and $_POST["message"] ){

mail (
"info@casariegoart.com",
"via website (EN)",
"
Name: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");

echo '<p align="center"><font color="#003366">Thanks !<br>Your message has been sent.<br> We will get back to you as soon as we can.</font></p>';
}
else{
echo '<p align="center"><font color="#FF0000">Please fill in all data!<br>All fields are obligatory.</font></p><p><a href="contactEN.html">[back]</a>';

}
}
?>

thetestingsite
11-19-2006, 10:16 PM
When using the image, do you set the name as "Submit"? As a test, try taking out the


if ($_POST["Submit"];) {

in the above code, and use the image in the html form.

(Remember to take the last bracket ( } ) out of the php file or else it will cause an error once you take the first if statement out.)

Another thing you could try is adding a hidden field in the form (let's call it "act") with a value (let's say "submit_form"). Then instead of the


if ($_POST["Submit"]) part, change it to


if ($_POST["act"] == "submit_form")

That's just two solutions. Let me know if you need further help.

penguin
11-20-2006, 12:22 AM
the BEST way I have found to SUBMIT forms is to use INPUT TYPE=SUBMIT. Since you are saying that your form works with a submit button and not with an image button. Best bet is that there is not a problem with your php script.

The solution to this problem is extremely simple. USE A SUBMIT BUTTON.

The hidden type is a great technique posted above. I use this technique daily.

As I posted earlier if you want your submit button to be an image then you can do that, best way is with CSS.

thetestingsite
11-20-2006, 02:38 AM
I agree with everything that penguin stated above. And as said above


The hidden type is a great technique posted above. I use this technique daily.

I as well use it in just about all of my scripts and programs that I create, due to the fact that if you simply request in your PHP scripts the name of the form element (i.e.:

<input type="name" value="something">
and


if ($_POST[name])


anybody could make the intended program cause an error, or cause the program to do something that was not intended when it was created. (If that makes any sense.)

Also, as stated above, the best way to change the submit button is through css. This is yet another technique that I have personally used in programs and whatnot.

Hopefully this helps you out.

chechu
11-20-2006, 10:42 AM
Sorry, but I am no expert at all in this matter.
You say the .php is not secure. How would the above code look like if you let it be sent by an image, and secured for errors ?
How would the css look like ?

chechu
11-20-2006, 10:52 AM
Is the following correct ?

Code for the contact.html



<html>
<head>
<script language="JavaScript" type="text/JavaScript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
</script>
</head>
<body background="bgpalet.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="80%" height="100%" border="0" cellpadding="0" cellspacing="0" align=right>
<tr><td align="center" valign="middle">

<table border="0" width=100% align=right>
<tr><td width="120" valign="middle">

</td><td valign="middle" align="right">

<FORM NAME="form1" METHOD="post" ACTION="contactEN.php">
<input type="hidden" name="action" value="send">
<table width="100%" valign="top" align=center>
<tr><td>
<table width=100%>
<tr><td align=left>
<INPUT TYPE=TEXT NAME="name" VALUE="your name" onFocus="clearDefault(this)" style="border:1px solid #666666; width:190px">
</td></tr>
<tr><td align=left>
<INPUT TYPE=TEXT NAME="email" VALUE="your e-mail" onFocus="clearDefault(this)" style="border:1px solid #666666; width:190px">
</td></tr></table>
</td><td width=20>
&nbsp</td><td align=left valign=middle>
<INPUT TYPE="image" src="emailgo.jpg" NAME="submit" VALUE="go" style="border: 0px">
</td></tr></table>

<table width="100%" valign="top" align=center>
<tr><td align=right>
<TEXTAREA NAME="message" style="border:1px solid #666666; width:265px; height:120px">
</TEXTAREA>
</td></tr></table>
<br>
<table width="100%" valign="top" align=center>
<tr><td align=center>
<fieldset style="border:1px solid #666666; width:260px">
tel: 0032.475.98.49.96
</fieldset></td></tr></table>

</FORM>

</td></tr></table>
</td></tr></table>

</body>
</html>


Code for the contact.php:


<?
if ($_POST["action"] == "send"){

if ($_POST["name"] and $_POST["email"] and $_POST["message"] ){

mail (
"info@casariegoart.com",
"via website (EN)",
"
Name: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");

echo '<p align="center"><font color="#003366">Thanks !<br>Your message has been sent.<br> We will get back to you as soon as we can.</font></p>';

}

else{
echo '<p align="center"><font color="#FF0000">Please fill in all data!<br>All fields are obligatory.</font></p><p><a href="contactEN.html">[back]</a>';

}
}
?>

thetestingsite
11-21-2006, 01:48 AM
I have tested the above code, and it seems to work correctly. If you experience any problems with it, let me know and I'll see what I can do about it. Other than that, you should be good to go with the pages above.

chechu
11-21-2006, 10:46 AM
I received a test e-mail, I suppose it was yours ?
Thank you so much !!!!!! I am starting now a fantastic day after three hours of traffic jam in Brussels, so again, someone unknown to me makes my day !

chechu
11-21-2006, 08:17 PM
The form works well, thanks.
But as in the first two fields there is already text written (your name, your e-mail), it is possible to send the mail by only filling in the text area.
How can this be avoided ?
And how ca

chechu
11-21-2006, 08:19 PM
And how can I make sure that they fill in a valid email adres (with @ f.ex.) ?

thetestingsite
11-22-2006, 03:12 PM
To verify email addresses, I would use javascript. As far as the only submitting data from the textarea, simply take this line out of the php:



if ($_POST[name] and $_POST[email] and ...


and just simply have this:



if ($_POST[textarea])


That will only ensure that they have something in the textarea.

And also, that test email was from me. I uploaded your code to my server, and forgot to change the $to email address. Sorry.

chechu
11-23-2006, 11:02 AM
To verify email addresses, I would use javascript. As far as the only submitting data from the textarea, simply take this line out of the php:


Code:
if ($_POST[name] and $_POST[email] and ...and just simply have this:


Code:
if ($_POST[textarea])That will only ensure that they have something in the textarea.


I see what you mean, but taking it away means that I cannot force them to place name and e-mail. I need the three infos.

thetestingsite
11-23-2006, 06:51 PM
Well one other thing you could take into consideration is in the HTML form, you have it set to have the value of "Your Name" and "Your email", and those disappear when you click on the form field. What if they don't click on it and only type in the textarea. Then you will recieve an email with

Name: Your name
Email: Your Email
Message: Whatever they type in.

To eliminate this issue, you could make the if statement I spoke of before to do the following:



if ($_POST[name] != "Your name" and $_POST[name] != "" and $_POST[email] != "Your email" and $_POST[email] != "" and $_POST[message] != "") {

/* This will verify they have entered something in the fields other than the default values. Change as needed */


Let me know if this helps or not.

chechu
11-24-2006, 03:47 PM
It works perfectly.
Thanks a lot !!

chechu
11-26-2006, 09:19 AM
Is it possible to have a text already in the textarea as well ? (like: your message goes here)

thetestingsite
11-26-2006, 05:06 PM
You could do it like so:



<textarea> Type your message here! </textarea>

chechu
11-27-2006, 08:17 AM
If I just type it, the text does not go away when I click in the textarea. It does so in the "name" and "adress". Can the same be done in textarea ?

thetestingsite
11-27-2006, 05:22 PM
Yes, by using javascript. Not sure right off the top of my head, but give me a little while and I'll post a code snippet for you to use.

thetestingsite
11-27-2006, 07:43 PM
<textarea name="info" rows="7" cols="40" onfocus="if (this.value=='Enter your text here.') {this.value='' }" onblur="if (this.value=='') {this.value='Enter your text here.'} ">Enter your text here</textarea>

That should do it, you could probably save it in an external file, or in the head of the document (not sure how to do it exactly).

chechu
11-28-2006, 12:18 PM
There is a part of the script missing, right ? I place this in the form, but what do I place in the head ?
<textarea name="info" rows="7" cols="40" onfocus="if (this.value=='Enter your text here.') {this.value='' }" onblur="if (this.value=='') {this.value='Enter your text here.'} ">Enter your text here</textarea>

thetestingsite
11-29-2006, 12:42 AM
onfocus="if (this.value=='Enter your text here.') {this.value='' }"
this part clears the data if there is the defualt text in it and


onblur="if (this.value=='') {this.value='Enter your text here.'}"
this part returns the default text if there was nothing entered in the textarea. Like I said in my previous post, you could put it in the head of the html but I'm not sure how you would do it.

chechu
11-29-2006, 12:04 PM
Thanks, I figured it out.
Another question: what do I need to add in the php code, to have a customized confirmation mail being sent to the adress filled in the form ?

thetestingsite
11-29-2006, 03:26 PM
simply add another mail(); to the script, but instead of putting in your email address for the to field, use the variable for what they entered. change the subject, and the message if you would like as well.

chechu
11-29-2006, 03:31 PM
Really big thanks for all your help and explanations, but you are talking to a rookie. Can you show me the codes with explanations, please ?

thetestingsite
11-29-2006, 08:54 PM
As you have in your php script



mail (
"info@casariegoart.com",
"via website (EN)",
"
Name: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");


just simply add another one somewhere else in the script. So it will look almost like the following:




$to1 = "me@mydomain.com"; //your email address
$to2 = $_REQUEST["email"]; //the email address entered in the form

$subject1 = "Form mail script"; //subject for the email going to you
$subject2 = "Thank you for submitting the form"; /*subject for the confirmation email*/

$message1 = "This is the message you will recieve."; //self-explained
$message2 = "This is the message going to the person who submitted the form"; //self-explained

$headers = "From: My Website <noreply@mydomain.com>";

mail($to1, $subject1, $message1, $headers); //this one goes to you
mail($to2, $subject2, $message2, $headers); //this one goes to the other person.


Hope this kinda explains it. If not let me know and I'll try to incorporate it with the script you already have set up.

chechu
11-30-2006, 10:09 AM
Thanks a lot. I placed the codes together, with a few questions in it. Could you check them please, and correct them where needed ?


<?
if ($_POST["action"] == "send"){

if ($_POST[name] != "your name" and $_POST[name] != "" and $_POST[email] != "your e-mail adress" and $_POST[email] != "" and $_POST[message] != "") {

mail (
"info@casariegoart.com",
"via website (EN)",
"
Name: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");

echo '<p align="center"><font color="#003366">Thanks !<br>Your message has been sent.<br> We will get back to you as soon as we can.</font></p>';

}

else{
echo '<p align="center"><font color="#FF0000">Please fill in all data!<br>All fields are obligatory.</font></p><p><a href="contactEN.html">[back]</a>';

$to1 = "info@casariegoart.com";
$to2 = $_REQUEST["email"];

$subject1 = "message via website";
$subject2 = "Thank you for submitting the form";

$message1 = "This is the message you will recieve."; // here I place nothing ?
$message2 = "This is the message going to the person who submitted the form"; // so here I place the customized text ?

$headers = "From: My Website <noreply@mydomain.com>"; //what is headers ?
mail($to1, $subject1, $message1, $headers);
mail($to2, $subject2, $message2, $headers);

}
}
?>

thetestingsite
12-01-2006, 02:22 AM
The example i posted for you was just that, an example; but you have got the idea. The headers is the thing in the email that tells you who and where the email was sent from. Usually this is done automatically, but if you wanted to edit the headers yourself then use this, otherwise you can take it out (it is optional).

thetestingsite
12-01-2006, 03:19 AM
After looking over the code a little closer, I noticed a few things out of order. Below is the modified code as to how it should look.



<?php

if ($_POST["action"] == "send"){

if ($_POST[name] != "your name" and $_POST[name] != "" and $_POST[email] != "your e-mail adress" and $_POST[email] != "" and $_POST[message] != "") {

//send the info submitted in the form

mail ("info@casariegoart.com", "via website (EN)",
"
Name: ".$_POST['name']."
E-mail: ".$_POST['email']."
Message: ".$_POST['message']."

",
"From: ".$_POST['name']." <".$_POST['email'].">");

//assign the simple variables
$subject = "Your message was recieved!"; //subject for confirmation email

$msg = "Thank you for submitting your data to us. A rep will contact you as soon as possible. Blahblahblahblah"; //confirmation email message.

$headers = "From: Casariegoart.com <info@casariegoart.com>";
/*who the message is being sent from */

//send the confirmation to the user
mail($_POST[email], $subject, $msg, $headers);

echo '<p align="center"><font color="#003366">Thanks !<br>Your message has been sent, and you have been sent a confimarion email as a "reciept". <br> We will get back to you as soon as we can.</font></p>';

}

else{
echo '<p align="center"><font color="#FF0000">Please fill in all data!<br>All fields are obligatory.</font></p><p><a href="contactEN.html">[back]</a>';
}
}
?>


That should do it.

chechu
12-01-2006, 02:54 PM
Works perfectly. Thanks a lot ! Just two more questions, and then I guess I'll leave you alone.
- how can I add <html> in this sentence (like <br>)?

$msg = "Thank you for submitting your data to us. A rep will contact you as soon as possible. Blahblahblahblah"; //confirmation email message.

- how can I place a copy of the sent mail into the confirmation mail (unther the above text) ?

thetestingsite
12-01-2006, 04:43 PM
well you could try something like this, but keep in mind that some email clients do not support HTML emails; however, most of them do.




$msg = <<<HERE

This will be the message of the email. <BR>
You <font color="red">should</font> have recieved this &amp; that. <BR><HR><BR>
The message you submitted was the following:<BR>
Name: $_POST[name] <BR>
E-mail: $_POST[email] <BR>
Message: $_POST[message]
<BR><HR><BR>

Thank you,<BR>
Blah blah!!!

HERE;

chechu
12-01-2006, 08:08 PM
No, with my server it doesn't work; I can see the codes.
But when I place
text
like
this
I can have new lines. Hopefully this will work with all browsers ?

And what happens if the email submitted is not a real one ? Can that be protected (f.ex. there must be a @) ?

thetestingsite
12-01-2006, 08:23 PM
You could use regular expressions to check for these items. Look through the forums there are a ton of posts about this. Sorry, I cannot help you with this, but I am short of time at the moment. Perhaps some other posters can help you out a little more than me. Also, you could probably find some Javascripts on DD that have form validation.

chechu
12-01-2006, 08:29 PM
Thank you so much for all your help.
I really appreciate it. And I know that lots of other posters will find a lot of inspiration in this thread. Thanks !