0

I am a newbie, old and very new to php. I am trying to learn php (and everything else) to incorporate into web sites. I have ripped a sample form, with php control, to help me learn. Uploading the HTML page and the php file to my web host, I find that nothing happens when I press the submit "button" - there is brief pause on the web page and then a blank page. Would somebody very kindly look at stripped-down php code and please tell me what error/s I have made? Thank you so much.

HTML code:

<!DOCTYPE html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Try</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
.contact-form {
  margin-top: 40px;
}
.contact-form input, .contact-form textarea {
  width: 50%;
  outline: 0;
  border: 1px solid #aaaaaa;
  padding: 12px;
  color: #999999;
  margin-bottom: 30px;
}
.contact-form input:focus, .contact-form textarea:focus {
  outline: 0;
}
.contact-form .button {
  width: auto;
  background: #5f6d7e;
  color: white;
  text-transform: uppercase;
  border: 0;
}
</style>

    </head>
    <body>

                    <!-- CONTACT -->
                    <div id="contact">
                    <div class="row">
                        <div>
                            <h4>Contact Us</h4>
                            <p>We welcome any enquiries.</p>
                        </div>
                    </div>
                    <div>
                        <form action="send_form_email.php" method="post" class="contact-form">
                                    <fieldset>
                                        <input type="text" name="first_name" placeholder="Your First name...">
                                    </fieldset>
                                    <fieldset>
                                        <input type="submit" class="button big default" value="Send Message">
                                    </fieldset>

                        </form>
                     <!-- .contact-form -->
            </div>
            </div>        
    </body>
</html>

php Code:

<?php

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

        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to = "me@myself.xxx";
        $email_subject = "Test php";

        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']) ||
            {
            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }



        $first_name = $_POST['first_name']; // 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(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";

    // 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

    }
    ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

1 Answers1

1

Your whole PHP file is surrounded by a large if:

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

but your form does not contain a field with name email. So either include a text field named email, remove the if, or change the if to

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

As the code seems to generate an email, the first option might be the best choice.

Karsten Koop
  • 2,475
  • 1
  • 18
  • 23
  • Thank you so much for your reply. What I had done was strip both the html and the php to what I thought was the minimum (to save space). If I were able to post the full (original) HTML and php codes, would that help? There is the email field in the original ..... mea maxima culpa. – Dunderhead Feb 23 '18 at 12:31