php code in movable type page templates

431 views Asked by At

I'm trying to write a contact form for a company that uses the Movable Type platform. I'm in the process of creating page templates for them to use. I am 95% sure that my php code and my html is correct for the table and referencing each other- this is not the source of the issue. However MT doesn't automatically render php. Instead I get a huge block of all my php text. The html renders just fine. I am unsure of how to force MT to recognize the php. I can't find a setting or anything that directly addresses this problem. The closest I can find in the MT help/faq areas is here. But I've tried using the code they provide <$MTEntryText encode_php="here"$> but it changes absolutely nothing about how the page renders. Below is the php I'm trying to use, but I don't think it's the source of the problem. I figured I should include it just in case. Am I just missing the point of how I need to tag things for MT? I'm working on the Movable Type platform for the first time, and with php for the third-ish time, so please feel free to talk to me like I'm an infant when explaining what I'm missing.

<?php

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

    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_to = "[email protected]";

    $email_subject = "Web Contact Response";

    function died($error) {

        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['telephone']) || 
        !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['telephone']; // not required 
    $comments = $_POST['comments']; // 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(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);  
?>

Thank you for contacting us. We will be in touch with you very soon.  
<?php 
}
?>
1

There are 1 answers

1
Charlie Gorichanaz On

Movable Type outputs text in templates verbatim with the exception of MT template tags, which are processed accordingly.

For example, if you have an Index Template that contains only the code:

<?php
<a href="<$mt:BlogURL$>"><$mt:BlogName$></a>
?>

It will generate a file at the publishing path set for that index template containing the text:

<?php
<a href="http://blogurl.com">My Blog</a>
?>

The PHP will only be parsed by the web server upon user request of the file if the published file is a type typically run through the PHP parser, such as a .php file. Movable Type does not touch any PHP code when it publishes, nor does it touch anything other than MT template tags. The rest is just output as is.

Based on your question, it seems you might be trying to put your above code inside the body of an MT page, and are using an MT tag like <$MTEntryText encode_php="here"$> on the page template. You describe the code as appearing on the resulting page as you entered it, which sounds to me as expected. I am guessing you might be outputting this PHP on a page that ends in .html and PHP is not parsing therefore, but I cannot be sure without knowing exactly where you are entering the above code and what the publishing path of the template that generates the resulting file is.

If that is the case, you might be able to solve your issue simply by changing the extension of the template publishing path to .php. Or if your templates automatically use the system extension, you may need to go to the blog settings “General Settings” page and change the “File Extension” under “Archive Settings” to php.

Just FYI, the encode_php modifier is intended for use when plugging data into PHP code, as shown on the docs example: $the_title = '<$MTEntryTitle encode_php="q"$>';. This is not intended to be used on a PageBody or EntryBody tag for general output of code you intend to run, as it might end up escaping various things in your code you entered on the page.