PHP Problem - form submitting wont work?

  gplatt2000 16:07 10 Aug 2004
Locked

Hi all. I am gradually trying to learn PHP, and currently working my way through a tutorial - click here .

I have copied + pasted the code at the bottom half of the page to create a form and PHP processing page.

I have Apache and PHP installed, and all te other (simple) php files I ahve made, have worked fine when I test then (on htp://localhost/...' - these are only simple things such as echo, if, etc...

However, when I test the form, I fill in the feilds and click submit. But it doesnt work! The 'else' part of the PHP processing file is executed, and it dosplays the error message.

Does anyone know why this could be? Can you see any errors in the script? Or something wrong with my test servre (i.e. how Ive installed apache/php on my computer)? Perhaps if you have a php-enabled server you could test it on there for me?

Thanks a lot for any help

  gplatt2000 16:08 10 Aug 2004

here is the code for the form:

<html>
<body>

<form action="mail.php" method="post">

Your Name:
<input type="text" name="name"><br>

E-mail:
<input type="text" name = "email"><br><br>

Comments<br>
<textarea name="comments"></textarea><br><br>

<input type="submit" value="Submit">

</form>

</body>
</html>


----------------------

And for the php processing page (saved as 'mail.php'):

<?
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
$to="[email protected]";
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email";
if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>

--------------------------------

Thanks a lot in advance

  Matt45 22:43 10 Aug 2004

It seems to work fine on my server. I can't see anything wrong with the code and when I copied and pasted the code exactly as your have it to my server, it worked straight away.

click here to take a look

Matt

  Talented Monkey 23:11 10 Aug 2004

worked for me too. Strange. Which version of PHP are you using? try the following and see if it works. i prefer using the following method for checking as i have more control over what can be checked.

this is just a simple version. for example you can check to make sure the email address includes an @ sign etc, create a variable to store the check result, then simply include them in the if statement, if all the if statements are true then send the mail.

heres the code.

if(!$email) || (!$comments) {

echo "There was a problem sending the mail. Please check that you filled in the form correctly.";

}else{


mail($to,"Comments From Your Site",$message,"From: $email\n");
echo "thanks for your comments.";

}

  Taran 23:57 11 Aug 2004

I think that the problem here is very probably likely to be that the local testing server is not set up to process email. Setting a local server to handle emails is not a small subject but without proper configuration a bare bones, plain vanilla Apache installation on your own PC will not process most email handlers.

If you have a friend with a web account on an Apache server why not upload a test page to it with your PHP form handler to try it on a 'live' server ? Better yet, why not invest in one of your own, so that you can play properly.

I only suggest this because your own local testing server is fine for many things but unless you really get into the server management side of things, it will never fully emulate a commercial web server.

Finally, if you do go for a proper Apache web hosted account, be sure to check which version of Apache, PHP and whatnot are installed. Some of the less expensive options are running old versions of Apache and PHP, and this will hamper you since many of the current functions will be unavailable or will not work, either partially or at all.

  gplatt2000 12:13 12 Aug 2004

Talented Monkey - I tried your suggestion, but still it didnt work :( But thanks anyway (by the way I am running PHP

I'm going to be getting a host failry soon anyway, so may aswell get one sooner rather than later, in the meantime I'll do what I can without it. I'm thinking of using click here - any opinions on them?

Thanks again everyone

  gplatt2000 12:25 12 Aug 2004

Sorry in my last post I went to find out mid-sentance what version of pHP I had, but then forgot to finish the sentance lol, so it's version 4.3.6

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

AMD Radeon Adrenalin release date, new features, compatible graphics cards

Print designer Kelly Anna on confident mark making & modern femininity

New iMac Pro release date, UK price & specs rumours

Comment créer, utiliser et supprimer son compte Facebook ?