Web Design Forum: PHP Contact Form - Error 404 Object Not Found - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP Contact Form - Error 404 Object Not Found Rate Topic: -----

#1 User is offline   woodsytime 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 21-January 10
  • Reputation: 0

Posted 16 March 2010 - 11:10 AM

Hi there,

I've created a contact form which relies on a piece of php (mailer.php) to send through the data to a specified email address. When I submit the form I get an Error 404 Object Not Found page attached here:

Attached File  error404.jpg (58.28K)
Number of downloads: 6

Not sure if it helps but I'm using Xampp as my virtual server.

Here's the code for the contact form:

<form action="mailer.php" method="POST" >
        Fields marked (*) are required
        
        <p>FirstName:* <br>
        <input type="text" name="FirstName">
        <p>LastName:* <br>
        <input type="text" name="LastName">
        <p>City:* <br>
        <input type="text" name="City">
        <p>Tel:<br>
        <input type="text" name="Tel">
        <p><input type="submit" name="submit" value="Submit">
        </form>
        <p>



Here's the mailer.php code:

<?php

// get posted data into local variables
$EmailFrom = "Enquiry";
$EmailTo = "myname@myemail.com";
$Subject = "Enquiry";
$FirstName = Trim(stripslashes($_POST['FirstName'])); 
$LastName = Trim(stripslashes($_POST['LastName'])); 
$City = Trim(stripslashes($_POST['City'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 

// validation
$validationOK=true;
if (Trim($FirstName)=="") $validationOK=false;
if (Trim($LastName)=="") $validationOK=false;
if (Trim($City)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "FirstName: ";
$Body .= $FirstName;
$Body .= "\n";
$Body .= "LastName: ";
$Body .= $LastName;
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>



Server Side scripting and PHP etc. is all pretty new to me. However, one thing which did occur to me is do I need an 'ok.htm' page sitting somewhere on my virtual server to be displayed as a 'Success' page?

Any help greatly appreciated.

Cheers
0

#2 User is offline   Wickham 

  • Web Guru
  • View gallery
  • Group: Moderators
  • Posts: 2,875
  • Joined: 11-June 09
  • Reputation: 257
  • Gender:Male
  • Location:Salisbury UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 16 March 2010 - 11:27 AM

The 404 error might refer to $EmailFrom = "Enquiry"; because the mail code
mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


has $EmailFrom but Enquiry isn't a field name in the form. It needs to be an email address, not text Enquiry.

The error also says localhost, so are you trying to send the email from localhost on your computer? It's better to upload everything and test online.
1

#3 User is offline   woodsytime 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 21-January 10
  • Reputation: 0

Posted 16 March 2010 - 11:49 AM

I tried putting in an email address in the $EmailFrom, but this didn't work. Does this email address need to be verified or something??? I still don't really understand?

But then, the only reason $EmailFrom is there is so that the receiving email address can see who sent it, so its not integral to get the php code working is it? When the $EmailFrom is called in this line of code
mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
its just used as a text string isn't it? So does it necessarily have to be an email address?

I also commented out the $EmailFrom part but that didn't help either.

With regards to localhost that's correct, I'm testing on my computer using Xampp as don't want to put it live yet. Is this the problem? Do I need to put it live to get it working?

Still all new to me so apologies for my ignorance in these matters!
0

#4 User is offline   TopShopper 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 878
  • Joined: 21-July 09
  • Reputation: 14
  • Gender:Male
  • Location:Cardiff, UK
  • Experience:Intermediate
  • Area of Expertise:Coder

Posted 16 March 2010 - 12:13 PM

View Postwoodsytime, on 16 March 2010 - 11:10 AM, said:

Server Side scripting and PHP etc. is all pretty new to me. However, one thing which did occur to me is do I need an 'ok.htm' page sitting somewhere on my virtual server to be displayed as a 'Success' page?

Any help greatly appreciated.

Cheers


Yes you need ok.htm and error.htm in same directory as mailer.php
1

#5 User is offline   woodsytime 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 21-January 10
  • Reputation: 0

Posted 16 March 2010 - 12:32 PM

Thanks for confirming that...

So I've now got 'ok.htm' and 'error.htm' in the root directory where the 'mailer.php' file is.

Next step is to get it to actually send the email! I submitted the form and the 'error.htm' was displayed which I just created. Do I need to upload the site to the web to get it send the email to the stated address?
0

#6 User is offline   Wickham 

  • Web Guru
  • View gallery
  • Group: Moderators
  • Posts: 2,875
  • Joined: 11-June 09
  • Reputation: 257
  • Gender:Male
  • Location:Salisbury UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 16 March 2010 - 02:57 PM

View Postwoodsytime, on 16 March 2010 - 11:49 AM, said:

I tried putting in an email address in the $EmailFrom, but this didn't work. Does this email address need to be verified or something??? I still don't really understand?

But then, the only reason $EmailFrom is there is so that the receiving email address can see who sent it, so its not integral to get the php code working is it? When the $EmailFrom is called in this line of code
mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
its just used as a text string isn't it? So does it necessarily have to be an email address?

I also commented out the $EmailFrom part but that didn't help either.

With regards to localhost that's correct, I'm testing on my computer using Xampp as don't want to put it live yet. Is this the problem? Do I need to put it live to get it working?


An email has four sections, the To, Subject, Message body and From so the PHP mail function has to have four sections separated by commas, which your code does have, but an email cannot be sent unless it has a From email address; it will just be rejected by the ISP.

I think it's difficult to send emails from your computer localhost; it's better to test with a real form and a real email online (you could use the same email address for $EmailTo and $EmailFrom or use another of your own email addresses).
1

#7 User is offline   woodsytime 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 64
  • Joined: 21-January 10
  • Reputation: 0

Posted 16 March 2010 - 03:30 PM

Thanks for your explanation. I included an email address in the $EmailFrom part and just tried a live test on the web and it worked!!!

Thanks for your help!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

4 User(s) are reading this topic
0 members, 4 guests, 0 anonymous users