Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2007
    Location
    Leeds - UK
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP form sends emails twice for some reason...

    I have a simple PHP form that I am practising with. It should send an email when I have filled out the fields and hit submit, but when I land on the page it will automatically send me a blank email.

    I know this is probably something stupidly simple, but I would appeciate any incite into why this is.

    I've included a simplified version of my code below. I hope someone can help.

    Thanks

    Swi.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <?php
    $from = $_POST['from'];
    $body = "Your message:\n {$_POST['bodyp1']}\n{$_POST['bodyp2']}";

    mail( '###@###.co.uk', 'application assignment', $body, "From: {$from}" );
    ?>

    <body>
    <form id="form1" name="form1" method="post" action="register.php">
    <label>from
    <input type="text" name="from" />
    </label>
    <p>
    <label>body
    <input name="bodyp1" type="text" id="bodyp1" />
    </label>
    </p>
    <p>
    <label>part 2
    <input name="bodyp2" type="text" id="bodyp2" />
    </label>
    </p>
    <p>
    <label>submit
    <input type="submit" name="Submit" value="Register" />
    </label>
    </p>
    </form>
    </body>
    </html>

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yah, try:

    PHP Code:

    $from 
    $_POST['from'];
    if(IsSet(
    $from)){
        
    $body "Your message:\n {$_POST['bodyp1']}\n{$_POST['bodyp2']}";
        
    mail'###@###.co.uk''application assignment'$body"From: {$from}" );


  • Users who have thanked angst for this post:

    sjwatsi (01-25-2008)

  • #3
    New to the CF scene
    Join Date
    Sep 2007
    Location
    Leeds - UK
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the speedy reply.

    Can I just clarify? Does this if(IsSet($from)){} mean that if the from address has been set that the form can send an email? So if it isn't set you can't send an email?

    Sorry I can't test it out right now - I wasn't expecting such a fast reply . But thanks for your you help! If I understood that right then that will be brilliant and sort of fixes another problem too .

    Swi!

  • #4
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yup, you've got it.

  • #5
    New to the CF scene
    Join Date
    Sep 2007
    Location
    Leeds - UK
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Great. Thanks.

  • #6
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by sjwatsi View Post
    Can I just clarify? Does this if(IsSet($from)){} mean that if the from address has been set that the form can send an email? So if it isn't set you can't send an email?
    Just to clarify further... your php code was running every single time you loaded the page. So when you first loaded the page so you could type data into the fields it was already running the php script and sending an email (with those fields being blank).

    Adding isset($from) makes sure that someone entered data into the form and clicked the submit button. Or more specifically it means that someone entered data in the "from" field and then clicked submit.

  • #7
    New to the CF scene
    Join Date
    Jan 2008
    Location
    U.S.A
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @arnyinc
    Are you good with the PHP?I have a little Job for you please contact me on
    lilanjaxson01@yahoo.com we need to sort out briefly.


  •  

    Posting Permissions

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