2

I am trying to configure my 'send message/submit' button so that when clicked it accesses a php file in the local directory and executes the code to email the corresponding data.

layout

The HTML code shows the following:

   <section class="contact-us">
    <div class="form-wrapper">
        <div class="contact-form">

            <form>
                <fieldset>
                    <div class="control-group">
                        <input id="Field12" name="Field12" placeholder="Full Name" required="" tabindex="1" type="text" value="">
                    </div>
                    <div class="inline-groups">
                        <div class="control-group">
                            <input id="Field3" maxlength="255" name="Field3" placeholder="Email" required="" spellcheck="false" tabindex="2" type="email" value="">
                        </div>
                        <div class="control-group">
                            <input id="Field4" maxlength="255" name="Field4" placeholder="Phone number (optional)" tabindex="3" type="tel" value="">
                        </div>
                    </div>
                    <div class="control-group">
                        <textarea cols="50" id="Field5" name="Field5" placeholder="Let us know what we can do for you!" spellcheck="true" tabindex="4"></textarea>
                    </div>
                    <input id="idstamp" name="idstamp" type="hidden" value="r885sYVzp1O8HO4V4MdnZvkis7E3NZIfZ8CtHRCtCdY">
                    <button class="button" action="send_form_email.php" type="submit">Send message</button>
                </fieldset>
            </form>

The PHP code shows the following:

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "matty65622@hotmail.com";

    $email_subject = "Greenmount Engineering | Customer Enquiry";





    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['name']) ||

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

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

        !isset($_POST['comments'])) {

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

    }



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

    $email_from = $_POST['email']; // required

    $telephone = $_POST['tel']; // not required

    $comments = $_POST['comments']; // 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,$name)) {

    $error_message .= 'The name you entered does not seem to be valid.<br />';

  }


  if(strlen($comments) < 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 .= "Name: ".clean_string($name)."\n";

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

    $email_message .= "Telephone: ".clean_string($telephone)."\n";

    $email_message .= "Comments: ".clean_string($comments)."\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

}

?>

I am not sure if this code will work 100% yet as I have not tried it, but as you can see the attempt to link the HTML button to the php script only refreshes the page when sent, but when I check my email nothing comes through. <button class="button" action="send_form_email.php"

The Beast
  • 1
  • 1
  • 2
  • 24
Revo
  • 51
  • 5
  • 3
    `
    ` && ``.
    – D4V1D Dec 03 '15 at 08:18
  • When the button is clicked, it takes me to a new webpage with the php code, it doesnt seem to be executing/emailing this data to the targeted email? Can you review my php and tell me where I have gone wrong? – Revo Dec 03 '15 at 09:17
  • seeing the comments below, am guessing that you're trying to run this from your own computer without a webserver installed or PHP, or running as `c:///file.xxx` instead of `http://localhost/file.xxx` @Revo am I right? – Funk Forty Niner Dec 03 '15 at 11:45

2 Answers2

2

You have to replace one line of code.

Always use action in form tag so move the action="send_form_email.php" in <form method="post"> tag and your code will run.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Sunil Gehlot
  • 1,549
  • 2
  • 17
  • 32
  • You forgot to mention the `method` attribute that has to be set to `post`. It will send the form with `get` otherwise. – D4V1D Dec 03 '15 at 08:23
  • [No, forms are sent with `get` method by default.](http://stackoverflow.com/a/2314441/2788131) – D4V1D Dec 03 '15 at 08:27
  • Thanks for pointing i'll edit. Thanks – Sunil Gehlot Dec 03 '15 at 08:28
  • When the button is clicked, it takes me to a new webpage with the php code, it doesnt seem to be executing/emailing this data to the targeted email? Can you review my php and tell me where I have gone wrong? – Revo Dec 03 '15 at 09:17
2

A few adjustments need to be done to your code:

  1. Add method="post" attribute to your <form> tag (so the method used is not get).

  2. Add action="send_form_email.php" attribute to your <form> tag (so your form redirects to your script).

  3. Add name="submit" to your <button> tag (so the expression if(isset($_POST["submit"])) in send_form_email.php evaluates to true and the code block gets executed).

  4. Change name attributes of your <input>s according to your $_POST['...'] (so PHP recognizes the input values).

And you should be good to go.

D4V1D
  • 5,805
  • 3
  • 30
  • 65
  • name="submit" or type="submit" ?? Also, in:
    &&
    – Revo Dec 03 '15 at 09:26
  • The && are showing as text, not as actual functioning code – Revo Dec 03 '15 at 09:26
  • Both `name` and `type` are required in your ` – D4V1D Dec 03 '15 at 09:27
  • It's not working :/ My php code is wrong but I have updated it, please go through it and help me fix why its continuing to open the code in a browser and not send to my target email? – Revo Dec 03 '15 at 09:43
  • 1
    @Revo because, you don't have a webserver/PHP/mail installed or you're accessing as `c:///file.xxx` instead of `http://localhost/file.xxx`. Pretty straightforward here. – Funk Forty Niner Dec 03 '15 at 11:58
  • So you are saying, my php script (**send_form_email.php**), in the: `
    ` it should be like this? rather than a direct link to my script from my computer? And yes I do not have a webserver/php/mail installed..? I am just trying to make it so that this code sends the data to the email targeted but it doesnt actually leave the page that im at (**Contact Us**)
    – Revo Dec 05 '15 at 01:26
  • @Revo: One needs to have implemented a working web server for PHP to be executed (or use CLI). – D4V1D Dec 05 '15 at 22:51