Solved

PHP Email form code problem

Posted on 2010-11-19
9
386 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
  • 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now