-1

I have a simple form:

<form action="send_form_email.php" method="post" enctype="text/plain">
    <input type="text" name="first_name" placeholder="NAME:" size="100" class="grey-bg"/>
    <input type="text" name="last_name" placeholder="E-MAIL:" size="100" class="grey-bg"/>
    <br />
    <br />
    <textarea type="text" name="message" rows="20" cols="201" placeholder="MESSAGE:" size="162"></textarea>
    <br />
    <div>
        <input id="contact-send-btn" type="submit" value="Send">
    </div>
</form>

And this is the php file:

<?php

if(isset($_POST['email'])) {



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "xxxxxxxx";

    $email_subject = "Hello";





    function died($error) {

        // your error code can go here

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }



    // validation expected data exists

    if(!isset($_POST['first_name']) ||

        !isset($_POST['last_name']) ||

        !isset($_POST['message']) ||

        died('We are sorry, but there appears to be a problem with the form you submitted.');       

    }



    $first_name = $_POST['first_name']; // required

    $last_name = $_POST['last_name']; // required

    $message = $_POST['message']; // required

    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

  if(strlen($message) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";

    $email_message .= "Last Name: ".clean_string($last_name)."\n";

    $email_message .= "Message: ".clean_string($email_from)."\n";



// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->



Thank you for contacting us. We will be in touch with you very soon.



<?php

}

?>

However when I click the button it just opens the php file in the browser and nothing is received in my inbox? All I want is to receive the email with the details in the form?

SFDSF
  • 19
  • 3

3 Answers3

1

You have several problems.

Encoding Type

enctype="text/plain"

PHP does not support plain text encoded form data. Remove that attribute (and use the default encoding type).

See also the spec:

Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).

Test for submission

You said if(isset($_POST['email'])) {

… but you don't have any field with name="email". Test to see if a field you actually have exists*

Opening PHP in the browser

This statement is a little ambiguous:

it just opens the php file in the browser

If you mean that your browser displays the PHP source code, then see this question: PHP code is not being executed (I can see it on source code of page)

Asides

placeholder="NAME:"

The HTML5 placeholder attribute is not a substitute for the label element

 $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

This fails to match many perfectly valid email addresses (such as those with a + in the section before the @ or those from the .museum top level domain).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Exactly and just as I told the OP [in this comment](http://stackoverflow.com/questions/34435124/form-send-button-php-not-working#comment56616804_34435124) I left them ;-) – Funk Forty Niner Dec 23 '15 at 14:41
0

You did not add name attribute to submit button.

Change

<input id="contact-send-btn" type="submit" value="Send">

To:

<input id="contact-send-btn" type="submit" value="Send" name="email"/>

While posting forms, only elements with name get posted.

In your case, submit button has no name attribute, so, it was not getting posted.

Hence, it is not coming into the if.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • I did that but still nothing? – SFDSF Dec 23 '15 at 12:02
  • I believe their use of "email" was intended for `` and should be adding another input for "email" and changing their placeholder and not as the submit button. Their first conditional statement is where it's all supposed to "happen" ;-) – Funk Forty Niner Dec 23 '15 at 14:43
0

Looks like you don't have apache , php installed thats why you are seeing the content of the .php file . If you are on Windows Install XAMPP if you are on Linux Install LAMP Stack . Hope this helps

Arijit
  • 1,506
  • 14
  • 23