All-In-One email form

Submit a simple email form from one php file

Page 1 of 1

2 Replies - 2657 Views - Last Post: 20 April 2008 - 07:07 AM Rate Topic: -----

#1 parenteau  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 12-February 08

All-In-One email form

Posted 19 April 2008 - 06:18 PM

Ok, so I have this book... it is eight years old and I am trying to learn php from it. It is missing all kinds of code!!!
I put a request to see what was missing when I tried to do a simple calculator. The one bit of code that had to do with gathering the data from the html file:
$variable = $_POST["variable"];
...has taken me pretty far in the book... however, noow that the book is getting a bit deeper... I am starting to get a bit lost. Here is a simple All-In-One email form that is supposed to print errors if the fields aren't filled in and it is also supposed to send... without going to another php script outside of this file. Here is the code.. I know it must be missing something!

If any body could help nudge me along again so I can get through another couple of chapters.. that would be great!
Thanks

<HTML>
<HEAD>
<TITLE>All-In-One Feedback Form</TITLE>
</HEAD>
<BODY>

<?

$form_block = "

<FORM METHOD=\"post\" ACTION=\"$PHP_SELF\">

<P><strong>Your Name:</strong><br>
<INPUT type=\"text\" NAME=\"sender_name\" VALUE=\"$sender_name\" SIZE=30></p>

<P><strong>Your E-Mail Address:</strong><br>
<INPUT type=\"text\" NAME=\"sender_email\"  VALUE=\"$sender_email\" SIZE=30></p>

<P><strong>Message:</strong><br>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual>$message</TEXTAREA></p>

<INPUT type=\"hidden\" name=\"op\" value=\"ds\">

<P><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send This Form\"></p>

</FORM>

";

if ($op != "ds") {

	// they need to see the form
	echo "$form_block";
	
} else if ($op == "ds") {

	if ($sender_name == "") {
		$name_err = "<font color=red>Please enter your name!</font><br>";
		$send = "no";
	} 
	
	if ($sender_email == "") {
		$email_err = "<font color=red>Please enter your e-mail address!</font><br>";
		$send = "no";
	} 
	
	if ($message == "") {
		$message_err = "<font color=red>Please enter a message!</font><br>";
		$send = "no";
	} 
	
	if ($send != "no") {
	
		// it's ok to send!
		$msg = "E-MAIL SENT FROM WWW SITE\n";
		$msg .= "Sender's Name:	$sender_name\n";
		$msg .= "Sender's E-Mail:  $sender_email\n";
		$msg .= "Message:		  $message\n\n";
	
		$to = "myemail@email.com";
		$subject = "All-in-One Web Site Feedback";
		$mailheaders = "From: My Web Site <> \n";
		$mailheaders .= "Reply-To: $sender_email\n\n";

		mail($to, $subject, $msg, $mailheaders);
		echo "<P>Mail has been sent!</p>";
		
	} else if ($send == "no") {
	
		echo "$name_err";
		echo "$email_err";
		echo "$message_err";
		echo "$form_block";  
			
	} 
	
}

?>

</BODY>
</HTML>


Is This A Good Question/Topic? 0
  • +

Replies To: All-In-One email form

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: All-In-One email form

Posted 19 April 2008 - 07:57 PM

Ok the main problem here is that the old code doesn't know about the change to super global arrays.

For instance, instead of $PHP_SELF you use $_SERVER["PHP_SELF"] because PHP_SELF is now part of a super global array called $_SERVER which is an array that contains info about the server. You can loop through this array too if you like to see what is in it. This could be the only thing stopping your script right now but I will continue...

Your form also submits by the method of POST so when this script receives the data from the submission, it is going to be stored in another super global array called $_POST where each entry in the array corresponds to the name of the field. Now if you have the PHP option "register globals" set then you will create variables of each field name, but you should always use $_POST when you can.

Thus before you can test $op you must go $op = $_POST["op"];.

I believe the only thing stopping you though is the $_SERVER["PHP_SELF"] part. Without it the form will not be submitting the data to itself and thus not mailing. Also make sure that you are checking that each of your if statements are indeed turning out true and that it eventually reaches your mail function.

This is what I created that seemed to work fine for me...

<HTML>
<HEAD>
<TITLE>All-In-One Feedback Form</TITLE>
</HEAD>
<BODY>

<?php

$form_block = "

<FORM METHOD=\"post\" ACTION=\"{$_SERVER['$PHP_SELF']}\">

<P><strong>Your Name:</strong><br>
<INPUT type=\"text\" NAME=\"sender_name\" VALUE=\"$sender_name\" SIZE=30></p>

<P><strong>Your E-Mail Address:</strong><br>
<INPUT type=\"text\" NAME=\"sender_email\"  VALUE=\"$sender_email\" SIZE=30></p>

<P><strong>Message:</strong><br>
<TEXTAREA NAME=\"message\" COLS=30 ROWS=5 WRAP=virtual>$message</TEXTAREA></p>

<INPUT type=\"hidden\" name=\"op\" value=\"ds\">

<P><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send This Form\"></p>

</FORM>

";

if ($_POST["submit"]) {
	$sender_name = $_POST["sender_name"];
	$sender_email = $_POST["sender_email"];
	$message = $_POST["message"];
	$op = $_POST["op"];
}

if ($op != "ds") {

	// they need to see the form
	echo "$form_block";

} else if ($op == "ds") {

	if ($sender_name == "") {
		$name_err = "<font color=red>Please enter your name!</font><br>";
		$send = "no";
	}

	if ($sender_email == "") {
		$email_err = "<font color=red>Please enter your e-mail address!</font><br>";
		$send = "no";
	}

	if ($message == "") {
		$message_err = "<font color=red>Please enter a message!</font><br>";
		$send = "no";
	}

	if ($send != "no") {

		// it's ok to send!
		$msg = "E-MAIL SENT FROM WWW SITE\n";
		$msg .= "Sender's Name:	$sender_name\n";
		$msg .= "Sender's E-Mail:  $sender_email\n";
		$msg .= "Message:		  $message\n\n";

		$to = "myemail@email.com";
		$subject = "All-in-One Web Site Feedback";
		$mailheaders = "From: My Web Site <> \n";
		$mailheaders .= "Reply-To: $sender_email\n\n";

		mail($to, $subject, $msg, $mailheaders);
		echo "<P>Mail has been sent!</p>";

	} else if ($send == "no") {

		echo "$name_err";
		echo "$email_err";
		echo "$message_err";
		echo "$form_block";  

	}

}


?>

</BODY>
</HTML>



Hope that helps you out. :)

This post has been edited by Martyr2: 19 April 2008 - 08:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 parenteau  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 12-February 08

Re: All-In-One email form

Posted 20 April 2008 - 07:07 AM

Thank You!
Now the error msg's are working...
I have not received the email I sent from the form though... itr does say that the message has been sent... but I don't know. Did you submit a form to yourself to test?
Just trying to understand this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1