• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

PHP Email form code problem

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
dejandejanovic
Asked:
dejandejanovic
  • 4
  • 3
  • 2
2 Solutions
 
armchangCommented:
Hi,

Your code above should be working. Can you post more details about the error and the link you browsed?
0
 
Mark BradyPrincipal Data EngineerCommented:
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
 
Mark BradyPrincipal Data EngineerCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
armchangCommented:
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
 
dejandejanovicAuthor Commented:
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
 
Mark BradyPrincipal Data EngineerCommented:
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
 
dejandejanovicAuthor Commented:
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
 
dejandejanovicAuthor Commented:
Thank you all for help.
0
 
dejandejanovicAuthor Commented:
complete
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now