Solved

PHP Email form code problem

Posted on 2010-11-19
9
394 Views
Last Modified: 2013-12-13
Heillo all,

Need someone to help/check with this code which I wrote. I`m a begginer at php programmaing, so still learning.
Code was working until I have add field Surname. I`m not sure what could be wrong.
For info, this code is used with flash (swf) file.

<HTML>
<BODY>

<?

$to = "AddHere@yourmail.com";

echo"Thank you for your message.";

//list of all fields
echo"<br>Surname: $_GET[f_surname]\n";
echo"<br>Name: $_GET[f_name]\n";
echo"<br>Phone: $_GET[f_phone]\n";
echo"<br>Email: $_GET[f_email]\n";
echo"<br>Message: $_GET[f_text]\n";

echo"<br>\n";

//send the email: $mailtxt is the content of the email
$mailtxt  ="This text will appear at the top of the email";

// use "\n" to add line breaks
$mailtxt .="\n";

$mailtxt .=" Surname: $_GET[f_surname]\n";
$mailtxt .=" Name: $_GET[f_name]\n";
$mailtxt .=" Phone: $_GET[f_phone]\n";
$mailtxt .=" Email: $_GET[f_email]\n";
$mailtxt .=" Message: \n$_GET[f_text]\n";

$mailtxt .="\nThis text will appear at the bottom of the email\n";

$subject = "Put your subject here";
$headers="From: Your name <" + $to + ">\r\n";

mail($to, $subject, $mailtxt, $headers);

?>
</BODY>
</HTML>
0
Comment
Question by:dejandejanovic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Expert Comment

by:armchang
ID: 34176484
Hi,

Your code above should be working. Can you post more details about the error and the link you browsed?
0
 
LVL 20

Accepted Solution

by:
Mark Brady earned 300 total points
ID: 34176493
The form is coded very wrong. I don't see how this would work at all. For starters, each of the items that are posted to this form when the user fills out the email form, should be captured into a variable. So instead of doing this:

echo"<br>Surname: $_GET[f_surname]\n";
echo"<br>Name: $_GET[f_name]\n";
echo"<br>Phone: $_GET[f_phone]\n";
echo"<br>Email: $_GET[f_email]\n";
echo"<br>Message: $_GET[f_text]\n";

You should be doing this:

$surname = $_POST['f_surname'];

Notice I changed the $_GET to $_POST and added quotes around the ['f_surname']  <- must have quotes (single)

I'm not sure wh you are echo ing anything to the screen in this form. So I can help you, please post the actual email form your are using for the user to fill out their email. I will tidy all this up for you and make it work.

Thanks ~ elvin
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 34176772
Sorry, I should re-phrase my response. Your form will work as it is. In fact, I just tested it and yes it works for me. What I was meaning is that is is good practice especially when you are just learning, to follow standard code writing rules. If you learn the correct way, then as you progress through with your script writing, other coders when trying to help you, will be able to read clearly your code. To answer your question, nothing is wrong with the actual code you posted. Make sure the form that sends the data to this script is correctly done. Here is the sample form I used to test your script.

<html>
<head><TITLE></TITLE></head>
<body>
<form action="testmail.php" method="GET">
First Name<br>
<input type="text" name="f_name"><br><br>
Surname<br>
<input type="text" name="f_surname"><br><br>
Phone<br>
<input type="text" name="f_phone"><br><br>
Email<br>
<input type="text" name="f_email"><br><br>
Message<br>
<textarea name="f_text" cols=50 rows=15></textarea><br><br>

<input type="submit" value="Send">
</form>

</body>
</html>



Just a few things to remember. Try not to use "$_GET" for form submission. That puts all the information in the URL and makes it easy to hijack so use $_POST method for those forms instead.

Also, when dealing with strings in php and javascript, always enclose the string with single quotes '  & '

This makes a clear distinction between a string and a number. For eg, if you are posting a number, it would be like this:

$my_number = $_POST[num];

and a string would be:

$my_string = $_POST['string'];

Also, there are some things in your script that don't make much sense but rather than picking it to pieces, I'll leave off here. I hope I have helped in a small way.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:armchang
armchang earned 200 total points
ID: 34176795
Actually, the things you've missed as what elvin66 is saying is that these lines need to have a single apostrophe inside of the brackets:

$mailtxt .=" Surname: $_GET[f_surname]\n"; --> $mailtxt .=" Surname: $_GET['f_surname']\n";
$mailtxt .=" Name: $_GET[f_name]\n"; --> $mailtxt .=" Name: $_GET['f_name']\n";
$mailtxt .=" Phone: $_GET[f_phone]\n"; --> $mailtxt .=" Phone: $_GET['f_phone']\n";
$mailtxt .=" Email: $_GET[f_email]\n"; --> $mailtxt .=" Email: $_GET['f_email']\n";
$mailtxt .=" Message: \n$_GET[f_text]\n"; --> $mailtxt .=" Message: \n$_GET['f_text']\n";

See if this one works at your end.
0
 

Author Comment

by:dejandejanovic
ID: 34179533
Hm, I have recevied message to my email, but getting like this. Line Name is empty, and result of name is next to surname?!
Any idea?

This text will appear at the top of the email
Surname: DEJANOVICf_name=DEJAN
Name:
Phone: 031344119
Email: DEJAN.DEJANOVIC@YAHOO.COM
Message:
TEST

This text will appear at the bottom of the email

0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 34180156
Yes your form is not written correctly. That's what we are all trying to tell you. I have a suggestion (read my first post). Post your email form here and one of us will rewrite it so it conforms properly, then you will no longer have to worry about it.
0
 

Author Comment

by:dejandejanovic
ID: 34180264
I have found the problem. It was in the actionscript code which was write on Submit button. It was missing this sign & in front of f_suname and f_name.

f_error = "";

url = "mail.php?";
url = url + "&f_surname=" + f_surname;
url = url + "&f_name=" + f_name;
url = url + "&f_phone=" + f_phone;
url = url + "&f_email=" + f_email;
url = url + "&f_text=" + f_text;


if(f_surname=="") f_error = "Enter your surname";
if(f_name=="") f_error = "Enter your name";
if(f_email=="") f_error = "Enter your email";
if(f_email.indexOf("@",0)<0) f_error = "Enter valid email";
if(f_email.indexOf(".",0)<0) f_error = "Enter valid email";

if(f_phone=="") f_error = "Enter your phone";
if(f_text=="") f_error = "Please write the message";


if(f_error=="") getURL(url,"_blank");
0
 

Author Comment

by:dejandejanovic
ID: 34180282
Thank you all for help.
0
 

Author Closing Comment

by:dejandejanovic
ID: 34180286
complete
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question