azhpo azhpo - 3 months ago 42
PHP Question

Cannot POST /contact.php

I have a contact form on my website and I get the "Cannot POST /contact.php" error.

This is my form at html page:

<form role="form" action="contact.php" method="post">
<div class="form-group">
<label for="InputName">Nom</label>
<div class="input-group">
<input type="text" class="form-control" name="name" id="InputName" placeholder="Nom" required>
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>
</div>
<div class="form-group">
<label for="InputEmail">Email</label>
<div class="input-group">
<input type="email" class="form-control" id="InputEmail" name="email" placeholder="Email" required>
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>
</div>
<div class="form-group">
<label for="InputEmail">Sujet</label>
<div class="input-group">
<input type="text" class="form-control" id="InputEmail" name="subject" placeholder="Sujet" required>
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>
</div>
<div class="form-group">
<label for="InputMessage">Message</label>
<div class="input-group">
<textarea name="message" id="InputMessage" class="form-control" rows="5" required></textarea>
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>

</div>

<div class="form-group">
<label for="InputReal">5 + 10? (Spam Checker)</label>
<div class="input-group">
<input type="text" name="human" class="form-control" name="InputReal" id="InputReal" required>
<span class="input-group-addon"><i class="glyphicon glyphicon-ok form-control-feedback"></i></span>
</div>
</div>

<input type="submit" name="submit" id="submit" value="Submit" class="btn btn-info pull-left">
</form>


And then I tried my PHP code (contact.php). I don't know if there is something wrong as I am very bad at PHP:

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'myemail@email.com';
$subject = $_POST['subject'];
$human = $_POST['human'];

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

if ($_POST['submit'] && $human == '15') {
if (mail($to, $subject, $body)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $human != '15') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>


If anyone who understands PHP could help, that would be amazing.

Thanks in advance.

Answer

As said by Sakthi Karthik, you need to check the directory/path of contact.php.

Also in the condition you should check:

if(isset($_POST['submit'] && $human == '15')

Likewise:

else if(isset($_POST['submit'] && $human != '15')