Page 1 of 1

All in one - EMAIL FORM

Posted: Mon Apr 04, 2011 3:05 pm
by webby8842
Hi,

New to php and i'm working through exercises in a text book. I have finished the script, put it on my server, when I hit the Submit button I receive this message "Error 404: NOT FOUND!Your browser cannot find the document corresponding to the URL you typed in. ". After hitting the submit button, In my address bar it looks like it's trying to find a file named POST. After going through the code I can't figure out why it's looking for POST. (I only have this one file (allinone_form.php) on my server in a folder (phptext), I'm assuming I only need the one file considering it's an "all in one" form. Anyways, here is the code, could someone please help clarify what I have done wrong? It would really help to understand before I move on...


<? //Below I am putting the ENTIRE form into a variable called $form_block

/*When you use $_SERVER[PHP_SELF] as a form action, you're saying, "When
the submit button is clicked, reload this script and do something."
$_SERVER[php_self] has a value of the script's current name (allinone_form.php)

*/
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"$_SERVER[PHP_SELF]\">

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

<p><strong>Your Email Address:</strong><br />
<INPUT type=\"text\" NAME=\"sender_email\" SIZE=30></p>

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

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

</form>";
/*the 3 if statements below just make sure there's something in the appropriate fields*/
if ($_POST[op] != "ds"){
//this then shows the form...
echo "$form_block";
}else if ($_POST[op] == "ds") {
if ($_POST[sender_name] ==""){
$name_err = "<font color=red>You forgot to enter your name!</font><br />";
$send = "no";
}

if($_POST[sender_email] = "" ){
$email_err = "<font color=red>We need your email address dude.</font><br />";
$send = "no";
}

if($_POST[message] = ""){
$message_err = "<font color=red>You forgot to give us a message.</font><br />";
$send = "no";
}

/*This next if statement says it's ok to send*/

if ($send != "no") {

$msg = "Email sent from www site\n";
$msg .= "Sender's Name: $_POST[sender_name]\n";
$msg .= "Sender's Email: $_POST[sender_email]\n";
$msg .= "Message: $_POST[message]\n\n";
$to = "brad@glacierridge.ca";
$subject = "All in one Web site feedback";
$mailheaders = "From: My Web Site <http://www.glacierridge.ca>\n";
$mailheaders .= "Reply to: $_POST[sender_email]\n";

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

/*the next else if sends the error messages if the text fields or message box is empty*/


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

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

/*and then it print's the form again*/

echo "$form_block";
}
}
?>
</body>
</html>

Thanks for your help!

Re: All in one - EMAIL FORM

Posted: Mon Apr 04, 2011 9:18 pm
by higibigi
Hey! All the slashes in your form are kind of foreign to me, but I tried out the form myself and it appears to be working properly.

I'm assuming it's something related to the PHP version you're using or the php.ini setting.

Ehh.. not sure what you could try aside from toggling between PHP versions if your host supports it, or unless your short php tags had something to do with it but I'm assuming you would be getting a different error message.

Re: All in one - EMAIL FORM

Posted: Tue Apr 05, 2011 2:44 pm
by internet-solution
What happens if you delete the following?

Code: Select all

ACTION=\"$_SERVER[PHP_SELF]\"

Re: All in one - EMAIL FORM

Posted: Wed Apr 06, 2011 2:48 pm
by webby8842
higibigi wrote:Hey! All the slashes in your form are kind of foreign to me, but I tried out the form myself and it appears to be working properly.

Hi. Thanks for responding, I really appreciate it. I'm not exactly sure which slashes you're referring to exactly, but I will assume that you're talking about the one's in the form. The textbook I'm using told me to put backslashes before quotation marks when they are within a string. That way the "something" can determine the difference? (i'm slow). Do I no longer have to do that?

In reference to the potential php.ini file issue, I sure hope it's not something to do with that, because then i'll be in trouble. Right now i'm learning out of textbook that is 7 years old and geared toward php use with a windows operating system and old versions of everything. I'm working on a Mac and trying to improvise on the textbooks' suggestions as I go (i'm sure i'm doing something wrong). Any configuration settings that the book suggests, usually don't apply. (?)

Again, I really appreciate the response, (file still not working, lol)

Re: All in one - EMAIL FORM

Posted: Wed Apr 06, 2011 2:55 pm
by webby8842
internet-solution wrote:What happens if you delete the following?

Code: Select all

ACTION=\"$_SERVER[PHP_SELF]\"


Prior to removing the above code, I no longer get the 404 error...the page just reloads without giving me a "sent message" (text fields also clear).

After removing the above code, the same thing happens.

(Thanks for your response as well my friend)

Re: All in one - EMAIL FORM

Posted: Wed Apr 06, 2011 4:31 pm
by higibigi
I know there's easier ways of getting around this such as changing it to action="allinone_form.php" but I guess that kinda defeats the purpose of learning PHP :P

The slashes I was referring to were here: \"$_SERVER[PHP_SELF]\".

Usually I wouldn't have the whole form in a variable and it would look like this.

Code: Select all

hi this is the start of the form in HTML and the ACTION is in PHP!
<form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?>">
Anyways, going to school and having textbooks for programming, I understand at times gotta try going by the book, so my suggestion for getting your code to work (which works for me), is to try toggling your PHP version back and forth maybe.

I worked for a Web Hosting company for a few years and that was a big troubleshooting step for me with PHP sites because someone might be using PHP5 but the coding only works in PHP4 or vise-versa.

Re: All in one - EMAIL FORM

Posted: Wed Apr 06, 2011 5:58 pm
by danwguy
The easiest way to go about sending a form to itself is to use no action... i.e.

Code: Select all

<form method="post" action="">
that will send the form back to itself when the submit button is pushed and all the $_POST data will come along with it. The only time you need to use a \ before a " is if you are doing it in an echo but your form is in html so no need for those slashes, that's why it's looking for something called post because your action is sending it to root...\ or/ ... and that's not what you want. if it were in php you could do

Code: Select all

echo "<form method='post' action=''>";
or
echo "<form method=\"post\" action=\"\">";
note that if you use a double quote " to start the echo you can use a single quote ' without ending the echo statement