-2

I have the following form and php script:

<form method="post" action="index.php">
    <h1>Contact</h1> 

    <label>Name</label>
    <input name="name" placeholder="Type Here">

    <label>Email</label>
    <input name="email" type="email" placeholder="Type Here">

    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>

    <input id="submit" class="button" name="submit" type="submit" value="Submit">
</form>

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'SJW&T Contact Form'; 
    $to = '[email protected]'; 
    $subject = 'Hello';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {              
        if (mail ($to, $subject, $body, $from)) { 
            echo '<p>Your message has been sent!</p>';
        } else { 
            echo '<p>Something went wrong, go back and try again!</p>';
        } 
    }
?>

But my web host seems to be returning an error 404. I tried the suggestion in this thread, which is a near duplicate, but it did not work. Anyone see any syntax errors? My web host is 000webhost.com. It doesn't give me an actual 404 message, but the URL it takes me to is error404.000webhost.com.

9
  • 8
    A syntax error would not cause a 404 error. Something else must be the problem. Commented Jul 6, 2014 at 20:15
  • 1
    Maby you could try the PHPSELF as action ,html-form-guide.com/php-form/php-form-action-self.html Commented Jul 6, 2014 at 20:16
  • 1
    @ByronS So you see the form? If so, change the filename to index.php instead of index.html. That way your action is valid and the php will be processed. Commented Jul 6, 2014 at 20:20
  • 1
    Crappy tutorial by the way, you should move your assignments to inside the if statement and use isset() or empty() to get rid of the warnings. Commented Jul 6, 2014 at 20:23
  • 1
    Expanding on @jeroen's comment, if a file has the html extension, the web server will not parse any PHP, as it interprets the page as an HTML file. Changing the extension to php will tell the web server "hey, I'm a PHP file. Parse anything between <?php and ?> as PHP". Commented Jul 6, 2014 at 20:24

3 Answers 3

0

That might happen, when you don't have the action page:

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

You set the action page to index.php, so if that file doesn't exist or located somewhere else, you might get a 404 error or Object not found.

Make sure you have index.php AND it exists in the same directory with your other files.

If you want to take the action on the page itself, then do:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Sign up to request clarification or add additional context in comments.

2 Comments

The tutorial I am following asked me to place the php script directly under the form so that it's output would appear there. Do I have to make another file as well?
Well, of course, you set the action page to index.php, if you wanted the action to the page itself then you would do this: <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
0

Make sure your form action name is the same thing as the file name. E.g form action-"forms.php". make sure the folder or file name is also-forms.php 😉

Comments

0

Try this:

< print >

  • $body = "From: $name\n E-Mail: $email\n Message:\n $message";

I think this will solve ur problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.