1

I am trying to get a PHP form to email me the input fields but I am very new to PHP and don't understand why this isn't working. When Submit is clicked it displays the PHP file as a text file in the web browser. How do I fix this? Thanks in advance. Here is the PHP as it sits. I can upload the HTML and relevant CSS if needed, but don't know why they would be. Also, in case it is relevant, the file extension for the PHP is PHP. I'm pretty sure that's what it is supposed to be.

Do to a few comments I reduced the name of the variables and removed the special characters. Here is the updated code.

Thanks for the answers and links. I checked them all out and discovered that in the end this was a total noob mistake. I hadn't downloaded any php software so my computer want translating the code from the local source. I'm downloading wamp when I get home. That should hopefully fix things.

if(isset($_POST['email'])) {
    $email_to = "sellit4223@gmail.com";
    $email_subject = "GOT ONE";
    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['email']) ||
        !isset($_POST['telephone']) ||
    !isset($_POST['business_or_product_name']) ||
        !isset($_POST['Doing']) ||
    !isset($_POST['Do'])) {
        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
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // required
    $business_or_product_name = $_POST['business_or_product_name']; // required
    $Doing = $_POST['Doing']; // not required
    $Do = $_POST['Do']; // not 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(!preg_match($string_exp,$business_or_product_name)) {
    $error_message .= 'The business or product name you entered does not appear to be valid.<br />';
  }

 if(strlen($Doing) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

if(strlen($Do) < 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 .= "business_or_product_name: ".clean_string($business_or_product_name)."\n";
    $email_message .= "Doing: ".clean_string($Doing)."\n";
    $email_message .= "Do: ".clean_string($Do)."\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

}

?>
  • This sounds more like a server issue than a php issue - are other .php files working? – willoller May 27 '16 at 05:00
  • Setting variable name with so much character doesn't seem to be a good idea –  May 27 '16 at 05:08
  • I have reduced the variable names to "Doing" and "Do", but this did not help the issue :( I don't know if other PHP files are working as this is my first attempt. Is there a simple way to tell? – Cyric Crowley May 28 '16 at 08:17
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – miken32 May 30 '16 at 22:37

4 Answers4

2

Change Briefly_Explain_What_You're_Doing to Briefly_Explain_What_Youre_Doing

<?php

if(isset($_POST['email'])) {
    $email_to = "sellit4223@gmail.com";
    $email_subject = "GOT ONE";
    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['email']) ||
        !isset($_POST['telephone']) ||
    !isset($_POST['business_or_product_name']) ||
        !isset($_POST['Briefly_Explain_What_Youre_Doing'])) {
        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
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // required
    $business_or_product_name = $_POST['business_or_product_name']; // required
    $Briefly_Explain_What_Youre_Doing = $_POST['Briefly_Explain_What_Youre_Doing']; // not required
    $Briefly_Explain_What_We_Can_Do_For_You = $_POST['Briefly_Explain_What_We_Can_Do_For_You']; // not 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(!preg_match($string_exp,$business_or_product_name)) {
    $error_message .= 'The business or product name you entered does not appear to be valid.<br />';
  }

 if(strlen($Briefly_Explain_What_Youre_Doing) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

if(strlen($Briefly_Explain_What_We_Can_Do_For_You) < 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 .= "business_or_product_name: ".clean_string($business_or_product_name)."\n";
    $email_message .= "Briefly_Explain_What_You're_Doing: ".clean_string($Briefly_Explain_What_Youre_Doing)."\n";
    $email_message .= "Briefly_Explain_What_What_We_Can_Do_For_You: ".clean_string($Briefly_Explain_What_What_We_Can_Do_For_You)."\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

}

?>
keziah
  • 564
  • 1
  • 6
  • 24
  • Thank you so much for your help. In response to your comment and a few others I have changes "Briefly_Explain_What_You're_Doing" to "Doing" and Briefly_Explain_What_We_Can_Do_For_You" to "Do" but the problem persists. – Cyric Crowley May 28 '16 at 07:02
1

Rules for PHP variables:

  1. A variable starts with the $ sign, followed by the name of the variable.
  2. A variable name must start with a letter or the underscore character.
  3. A variable name cannot start with a number.
  4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

You have a variable named $Briefly_Explain_What_You're_Doing, which violates rule #4, and is causing your code to go haywire.

e_i_pi
  • 4,590
  • 4
  • 27
  • 45
  • Thank you so much for your help. In response to your comment and a few others I have changes "Briefly_Explain_What_You're_Doing" to "Doing" and Briefly_Explain_What_We_Can_Do_For_You" to "Do" but the problem persists. – Cyric Crowley May 28 '16 at 07:03
0

There is an additional ' you don't need ("You're"):

!isset($_POST['Briefly_Explain_What_You're_Doing'])) {

You must omit it.

Gynteniuxas
  • 7,035
  • 18
  • 38
  • 54
  • It needs to be `$_POST["Briefly_Explain_What_You're_Doing"]`. It's breaking the string. Just look at the syntax highlighting. – Darren May 27 '16 at 05:03
  • It's best to not use any special characters because it can cause problems in future. I would say only letters, numbers and underscore should be used. – Gynteniuxas May 27 '16 at 05:05
  • Thank you so much for your help. In response to your comment and a few others I have changes "Briefly_Explain_What_You're_Doing" to "Doing" and Briefly_Explain_What_We_Can_Do_For_You" to "Do" but the problem persists. – Cyric Crowley May 28 '16 at 07:02
  • Are you sure all indexes are replaced and not just the first one? – Gynteniuxas May 28 '16 at 07:26
  • Yes, I double checked it. I would repost the code as it sits now, but it is too long for a comment. I am about to try to update the original post. – Cyric Crowley May 28 '16 at 08:19
  • So, what exactly is the problem right now? Earlier it was because it showed PHP code, what about now? – Gynteniuxas May 28 '16 at 08:43
  • It still shows the PHP code in my browser instead of sending me an email. – Cyric Crowley May 28 '16 at 21:24
0

You should read about variables naming convention in php, and how to use single/double quotes.

according documentation - php.net

D.Dimitrioglo
  • 3,413
  • 2
  • 21
  • 41