Log in

View Full Version : Another quick PHP question....



Fehm
06-03-2009, 01:01 PM
Hey, Im learning a bit of PHP as youve probably gathered from my many threads on PHP, and was jsut wondering if someone could help me out with this:
Im making an email contact us form,


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

Thats the very simple form i made.....
My question is, the send.php is going to contact the PHP for what is going to be sent (obviously) And i was wondering why this wont work:



<?php
$to = [email protected];
$subject = "Contact Us";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$sent = mail($to, $subject, $message;) ;
if($sent)
{print "Your mail was sent successfully"; }
else
{print "We encountered an error sending your mail"; }
?>

Thanks for any help,
Callum,
(I feel like such a noob asking sooo many questions)

Excellent2
06-03-2009, 01:03 PM
You're not checking if the form has been posted or not, thats why it won't work.

Dentafrice
06-03-2009, 01:42 PM
It doesn't matter if it's checking or not, it's going to send.php through a posted form, so of course it's going to be posted, although I don't recommend this, you'll get a lot of blank e-mails.

If it was at the top of the form's page, then yes.. it would need to be checked.

The code here is horrible, so I'm going to rewrite it again. Basically the problem, I'm pretty sure, is that your e-mail address: to, is being written as an integer (no ""(s)).

You really need to start using a debugger to check for errors, or turn your error reporting on at the top of your scripts:



ini_set("display_errors", "On");
error_reporting(E_ERRORS);


If you check for errors, you will see that is the problem:

http://img.skitch.com/20090306-km6kg8idr4xp4bj7fkphm86edk.jpg

Another reason, is you have a semicolon in the middle of your mail() before the closing parenthesis.

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

Also, why are you even getting the $email from the post and not even using it?

Put this as your send.php file, it should work:



<?php
if($_POST) {
$to_email = "[email protected]"; // set to e-mail. This needs to be enclosed.
$email_subject = "Contact Us"; // subject for the e-mail.
$email = $_POST["email"]; // gets the e-mail from the post.
$email_message = $_POST["message"]; // gets the message from the post.

/* Let me say, you probably should clean the $message variable for XSS */
/* as your e-mail client probably will display this if it is web-based */

$message = nl2br($message); // converts textbox linebreaks to <br>'s, other wise you have a long message, no wraps.

$send_mail = @mail($to_email, $email_subject, $email_message); // send the e-mail to the user, supress the errors.

/* Checks to see if the mail was sent correctly. */
if($send_mail) {
exit("Your e-mail as successfully send."); // the mail was sent to the user, stop here.
} else {
exit("We have encountered a problem in sending your e-mail. Please try again later."); // the mail was not sent, problem.
}
}
?>

Want to hide these adverts? Register an account for free!