...

View Full Version : Please help with PHP based contact form.



TyeNewton
07-02-2010, 07:04 AM
Hey folks,

I have been working toward filling gaps in my coding knowledge, and I hope to tackle PHP soon, but in the meantime I am stumbling through the creation of a simple PHP based contact form in order to put the finishing touches on a website.

I've taken the code from a super basic (and license free) tutorial and tailored it to my needs. Everything appears to work, except that I do not receive the test email, sent from the form.

The tutorial had me create two .php files. One for the page on the site containing the form, and another for it to communicate with in order to send the email. The first file is actually XHTML (with a touch of JavaScript, which I'm new to), but the tutorial told me to use the .php extension, so I have.

Here is the current code for both files (sans the XHTML):

Contact.php


<form action="ContactSend.php" method="post" enctype="text/plain" name="EmailForm" id="EmailForm">
<input type="text" name="name" id="name" value="Name" width="150px" style="color:#999999;" onFocus="this.value='';this.onfocus=null;this.style.cssText='';" />
<input type="text" name="email" id="email" value="Email" width="150px" style="color:#999999;" onFocus="this.value='';this.onfocus=null;this.style.cssText='';" /><br /><br />
<input type="text" name="subject" id="subject" value="Subject" width="310px" style="color:#999999;" onFocus="this.value='';this.onfocus=null;this.style.cssText='';" /><br /><br />
<textarea name="message" id="message" rows="7" cols="40" style="color:#999999;" onFocus="this.value='';this.onfocus=null;this.style.cssText='';">Type your message here...</textarea>
<br />
<input type="submit" name="send" value="Send">
</form>

and ContactSend.php


<?php
// Contact subject
$subject ="$subject";
// Details
$message="$message";

// Mail of sender
$from="$email";
// From
$header="from: $name <$from>";

// Enter your email address
$to ='my@private.email';

$send=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send){
echo "<b>Thank you</b>.<br />We will get back to you very soon.";
}
else {
echo "<b>ERROR</b>: Please be sure that you complete all fields and try again.";
}
?>

Again, the remaining issue is simply that the test email doesn't arrive in my inbox. I successfully reach the page with the quote: "Thank you. We will get back to you very soon."

Seems like this should be SUCH a simple task. Any help will be so appreciated. : )
Gracious thanks!
-Tye

_Aerospace_Eng_
07-02-2010, 07:08 AM
Who is your webhost and do you know if they allow the use of php's mail() function? Some hosts don't allow it in fear of spam.

TyeNewton
07-02-2010, 07:28 AM
The host is GoDaddy. Considering their high profile, I'd expect they'd cover all the bases, but I guess I don't know... I know there was mention of php support during sign up, but you're just talking about the mail() function, I see.

Do you know where I should look for this information on GoDaddy's site? This is only the third time I've ever purchased hosting, and everything still seems very cluttered and convoluted to me.

Thanks! : )

bennyy007
07-02-2010, 07:50 AM
Create an example PHP file and adding the following inside:



<?php
if ( function_exists( 'mail' ) )
{
echo 'mail() is available';
}
else
{
echo 'mail() has been disabled';
}
?>


This will tell you if PHP mail() function is either enabled or disabled :)

TyeNewton
07-02-2010, 07:57 AM
I was writing this reply as I discovered Benny's, so here it is first...

--

Oh, look!
This super brief article appears to be confirming support for the php mail() function:
http://community.godaddy.com/help/article/951

Looks pretty generalized so it must cover all GoDaddy accounts, right??

--

I suppose I should still try your idea, though, Benny.
Are you suggesting I swap your code with my ContactSend.php file, then use the form to test it?? (Sorry about my lack of php knowledge.)

gh05
07-02-2010, 01:58 PM
I suppose I should still try your idea, though, Benny.
Are you suggesting I swap your code with my ContactSend.php file, then use the form to test it?? (Sorry about my lack of php knowledge.)


No he isn't. Just create a php file with that code (i.e. just go to notepad and rename to .php) and upload to you web server. Then run the script by going to yourwebserver/nameofphpfile.php. The code will output a message in your browser saying whether you are able to use the mail function.

_Aerospace_Eng_
07-02-2010, 02:12 PM
No just create one file and put nothing else but Benny's code in it, upload it to your site, and navigate to the file. What does it tell you?

mlseim
07-02-2010, 02:16 PM
At one time, GoDaddy only allowed PHP mail() to be sent to the email
that was assigned to the account (in your GoDaddy settings).

Not sure if they changed that or not, but you might want to check their documentation.

_Aerospace_Eng_
07-02-2010, 03:02 PM
Also turn on error reporting to see if any errors are being returned. Change this

<?php
// Contact subject
$subject ="$subject";

to this

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
// Contact subject
$subject ="$subject";

tomws
07-02-2010, 04:29 PM
Does GoDaddy force the use of their gdform.php form mailer? Or is that just a dummies version of form mailing? I know there have been several questions on that here. A forum search might turn up something.

TyeNewton
07-02-2010, 06:46 PM
Thank you, everyone, so much for the assistance!

Good news:
I tried Benny's test for mail() function support and it turned up positive.

Interesting news:
I also tried Aerospace's error-checking suggestion and received the following report...

"Notice: Undefined variable: subject in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 5

Notice: Undefined variable: message in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 7

Notice: Undefined variable: email in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 10

Notice: Undefined variable: name in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 12"

Looking back at my code, the lines it refers to are $subject ="$subject";, $message="$message";, $from="$email";, and $header="from: $name <$from>";. Basically every single variable received from the form.
Does this mean that it is in fact NOT receiving information from the form? If so, I must be reaching the "Thank you." message in error, correct?
On the other hand, if it IS receiving information from the form, then I can understand why it wouldn't like my "subject" and "message" variables, as those appear to refer to themselves, but I'm at a total loss as to why it doesn't like my "from" and "header" variables. : /

This is actually a website for my own father's business, so in the meantime, I am going to attempt to test out miseim's hypothesis by changing the "to" address to my father's email (the one associated with the GoDaddy account).

Thanks so much, to everyone, again!

TyeNewton
07-02-2010, 07:02 PM
I forgot to also add that I don't fully grasp what tomws is asking about. I wouldn't be surprised if what I'm using is a "dummies" version, however. ; )

Okay, so nothing was received by my father's email, either. However, I'm wondering now if mlseim (oops! I misspelled your username before) meant that the address has to be associated with the domain? My father still prefers to use his ISP email address, so we didn't bother purchasing email support with the domain.
I'll poke around at GoDaddy's documentation for an answer, in any case.

(Is it weird that I'm referring to everyone in the third person? I'm do more search than posting on forums, so I apologize if my posting habits appear rude. Also, should I be quoting people more?)

tomws
07-02-2010, 07:28 PM
Re: undefined variable. You're not "catching" the values from the form. The assignments you're using worked in PHP4, but not in PHP5 (unless the server operator has opened up a little security hole). Use this instead:

$myVar = $_POST['myVar'];
// or
$myVar = $_GET['myVar'];

TyeNewton
07-02-2010, 08:01 PM
Thanks tomws.

So I've turned this:


<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
// Contact subject
$subject ="$subject";
// Details
$message="$message";

// Mail of sender
$from="$email";
// From
$header="from: $name <$from>";

// Enter your email address
$to ='my@private.add';

$send=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send){
echo "<b>Thank you</b>.<br />We will get back to you very soon.";
}
else {
echo "<b>ERROR</b>: Please be sure that you complete all fields and try again.";
}
?>

Into this:


<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
// Contact subject
$subject = $_Get[ '$subject' ];
// Details
$message = $_Get[ '$message' ];

// Mail of sender
$from = $_Get[ '$email' ];
// From
$header = $_Get[ 'from: $name <$from>' ];

// Enter your email address
$to = $_Post[ 'my@private.add' ];

$send=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send){
echo "<b>Thank you</b>.<br />We will get back to you very soon.";
}
else {
echo "<b>ERROR</b>: Please be sure that you complete all fields and try again.";
}
?>

I'm not sure if I needed the $_POST for the "to" address, or if this is even correct at all.(?) Seems like there must be something more I need to do to the $send and $header variables.

TyeNewton
07-02-2010, 08:23 PM
Wuh-oh,
This time all of the same variables plus line 15 (the "to" address) turned up undefined.
I guess I didn't translate that properly, haha.

I apologize again for tackling this with so little prior php knowledge. Again, I intend to properly learn javascript and php after I finish with the first installment of this and another website.

tomws
07-02-2010, 08:52 PM
If your form method is "post", then you need to use POST instead of GET. Furthermore, you only use one of those to assign variable that are coming from the form. Therefore, something like this makes no sense:

$to = $_Post[ 'my@private.add' ];

Also, I didn't test whether PHP handles this for the GET/POST globals, but for normal variables $ThisName is not the same as $thisname. The GET/POST globals are all caps.

TyeNewton
07-02-2010, 09:26 PM
Ahhh, I see! I was translating "GET/POST" as "Receive/Send."

In that case, do GET and POST literally perform the same interchangeable function? (Is it appropriate for me to be using POST in this case?) Also, I guess this means POST needs to be all caps in the form, as well? (i.e. method="POST" instead of method="post"?)

Trying this out now... : )

TyeNewton
07-02-2010, 09:36 PM
Okay, this is what my code looks like now:


<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
// Contact subject
$subject = $_GET[ '$subject' ];
// Details
$message = $_GET[ '$message' ];

// Mail of sender
$from = $_GET[ '$email' ];
// From
$header = $_GET[ 'from: $name <$from>' ];

// Enter your email address
$to ='my@private.add';

$send=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send){
echo "<b>Thank you</b>.<br />We will get back to you very soon.";
}
else {
echo "<b>ERROR</b>: Please be sure that you complete all fields and try again.";
}
?>

And the report still looks like this:

Notice: Undefined index: $subject in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 5

Notice: Undefined index: $message in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 7

Notice: Undefined index: $email in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 10

Notice: Undefined index: from: $name <$from> in /home/content/m/t/n/mtnfinishing/html/ContactSend.php on line 12

I sure hope I'm not just being dense about something; very sorry if so.
I'm actually about to be late for my band's rehearsal right now, so I'll check back late tonight. Thanks so much for your patience!

Keleth
07-02-2010, 09:59 PM
Ahhh, I see! I was translating "GET/POST" as "Receive/Send."

In that case, do GET and POST literally perform the same interchangeable function? (Is it appropriate for me to be using POST in this case?) Also, I guess this means POST needs to be all caps in the form, as well? (i.e. method="POST" instead of method="post"?)

Trying this out now... : )

GET is variables passed along with the URL. POST is not. There are places where each is appropriate. I've never found passing a form via GET as appropriate, but there may be reasons why you would.

When accessing the variable, yes, _POST, but as a method, its not necessary to capitalize. At the browser end, it doesn't care about variable cap sensitivity.

_Aerospace_Eng_
07-02-2010, 10:54 PM
Sighs. If your form method is post then use $_POST, if it is get then use $_GET. It doesn't have to be capitalized. If this is for a client I think it might be time to hire a professional.

TyeNewton
08-10-2010, 12:37 AM
Hey folks,

I left a reminder for myself to tie things on in this thread, but it got lost, and I just remembered. I just want to thank everyone again for your help and let you know what happened:

I eventually discovered that my form had be functioning properly the whole time, and it was just GoDaddy's silly "gdform" method (which I think someone mentioned) that was screwing me up. When I eventually realized I needed to use their "gdform.php" on their system, it still took me a long time to figure out that the form wouldn't send unless I gave it a real, functioning email address (and NOT the same one it's set to send TO). This all turned out to be a huge waste of time for me, and I really appreciate everyone else's patience in trying to help.

(However, I'm glad I didn't hire anyone to figure this out for me, Aerospace... that would have been rather silly.)

_Aerospace_Eng_
08-10-2010, 02:23 AM
(However, I'm glad I didn't hire anyone to figure this out for me, Aerospace... that would have been rather silly.)

But it would have saved us all a headache as a pro would have finished in half an hour. Glad you got it fixed though.

TyeNewton
08-10-2010, 07:20 AM
Sighs. If your form method is post then use $_POST, if it is get then use $_GET. It doesn't have to be capitalized. If this is for a client I think it might be time to hire a professional.


But it would have saved us all a headache as a pro would have finished in half an hour. Glad you got it fixed though.

I do sincerely apologize for any headaches, and agree with your point that it's best to pay a professional coder if I'm completely stuck in the future. (Although it becomes hard for me to draw the line between what is fair and unfair to ask in these forums. Have I been a "help vampire?" Haha ; p )

The website was for my father's company, and I have been paid some for the job, but it was mostly to learn and build work for my portfolio. I did bring up the idea with him of hiring someone, and he had someone he knew in mind, but before that panned out the problem was resolved.

Thanks again, Aerospace and all. : )



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum