Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Beginner 1

Status
Not open for further replies.

ha1ls45

Programmer
Feb 20, 2005
105
US
Hi everyone,
I need to create a simple email form and this is my first attempt to write php. Anyhow, I found this code on the internet:
Code:
<html>
<head><title></title></head>
<body>

<?php
if(!isset($_POST[\'submit\'])) {
   // Form code
?>

<form method=\"post\" action=\"email.php\">
   Name:     <input type=\"text\" name=\"name\"><br>
   Email:    <input type=\"text\" name=\"email\"><br>
   Subject:  <input type=\"text\" name=\"subject\"><br>
   Message:<br>
      <textarea cols=10 rows=30 name=\"message\"></textarea><br>
   <input type=\"submit\" name=\"submit\" value=\"Send\">
</form>

<?php
} else {
   // Processing code
   $headers = \"From: \".$_POST[\'name\'].\" <\".$_POST[\'email\'].\">\\r\\n\";
             .\"Reply-To: \".$_POST[\'name\'].\" <\".$_POST[\'email\'].\">\\r\\n\";
   mail(\"", $_POST[\'subject\'], $message, $headers);
}
?>
</body>
</html>

Anyhow, I have tried to put this up on the server just to see what it looked like and when you open the email.php page, a dialog box pops up asking if I'd like to open the file or save it. Yes, my first step is to even get a php page to display on the net without this dialog box popping up. What am I doing wrong?

Thanks!
Ha1ls45
 
Your server is not configured to parse php code. If it is your server, look at which one is it and then post the question in the respective server forum. If it is hosted, I guess you will have to look for a new host.
 
First step, does the server you uploaded the code to understand PHP files?

Create a php file, named info.php containing the following lines:
Code:
<?
phpinfo();
?>
Upload this file to your server and type the URL
Does it show PHP information formated nicely on the screen? If it does, the server understands PHP, if not talk to your hosting company.

As for the code you found on the internet ... It won't work as coded at all. Here is the code, with at least the obvious errors corrected:
Code:
<html>
<head><title></title></head>
<body>

<?php
if(!isset($_POST['submit'])) { // incorrect usage of backslash
   // Form code
?>

<form method="post" action="email.php">
   Name:     <input type="text" name="name"><br>
   Email:    <input type="text" name="email"><br>
   Subject:  <input type="text" name="subject"><br>
   Message:<br>
      <textarea cols=10 rows=30 name="message"></textarea><br>
   <input type="submit" name="submit" value="Send">
</form>

<?php
} else {
   // Processing code
   $headers = "From: ".$_POST['name'].\" <".$_POST['email'].">\r\n\";
             ."Reply-To: ".$_POST['name']." <".$_POST['email'].">\r\n\";
   mail("", $_POST['subject'], $message, $headers);
}
?>
</body>
</html>

Whomever posted the original code, forgot to strip the slashes from the inputted text before sending it to the screen.

Also, the mail() function needs a value in the first parameter. That is the "To:" address.

Please read the manual (or at least skim it) at
Ken
 
Okay, I used this code:

Code:
<html>
<head><title></title></head>
<body>

<?php
if(!isset($_POST['submit'])) { // incorrect usage of backslash
   // Form code
?>

<form method="post" action="email.php">
   Name:     <input type="text" name="name"><br>
   Email:    <input type="text" name="email"><br>
   Subject:  <input type="text" name="subject"><br>
   Message:<br>
      <textarea cols=10 rows=30 name="message"></textarea><br>
   <input type="submit" name="submit" value="Send">
</form>

<?php
} else {
   // Processing code
   $headers = "From: ".$_POST['name'].\" <".$_POST['email'].">\r\n\";
             ."Reply-To: ".$_POST['name']." <".$_POST['email'].">\r\n\";
   mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);
}
?>
</body>
</html>

and I am getting these errors:

Code:
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /files/home/4/hmw364/php/email.php on line 22

Parse error: parse error, unexpected T_STRING in /files/home/4/hmw364/php/email.php on line 23

I am getting closer. I was able to get the info.php page to work...

Does anyone know what my parse error is?

Ha1ls45

 
You still have one too many backslash characters:
Code:
   $headers = "From: ".$_POST['name'].[COLOR=red]\[/color]" <".$_POST['email'].">\r\n\";
             ."Reply-To: ".$_POST['name']." <".$_POST['email'].">\r\n\";
   mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);
should be:
Code:
   $headers = "From: ".$_POST['name']." <".$_POST['email'].">\r\n\";
             ."Reply-To: ".$_POST['name']." <".$_POST['email'].">\r\n\";
   mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);
The second error is caused by the first.

Ken
 
Wow, you are quick! Thanks for helping out!

I changed it and am still getting the last error:

Code:
Parse error: parse error, unexpected T_STRING in /files/home/4/hmw364/php/email.php on line 23

Ha1ls45
 
This line:
Code:
$headers = "From: ".$_POST['name']." <".$_POST['email'].">\r\n\";
is terminated (semi-colon at the end) and means that it will be parsed ok. However, next line begins with [tt]."Reply-To: "...[/tt] which is actually a string concateator and some string, but since you're not assigning that to anything (remember, last line was parsed already), PHP chokes up. Try with this:
Code:
   $headers = "From: ".$_POST['name']." <".$_POST['email'].">\r\n\"
             ."Reply-To: ".$_POST['name']." <".$_POST['email'].">\r\n\";
   mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);
 
Still same error, so my email.php file looks like this now:

Code:
<html>
<head><title></title></head>
<body>

<?php
if(!isset($_POST['submit'])) { // incorrect usage of backslash
   // Form code
?>

<form method="post" action="email.php">
   Name:     <input type="text" name="name"><br>
   Email:    <input type="text" name="email"><br>
   Subject:  <input type="text" name="subject"><br>
   Message:<br>
      <textarea cols=10 rows=30 name="message"></textarea><br>
   <input type="submit" name="submit" value="Send">
</form>

<?php
} else {
   // Processing code
   $headers = "From: ".$_POST['name']." <".$_POST['email'].">\r\n\"
             ."Reply-To: ".$_POST['name']." <".$_POST['email'].">\r\n\";
   mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);

}
?>
</body>
</html>
 
Try this:
Code:
   $headers = "From: " . $_POST['name'] . " <" . $_POST['email'] . ">\r\n\";
   $headers .= "Reply-To: " . $_POST['name'] . " <" . $_POST['email'] . ">\r\n\";
   mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);
 
Oh wow, sorry, it's Sunday and my eyes are not so good. Remove the backslash before the double quote:
Code:
   $headers = "From: " . $_POST['name'] . " <" . $_POST['email'] . ">\r\n";
   $headers .= "Reply-To: " . $_POST['name'] . " <" . $_POST['email'] . ">\r\n";
   mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);
 
Okay!!! That worked. Now, I just filled out the form and it successfully sent the email to me; however, it was missing the message body of the email.

I tried to change this:

Code:
mail("ha1ls45@myemail.net", $_POST['subject'], $message, $headers);

To this:

Code:
mail("ha1ls45@myemail.net", $_POST['subject'], $message['message'], $headers);

and that didn't work. Is this something you easily know how to fix as well?

I also think it would be awesome if after they hit the submit button, it would say something like this:

"Thank you, [sender name] for the email! Your submission has been excepted." Is this easy too?

Yes, I understand the Sunday thing..I am super wiped out from the week!

Thanks for all of your help Vragabond!

Ha1ls45
 
You got the subject by referring to $_POST['subject']. That meant that variables you sent with your form appear in the $_POST superglobal array and you can referrence them simply through that. It is very easy to do it, you only have to understand php a little better:
Code:
mail("ha1ls45@myemail.net", $_POST['subject'], $$_POST['message'], $headers);
echo 'Thank you, ' . $_POST['name'] . ' for the email!  Your submission has been excepted.';
 
I had to take out one of the $ signs in this part:

Code:
$$_POST['message']

Then, it all worked perfectly!!!! I am actually proud that I figured it out that I had to take the $ sign out.. haha. Anyhow, yes, I start my PHP class in 2 weeks. Unfortunately, you always need it before you start class. Thank you so much for helping me out Vragabond! I really appreciate it!

Ha1ls45
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top