Log in

View Full Version : Help with WebFrom to EMail



jamez100
09-21-2009, 08:13 PM
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

traq
09-21-2009, 08:28 PM
// 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

jamez100
09-21-2009, 08:56 PM
Nice one, thats just what iv been looking for.

Cheers, James

sysout
09-21-2009, 09:28 PM
hey, have you tried how to send email from your localhost?

dj30324
09-21-2009, 09:56 PM
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


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/showthread.php?t=48580

traq
09-22-2009, 01:58 AM
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.

jamez100
09-22-2009, 09:24 PM
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.

traq
09-23-2009, 01:03 AM
<?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).