jeudi 13 août 2015

.PHP form submit not working

I have been trying to figure this stupid PHP thing for 2 days now. I am designing a website for my wife who is a personal trainer. I would like people to be able to submit their contact info to her via a form I have on the site. I have no clue how to code PHP myself, so I snagged a "working model" and made some edits to fit my site. Here is the HTML code:

<form name="contact" method="post" action="formsubmit.php">             
<li>

<input name="first_name" type="text" value="first name (Required)" onfocus="if(this.value == 'first name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'first name'; }" />

<input name="last_name" type="text" value="last name (Required)" onfocus="if(this.value == 'last name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'last name'; }" />

<input name="email" type="text" value="email (Required)" onfocus="if(this.value == 'email (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'email (Required)'; }" />

<input name="telephone" type="text" value="phone" onfocus="if(this.value == 'phone (Required)') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'phone'; }" />

<input name="comments" type="text" value="What are your goals?" onfocus="if(this.value == 'What are your goals?') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'What are your goals?'; }"  />

 <div class="g-recaptcha" data-sitekey="I am using site key here, just wasn't sure if the public should be privy to that info or not"></div>

 </li>
 </form>

 <li>
 <input type="submit" value="SUBMIT" class="submitbtn" form="contact" />
 </li>

And here is the .php

<!doctype html>
<html>
<head>

<!-- CSS -->
<link href="main.css" rel="stylesheet" type="text/css" />

<meta charset="UTF-8">
<title>We Will EnduroFit</title>
</head>

<body>
<?php

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



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "jasmine@freedomseed.org";

    $email_subject = "Message from EnduroFit visitor";        

    function died($error) {

        // your error code can go here

        echo "I am 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['email']) ||

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

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

        died('I am 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

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

    $telephone = $_POST['telephone']; // 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,$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($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 .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Last Name: ".clean_string($last_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 success html here --> 

<h2>Thanks! I will respond to you soon :)</h2>

<?php

}

?>
</body>
</html>

If anyone can take a look at that and tell me what I am missing. I am about to throw something (or just delete the stupid form altogether and say screw it). The submit button just acts like its dead. It does nothing at all. I know it was working as a model, so I have no idea what I did/did not change to break the stupid submit button. Thanks a million!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire