Log in

View Full Version : PHP mail form help



moose86
05-06-2011, 09:27 AM
Hi,

I have made a form and told it to send via web.php (form action="web.php")
The form works and it deoes send an email to the address specified but it sends a blank message with the email subject as 00:00 :confused:

Can anyone help please?? Thanks in advance:D

Here is the code:


<?php
$to = "myemail@domain.co.uk";
$subject = "Website Order";
$email = $_REQUEST['one']; <!-- Checkbox 1 -->
$message = $_REQUEST['two']; <!-- Checkbox 2 -->
$headers = "From: $email";
$sent = mail
($to, $subject, $message, $headers);
if($sent) {print "Your mail was sent successfully"; } else {print "We encountered an error sending your mail"; }
?>

midhul
05-06-2011, 10:14 AM
Firstly, remove those comments (<!-->) those are HTML comments.
In PHP, we use either, // or /* comment */, to specify comments.

In the line where you define headers, replace it with:


$headers = "From:" . $email;


If you write a variable within quotes, it's value will nt be returned by PHP.

So your final code would be:



<?php
$to = "myemail@domain.co.uk";
$subject = "Website Order";
$email = $_REQUEST['one']; //checkbox 1
$message = $_REQUEST['two']; //check box 2
$headers = "From:" . $email;
if(mail($to, $subject, $message, $headers)) {
echo "Your mail was sent successfully"; } else { echo "We encountered an error sending your mail"; }
?>




Anyway, getting email and message from checkboxes? Are you sure about what your doing? For your information, check boxes return boolean values(true or false), and I don't think you would want a true or false as your email and message

May be you want to make it display some message if a chech box is selected:



if ($_REQUEST['one'] = true) {
$message = "Option 1 was selected";
}

moose86
05-06-2011, 10:37 AM
hi, thanks for the tip, im new to php, just getting stuff from google atm, lol

i have amended it and now it works, but as u can see below, the coding where u mentioned about putting if statements for the boxes, it just emails "Option 2 was selected" even tho i have sent 2 diff emails with on of the boxes checked at one time, did i put the code in the wrong place?


<?php
$to = "mail@domain.co.uk";
$subject = "Website Order";
if ($_REQUEST['one'] = true) {
$message = "Option 1 was selected";
}
if ($_REQUEST['two'] = true) {
$message = "Option 2 was selected";
}
$headers = "From:" . $email;
if(mail($to, $subject, $message, $headers)) {
echo "Your mail was sent successfully"; } else { echo "We encountered an error sending your mail"; }
?>

traq
05-06-2011, 03:33 PM
In the line where you define headers, replace it with:


$headers = "From:" . $email;

If you write a variable within quotes, it's value will nt be returned by PHP.
This is not correct; variables are expanded inside double-quotes ( " ). $headers = "From: $email"; will work just fine, and will provide the same result as $headers = "From: ".$email;.

Anyway, getting email and message from checkboxes? Are you sure about what your doing? For your information, check boxes return boolean values(true or false)
Right. Do you want to display a "yes or no" input, or do you want the user to enter their email and message?

...as u can see below, the coding where u mentioned about putting if statements for the boxes, it just emails "Option 2 was selected" even tho i have sent 2 diff emails with on of the boxes checked at one time, did i put the code in the wrong place?


<?php
$to = "mail@domain.co.uk";
$subject = "Website Order";
if ($_REQUEST['one'] = true) {
$message = "Option 1 was selected";
}
if ($_REQUEST['two'] = true) {
$message = "Option 2 was selected";
}
$headers = "From:" . $email;
if(mail($to, $subject, $message, $headers)) {
echo "Your mail was sent successfully"; } else { echo "We encountered an error sending your mail"; }
?>
What's happening here is that you're using the wrong operator.

if ($_REQUEST['two'] = true) (with a single equal sign ( = )) means you're assigning the value of "true" to $_REQUEST['two'].
(If you do this, then $_REQUEST['two'] will always be "true," regardless of what was entered in the form: you're changing the value.)

Since you actually want to check if the value is true, you need to use a triple equal sign ( === ), like so:

if($_REQUEST['two'] === TRUE)

A double equal sign ( == ) will also work in this case, but is less specific: it will return true for TRUE, 1, or any other value except FALSE, 0, '0', NULL, or '' (an empty string).

One more recommendation is to _not_ use $_REQUEST. This is an outdated practice and can create security holes in your scripts. Since you know the info you want is coming from a form, you should only accept them if they are found in the $_POST superglobal.

If you'd like more assistance, please post the html <form> you're using.

midhul
05-06-2011, 04:44 PM
Thanks for correcting the mistake traq.

I totally forgot to put the === .

moose86
05-06-2011, 05:52 PM
In answer to ur question traq,

I am trying to make a form that has Radio buttons, Checkbox's (that say yes and no), text area's, emil, name, upload's (images)

if u could make a little php script for me displaying each type of these, i can alter it :) (If you could nicely do that for me please i would be greatful)

Thank You

traq
05-06-2011, 07:37 PM
I have no idea how you want each of the form inputs to be designed. If you post your existing html form, and describe how you want to change it, it would be a good "starting point" and I could offer advice; if you just want someone to make a form and processing script for you, you might post in the paid help (http://dynamicdrive.com/forums/forumdisplay.php?f=30) forum.

If you want to start from scratch, I'd recommend reading a tutorial (like this (http://www.javascript-coder.com/html-form/html-form-tutorial-p1.phtml), this (http://www.html-form-guide.com/php-form/php-form-tutorial.html), or this (http://myphpform.com/php-form-tutorial.php)) and then asking questions when you need help with specific items.