Results 1 to 7 of 7

Thread: PHP mail form help

  1. #1
    Join Date
    Dec 2009
    Posts
    54
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question PHP mail form help

    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

    Can anyone help please?? Thanks in advance

    Here is the code:

    PHP 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"; } 
    ?>
    Last edited by moose86; 05-06-2011 at 09:34 AM.

  2. #2
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Default

    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:
    PHP Code:
    $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 Code:
     <?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:

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


  3. #3
    Join Date
    Dec 2009
    Posts
    54
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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 Code:
     <?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"; } 
    ?>

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by midhul View Post
    In the line where you define headers, replace it with:
    PHP Code:
    $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;.
    Quote Originally Posted by midhul View Post
    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?
    Quote Originally Posted by moose86 View Post
    ...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 Code:
     <?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.

  5. #5
    Join Date
    Feb 2008
    Posts
    81
    Thanks
    8
    Thanked 5 Times in 5 Posts

    Default

    Thanks for correcting the mistake traq.

    I totally forgot to put the === .

  6. #6
    Join Date
    Dec 2009
    Posts
    54
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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 forum.

    If you want to start from scratch, I'd recommend reading a tutorial (like this, this, or this) and then asking questions when you need help with specific items.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •