Results 1 to 8 of 8

Thread: Help with WebFrom to EMail

  1. #1
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with WebFrom to EMail

    Hi there

    Im new to the forum (and to php), and was hoping someone could point me in the right direction.

    Im trying to embed a php variable into a html output email.

    So far I have created the html form, and a sample php function for sending the email (shown below). In the php processing script I can retrieve the form values and assigned them to a variable but I'm having problems using these variables in the email html output.

    Here's my code:

    <?php
    // recipient
    $to = 'jamez100@------------';
    $subject = 'WebForm';

    //form values, txtbox,txtbox,textarea
    $name_textbox = $_POST['txtName'];
    $textbox_Email = $_POST['txtcontactemail'];
    $textarea_Message = $_POST['txtmessage'];

    // this is the email html output
    $message = '
    <html>
    <head>
    </head>
    <body>
    <h1>Page Title</h1>
    <p>Name: <?=$name_textboxo?> 'Here I would like to add the form textbox value
    </p>
    <p>Email : <?=$textbox_Email?> </P
    <p>etc</p>
    </html>
    ';

    //SMTP server name
    ini_set("SMTP","localhost");

    // To send HTML mail, the Content-type header must be set
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Additional headers
    $headers .= 'To: jamez100@---------------' . "\r\n";
    $headers .= 'From: jamez100@--------------' . "\r\n";


    // Mail it
    mail($to, $subject, $message, $headers);
    ?>

    Any help would be greatly appreciated.

    Regards, James

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

    Default

    PHP Code:
    // this is the email html output
    $message '
    <html>
    <head>
    </head>
    <body>
    <h1>Page Title</h1>
    <p>Name: ' 
    $name_textbox ' </p>
    <p>Email : ' 
    $textbox_Email ' </p>
    <p>etc</p>
    </body>
    </html>
    '

    you're already in php, so no new tags are needed. just break out of the single-quotes ( ' ) and use the ( . ) to attach the variables
    Last edited by traq; 09-21-2009 at 08:32 PM. Reason: misread OP's code; corrected example

  3. #3
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nice one, thats just what iv been looking for.

    Cheers, James

  4. #4
    Join Date
    Feb 2009
    Posts
    73
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    hey, have you tried how to send email from your localhost?

  5. #5
    Join Date
    May 2007
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Post

    i also notice u are entering php code inside html before doing that did u edit the .htaccess codes to make it work

    like this
    Code:
    RemoveHandler .html .htm .shtml .dhtml .xhtml .phtml 
    AddType application/x-httpd-php .php .htm .html .shtml .dhtml .xhtml .phtml
    this .htaccess code enables you to enter php codes inside html with php tags so others cant see the html code. this is i found on the web, also some can help with this .htaccess code
    ------------------------------------
    http://www.dynamicdrive.com/forums/s...ad.php?t=48580

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

    Default

    this .htaccess code enables you to enter php codes inside html with php tags so others cant see the html code...
    You would still see the html that was output to the browser, and I'm not sure why (or how) the html residing on the server would be any different, so I'm not sure what the point of this would be.
    i also notice u are entering php code inside html before doing that did u edit the .htaccess codes to make it work
    As long as his file extension is .php, it should be handled fine by his server.
    If you're talking about the section of code under //this is the email html output, if you'll notice, he is not entering php code inside html. All the html code in that section is contained within single quotes ( ' html ' ) and is assigned to his string $message.

    I think the .htaccess bit might confuse things at this point. At the very least, it's not necessary for what he's trying to accomplish.
    Last edited by traq; 09-22-2009 at 02:04 AM.

  7. #7
    Join Date
    Sep 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Nice one folks, appreciate the help and got it sort now thanks to your advise.

    The only problem Im still having is processing a checkbox.

    My current code executes fine as long at the checkbox is checked, but if the user leaves the checkbox blank I get the following error.

    Undefined index: thechkbox

    Here is my code.

    $ch1 = $_POST['thechkbox'];

    if ($ch1 == 'checked') {
    $ch1 = 'my output message, the checkbox was checked';
    }
    else
    {
    $ch1 = 'no the checkbox wasn't checked';
    }

    Iv had a look around the web and I think the problem occurs because if the checkbox is not checked there is no value assigned to the variable, hence the error

    The problem is I havne't figured how to handle the checkboxes correctly.

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

    Default

    PHP Code:
    <?php
    if(isset($_POST['thechkbox'])){ 
       echo 
    'checkbox was checked'
    }else{ 
       echo 
    'checkbox wasn\'t checked'
    }

    // OR

    $ch1 = isset($_POST['thechkbox']) ? 'checkbox was checked' 'checkbox wasn\'t checked';
    echo 
    $ch1;
    this doesn't check for the checkbox's value; if there's only one checkbox with the name thechkbox it isn't really necessary. If you had an array of checkboxes, you'd want to verify the values. here, you're only seeing if the checkbox isset (has some non-NULL value) or not (is empty).
    Last edited by traq; 09-23-2009 at 01:11 AM.

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
  •