Sean Konig
Sean Konig

Reputation: 27

Contact PHP not working

I have never run in to this before, the temp came with no php so I tried to code a really simple one myself. The send message button seems to be a link though and does not submit. Would you be kind enough to check it out for me. Here is the HTML

<form method="post" action="contact.php">
<div class="row half">
    <div class="6u">
        <input name="name" placeholder="Name" type="text" class="text" />
    </div>
    <div class="6u">
        <input name="email" placeholder="Email" type="text" class="text" />
    </div>
</div>
<div class="row half">
    <div class="12u">
        <textarea name="message" placeholder="Message"></textarea>
    </div>
</div>
<div class="row half">
    <div class="12u">
    <a href="#" class="button button-icon icon icon-envelope">Send Message</a>
</div>

And then my php

<?php
   $name    = $_POST ['name'];
   $email   = $_POST ['email'];
   $message = $_POST ['message'];
   if(!$name || !$company || !$email)
   {
       echo "
<h2>Feedback</h2>
<form method='post' action='contact.php'>
   Name: <br /><input name='name' type='text' value='$name' /><br /><br />
   EMail: <br /><input name='email' type='text' value='$email' /><br /><br />
   Message:<br />
   <textarea name='message' cols='55' rows='8'>$message</textarea><br /><br />
   <input type='submit' value='Send' />
</form>
<p>All fields are required</p>
    ";
       exit;
   }
$send_to = '[email protected]'; // change to your email
mail($send_to, "Name: $name" , $message, "From: $email");
echo "Thank you for your feedback";
?>

Upvotes: 0

Views: 2208

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74232

You had if(!$name || !$company || !$email)

I replaced it with:

if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))

This checks for any empty fields.

NOTE:

You can add an extra field for company in your form, as well as empty($_POST['company']) || to your PHP form handler.

This is tested code:

Form:

<form method="post" action="contact.php">
<div class="row half">
    <div class="6u">
        <input name="name" placeholder="Name" type="text" class="text" />
    </div>
    <div class="6u">
        <input name="email" placeholder="Email" type="text" class="text" />
    </div>
</div>
<div class="row half">
    <div class="12u">
        <textarea name="message" placeholder="Message"></textarea>
    </div>
</div>
<div class="row half">
    <div class="12u">

<input type="submit" value="Send Message" name="submit_button" class="button button-icon icon icon-envelope">

    </div>
</form>

Handler:

<?php
   $name    = $_POST['name'];
   $email   = $_POST['email'];
   $message = $_POST['message'];

if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))

 {
       echo "
<h2>Feedback</h2>
<form method='post' action='contact.php'>
   Name: <br /><input name='name' type='text' value='$name' /><br /><br />
   EMail: <br /><input name='email' type='text' value='$email' /><br /><br />
   Message:<br />
   <textarea name='message' cols='55' rows='8'>$message</textarea><br /><br />
   <input type='submit' value='Send' />
</form>
<p>All fields are required</p>
    ";
       exit;
   }

else {
$send_to = '[email protected]'; // change to your email
mail($send_to, "Name: $name" , $message, "From: $email");

echo "Thank you for your feedback";
}

?>

Upvotes: 1

Schleis
Schleis

Reputation: 43800

You are correct, the "Send Message" is a link. So it is just acting like any link and not submitting the form.

In order to submit the form, you want to either change it to <input type="submit">, <input type="button">, or create a click event handler in javascript to submit the form.

Upvotes: 3

Related Questions