Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top