PHP contact form - Why doesn't it work?

Right now I am hitting my head against the wall. I simply can’t figure out what is wrong with my code! Please help me! :slight_smile:
I’m quite new in this field, so I would love some feedback.

This is my code:


// Check for form submission
// Form validation
$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $name . ' <' . $email . '>';
$to = "****@***********"; // This email will receive the data of the contact.php
$subject = "New email to Brand One"; // This is the subject Brand One will receive

$body = "From: $name\
 Tel: $tel\
 Email: $email\
 $message"; // The actual content of the email

if ($_POST['submit']) {
	if ($name != '' && $tel != '' && $email != '') {
		if ($validation == '4') {
			if (mail ($to, $subject, $body, $from)) {
				echo '<script type="text/javascript">alert("Thank you for contacting us. We will get back to you as soon as possible.");</script>';
			} else {
				echo '<script type="text/javascript">alert("Something went wrong. Go back and try again!");</script>';
		} else if ($_POST['submit'] && $validation != '4') {
			echo '<script type="text/javascript">alert("You have answered the anti-spam question incorrectly!");</script>';
	} else {
		echo '<script type="text/javascript">alert("You need to fill in all required fields!");</script>';
}// End of main isset() IF.

<section id="contact">

<h1 class="title">Contact Us</h1>
<h2 class="text">We'd love to hear from you.</h2>

<div class="contactform">
<form method="post" action="">

    <label for="name">Name:</label>
    <input id="name" name="name" type="text" placeholder="Write your name here" required aria-required="true"/>


    <label for="tel"><br/>Telephone:</label>
    <input id="tel" name="tel" type="tel" placeholder="Write your number here" required aria-required="true"/>


    <label for="email"><br/>Email:</label>
    <input id="email" name="email" type="email" placeholder="Write your e-mail here" required aria-required="true"/>


	<label for="validation"><br/>What is 2+2? (Anti-spam)</label>
	<input id="validation" type="text" name="validation" placeholder="Type here"/>

<div class="box">
    <label for="message">Message:</label>
    <textarea name="message" id="message" rows="7" cols="40" placeholder="Write your message here"></textarea>
<br />

<input id="submit" name="submit" type="submit" value="Submit Message">


<div class="contactinfo">
<h3 class="undertitle">Contact Us</h3>
<p>+45 27 15 58 28</p>
<br />
<h3 class="undertitle">Visit Us</h3>
<p>Brand One</p>
<p>Museumsgade 51A,</p>
<p>7400 Herning</p>
<br />
<h3 class="undertitle">Support</h3>
<p>Mandag - Torsdag:</p>
<p>09.00 - 17.00</p>
<br />
<p>09.00 - 14.00</p>

<iframe width="700" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src=";source=s_q&amp;hl=en&amp;geocode=&amp;q=Museumsgade+51A,+7400+Herning&amp;aq=&amp;sll=56.132621,8.975269&amp;sspn=0.007318,0.01929&amp;t=m&amp;g=Museumsgade+51A,+7400+Herning&amp;ie=UTF8&amp;hq=&amp;hnear=Museumsgade+51A,+7400+Herning,+Denmark&amp;ll=56.132621,8.975269&amp;spn=0.00183,0.004823&amp;z=14&amp;output=embed"></iframe><br /><small><a href=";source=embed&amp;hl=en&amp;geocode=&amp;q=Museumsgade+51A,+7400+Herning&amp;aq=&amp;sll=56.132621,8.975269&amp;sspn=0.007318,0.01929&amp;t=m&amp;g=Museumsgade+51A,+7400+Herning&amp;ie=UTF8&amp;hq=&amp;hnear=Museumsgade+51A,+7400+Herning,+Denmark&amp;ll=56.132621,8.975269&amp;spn=0.00183,0.004823&amp;z=14" style="color:#0000FF;text-align:left"></a></small>

<div class="solutionslink">
<a href="solutions.php"><h1 class="title">What can we do for you?</h1></a>

</section> <!-- end of contact  -->


Hi Hindkjaer. Welcome to the forums. :slight_smile:

What stands out to my untrained eye is your mail() function. It’s meant to have 4 (or 5) parameters, in a specific order, which you don’t have:

mail ($destination_email, $email_subject, $email_body, $header);

Try something like this instead:

mail ($to, $subject, $body, "From: \\"$name\\" <$email>" . "\\r\
" . "Reply-To: \\"$name\\" <$email>");

Thank you ralph.m :slight_smile:

I have applied the mail() function, as you mentioned. But it doesn’t fix my exact problem.
So far it seems, that the form sends the email even though the validation number is written as ‘6’ though it is set as ‘4’.

Ah, yes. There’s no actual $validation variable set in your script. So add this:

$name = $_POST['name'];
$tel = $_POST['tel'];
$email = $_POST['email'];
[COLOR="#FF0000"]$validation = $_POST['validation'];[/COLOR]

Mind you, this kind of spam prevention kind of sucks. Either hide that field and allow nothing or 4 to be entered (so that it mainly just picks up bots), or preferably, use a timer instead. The ideal is not to annoy legitimate users while snagging the miscreants. Here’s a tutorial on what I mean by a timer (and also a hidden version of what you have):

Thank you so much ! :slight_smile: Now the validation form works with honeypots. Great idea.
But somehow, the email is not sent to the email, as I told it to.
Can you see why?

Could you post the code you have now? What you posted above is a bit out of date. :slight_smile: