• 0

PHP Email HTML Form to PHP Code to Email


Question

Hello there,

i am hoping someone out there can tell me where im going wrong.

I have a website where i am trying to get potential customers to fill out 5 simple text boxes on a HTML page and for the text entries to be processed by PHP code to send an e-mail to my e-mail inbox.

I have put the following code together and after spending hours to get over Parse and syntax errors. It finally sends me an e-mail.

The problem being that none of the text entered onto the HTML form appears in the e-mail at the end.

Can you help?

Heres the code

Running 2 files - one for the PHP and one for the HTML page.


<html>
<form action="send.php" method="post">
Name:<br> <input type="text" name"names"><br>
Email:<br /><input type="text" name"email"/><br />
Date From: <br /><input type"text" name"from"/> <br />
Date to: <br /><input type"text" name "dateto"/><br />
Details: <br /><textarea name"details"></textarea>
<input type="submit" name="Submit" value"send" />
</form>

</html>
[/CODE]

and here is the 2nd file with all the php code

- i believe the probelm lies when we get to the $body part:

[CODE]
<!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=utf-8" />
<title>send.php</title>
</head>
<?php
$names = $_post['names'];
$email = $_post['email'];
$from = $_post['from'];
$dateto = $_post['dateto'];
$details = $_post['details'];
$to = "MY EMAIL ADDRESS HERE";
$subject = "Customer Enquiry";
$body = 'Name:'.$names .'Email:'.$email .'From:'.$from .'To:'.$dateto .'Details:'.$details;
mail ($to,$subject,$body);
echo "Thank you for your enquiry. Your Message has been Sent. You will be contacted as soon as is possible. <a href='http://www.stleonard.info/default.html'>Click Here";
?>
<body>
</body>
</html>
[/CODE]

Thanks !

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

You've forgot "=" for some name and value attributes, e.g.:

Date to: <br /><input type"text" name="dateto"/><br /
^
here _/
[/CODE]

Therefore $_POST didn't even contain the assumed entries.

Prevent this by temporarily echoing variables to see if they contain expected values, e.g., [font=Courier New]print_r($_POST);[/font] or [font=Courier New]echo $body;[/font]

Or you can use HTML validator which amidst of being confused by all other mischiefs gives you:

25668aq.png

Link to comment
Share on other sites

  • 0

i knew it would be something as simple as that - thanks

I also am having difficultie understanding why i never thought of getting the compiled message to print to the browser! Duh!

Thanks A MILLION

A

Link to comment
Share on other sites

  • 0

Okay,

I have spent a while now but have managed to remvoe all the errors from the code.

The only problem now, is that it doesnt do what it is suppost to do.

Getting Data entered by a user from a webpage in HTML >> sent to PHP >> sent to my e-mail inbox.

I get the emails but the data isnt in the message also using the code


echo $body
[/CODE]

produces nothing but the fields i typed.

I am all out of ideas now...

Any suggestions?

heres the amended code:

THE HTML FILE:

[CODE]
<!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=utf-8" />
<title>Form</title>
</head>
<body>
<form name="input" action="send.php" method="post"/>
Name:<input type="text" name="names"/>
Email:<input type="text" name="email"/>
Date From:<input type="text" name="from"/>
Date to:<input type="text" name="dateto"/>
Details:<textarea name="details" rows="8" cols="30"></textarea>
<input type="submit" name="Submit" value="Send"/>
</body>
</html>
[/CODE]

Heres the PHP File doing the mailing work:

[CODE]
<!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=utf-8" />
<title>send.php</title>
</head>
<?php
$names = $_post['names'];
$email = $_post['email'];
$from = $_post['from'];
$dateto = $_post['dateto'];
$details = $_post['details'];
$to = "MYEMAIL GOES HERE";
$subject = "Customer Enquiry";
$body = 'Name:'.$names .'Email:'.$email .'From:'.$from .'To:'.$dateto .'Details:'.$details;
mail ($to,$subject,$body);
echo "Thank you for your enquiry. Your Message has been Sent. You will be contacted as soon as is possible. <a href='http://www.stleonard.info/default.html'>Click Here" /n;
echo $body;
?>
<body>
</body>
</html>
[/CODE]

Link to comment
Share on other sites

  • 0

Variables in PHP are case-sensitive. Try using $_POST instead of $_post.

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name="input" action="send.php" method="post"/&gt;
Name:&lt;input type="text" name="names"/&gt;
Email:&lt;input type="text" name="email"/&gt;
Date From:&lt;input type="text" name="from"/&gt;
Date to:&lt;input type="text" name="dateto"/&gt;
Details:&lt;textarea name="details" rows="8" cols="30"&gt;&lt;/textarea&gt;
&lt;input type="submit" name="Submit" value="Send"/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

and

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;send.php&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
$names = $_POST['names'];
$email = $_POST['email'];
$from = $_POST['from'];
$dateto = $_POST['dateto'];
$details = $_POST['details'];
$to = "MYEMAIL GOES HERE";
$subject = "Customer Enquiry";
$body = 'Name:'.$names .'Email:'.$email .'From:'.$from .'To:'.$dateto .'Details:'.$details;
mail ($to,$subject,$body);
echo "Thank you for your enquiry. Your Message has been Sent. You will be contacted as soon as is possible.&lt;br /&gt;";
echo "&lt;a href='http://www.stleonard.info/default.html'&gt;Click Here&lt;/a&gt;";
echo $body;
?&gt;
&lt;/body&gt;
&lt;/html&gt;

Link to comment
Share on other sites

  • 0

It is a good practice to sanitize your input. Example:

$name = htmlspecialchars($_POST['name']);

Could you elaborate? - htmlspecialchars? - it is as you say in the code example below... Just dont understand what you mean by HTML Special Characters?


$names = $_POST['names'];
[/CODE]

Link to comment
Share on other sites

  • 0

In practice, users can write anything in the provided form fields. Someone with malicious intents can insert harmful scripts in your page, access database with admin rights, execute shell commands etc., in other words, it's how most of the security breaches are being done.

It's the task of the programmer to prevent such attacks by outright removing such commands and returning an error to the user before doing anything else with them. If such information needs to be preserved (e.g., like any stuff between code tags in these forums) then most non-alphanumeric characters (e.g., tag braces < and >) need to be converted to HTML entities (respectively, < and >) and various other safety precautions followed.

This kind of stuff is likely overwhelming now, I tried to be concise. There are many methods and helpful functions (like htmlspecialchars() mentioned above); people write whole books about it alone. However, it's the most important thing to implement if site is intended for production use.

Link to comment
Share on other sites

  • 0

For the benefit of those reading this forum also looking for help:

The resolved code, and the reason for its faliure.

HTML FILE (form.html)


<!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=utf-8" />
<title>Form</title>
</head>
<body>
<form name="input" action="send.php" method="post"/>
Name:<input type="text" name="names"/><br />
Email:<input type="text" name="email"/><br />
Date From:<input type="text" name="from"/><br />
Date to:<input type="text" name="dateto"/><br />
Details:<textarea name="details" rows="8" cols="30"></textarea><br />
<input type="submit" name="Submit" value="Send"/>
</form>
</body>
</html>
[/CODE]

PHP (send.php)

[CODE]
<!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=utf-8" />
<title>send.php</title>
</head>
<?php
$names = htmlspecialchars($_POST['names']);
$email = htmlspecialchars($_POST['email']);
$from = htmlspecialchars($_POST['from']);
$dateto = htmlspecialchars($_POST['dateto']);
$details = htmlspecialchars($_POST['details']);
$to = "enter your email here";
$subject = "Customer Enquiry";
$body = 'Name:'.$names .'Email:'.$email .'From:'.$from .'To:'.$dateto .'Details:'.$details;
mail ($to,$subject,$body);
echo "Thank you for your enquiry. Your Message has been Sent. You will be contacted as soon as is possible.<br />";
echo "<a href='http://www.stleonard.info/default.html'>Click Here</a>";
?>
</body>
</html>
[/CODE]

The probelm was:

[CODE]
$names = htmlspecialchars($_POST['names']);
$email = htmlspecialchars($_POST['email']);
$from = htmlspecialchars($_POST['from']);
$dateto = htmlspecialchars($_POST['dateto']);
$details = htmlspecialchars($_POST['details']);
[/CODE]

$_POST MUST BE CAPITAL - lowercase prevents data transfering from field to variable.

problem number 2:

[CODE]
echo "Thank you for your enquiry. Your Message has been Sent. You will be contacted as soon as is possible.<br />";
echo "<a href='http://www.stleonard.info/default.html'>Click Here</a>";
[/CODE]

That semi colon at the end of the code.... - a friend has said to me "everything i do in my day ends with a semi colon".

Ill certainly remember this when coding in PHP again.

Thanks for the advice

Alex

Link to comment
Share on other sites

This topic is now closed to further replies.