0

I am making my record label's official website and just arrived to the contact page of it. I had tried to set it up in a different way, asking help by Google with no positive result! Here you can see my actual using contact form's php part:

<?php
    $name=$_REQUEST['name'];
    $email = $_REQUEST['email'];
    $message = $_REQUEST['message'];
    $subject = $_REQUEST['subject'];
    $from = 'From: my website name'; 
    $to = 'my@email.com'; 
    $subject = 'New message from the Website!';

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

    if ($_POST['submit']) {
        if ($name != '' && $email != '') {
            if ($human == '4') {                 
               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>'; 
                } 
            } else if ($_POST['submit'] && $human != '4') {
                echo '<p>You answered the anti-spam question incorrectly!</p>';
            }
        } else {
            echo '<p>You need to fill in all required fields!!</p>';
        }
    }

    //redirect to the 'thank you' page
    header('Location: contact.html');
?>

Here is the HTML form:

<form role="form"  action="send.php" method="POST" enctype="multipart/form-data">
            <div class="form-group">
              <label for="name">name</label>
              <input type="name" id="name" placeholder="Your Name *">
            </div>
            <div class="form-group">
              <label for="email">Email</label>
              <input type="email" id="email" placeholder="Your Email *">
            </div>
            <div class="form-group">
              <label for="subject">Subject</label>
              <input type="text" placeholder="Subject" id="subject">
            </div>
            <div class="form-group">
              <label for="message">message</label>
              <textarea id="message" cols="30" rows="7" placeholder="Message"></textarea>
            </div>
             <div class="form-group">
              <label for="text" >*What is 2+2? (Anti-spam)</label>
              <input name="human" placeholder="2 + 2 =">
            </div>

            <button type="submit" class="btn-default">Send it</button>
          </form>

What is my mistake?

user2672165
  • 2,986
  • 19
  • 27
Roland
  • 5
  • 2

2 Answers2

2

You are asking for $_POST['submit'] which is never set, because no input field or button of your form is named "submit".

Change your button to:

<input type="submit" class="btn-default" name="submit" value="Send it">

or set a hidden field named submit:

<input type="hidden" name="submit" value="value">

And then your redirect will fail with the "Header already sent" error, because you must not have output before a header() call. Put the header() call in an else part of your if($_POST['submit'])

The same is for every other input field. The name is missing. Instead you use the name as the type. The type should be "text" everywhere.

Maarkoize
  • 2,601
  • 2
  • 16
  • 34
  • Can you (just for debugging) remove the header call and replace it with an echo which outputs something? Then we can see, what the script does. – Maarkoize Jan 13 '14 at 14:25
  • can you please help a bit more pls, i an a newcomer in programming So i will delete the header call and put instead of it: echo 'what i need to put here' – Roland Jan 13 '14 at 14:35
  • It doesn't matter. Just type `echo "test";` to see, if "test" is displayed. – Maarkoize Jan 13 '14 at 15:03
  • got back this: "You need to fill in all required fields!! test" But i had filled everything in! :O – Roland Jan 13 '14 at 15:08
  • Ah okay..that's the same error - you must name your input fields with name="thename" - for example name="name" or name="email". And the type should be "text" everywhere. – Maarkoize Jan 13 '14 at 15:27
  • nice! Thank you so much Marcel! it worked! and thanks for everybody who tried to help me! – Roland Jan 13 '14 at 16:42
-1

first try with plain function with hard coded parameters to mail() function, make sure you are receiving it.

example :

mail('your@email.com', 'hi', 'hello', 'from@email.com');
codepiper
  • 129
  • 10