如何在'echo'内容中使用php'include'功能?

时间:2014-01-15 11:23:57

标签: php html css

我有一个process.php文件用于处理评论/消息表单。如果在处理过程中出现错误,则会回显错误的表单内容,并将其显示为名为process.php的网页,以便进行更正和重新提交。

问题是我需要回显的内容包含各种<?php include("xxxx.php");?>元素,以便它与我网站的其余部分匹配。但这似乎使页面翻倒(显示没有内容的空白页)。我被告知我应该在回显的内容中使用include("xxxx.php");echo file_get_contents("xxxx.php");,但都不会显示预期的内容。

非常感谢对这些问题的任何帮助。

代码:(某些项目xxxxx为安全性)

<?php

// Information to be modified

$your_email = "xxxxxxxx@xxxxx.xx.xx"; // email address to which the form data will be sent
$subject = "Contact message"; // subject of the email that is sent
$thanks_page = "thankyou.htm"; // path to the thank you page following successful form submission
$contact_page = "mail_form_styled.php"; // path to the HTML contact page where the form appears


// Nothing needs to be modified below this line

if (!isset($_POST['submit'])) {
    header( "Location: $contact_page" );
  }

if (isset($_POST["submit"])) {
    $nam = $_POST["name"];
    $ema = trim($_POST["email"]);
    $com = $_POST["comments"];
    $spa = $_POST["spam"];

    if (get_magic_quotes_gpc()) { 
    $nam = stripslashes($nam);
    $ema = stripslashes($ema);
    $com = stripslashes($com);
    }

$error_msg=array(); 

if (empty($nam) || !preg_match("~^[a-z\-'\s]{1,60}$~i", $nam)) { 
$error_msg[] = "The name field must contain only letters, spaces, dashes ( - ) and single quotes ( ' )";
}

if (empty($ema) || !filter_var($ema, FILTER_VALIDATE_EMAIL)) {
    $error_msg[] = "Your email must have a valid format, such as name@mailhost.com";
}

$limit = 1000;

if (empty($com) || !preg_match("/^[0-9A-Za-z\/-\s'\(\)!\?\.,]+$/", $com) || (strlen($com) > $limit)) { 
$error_msg[] = "The Comments field must contain only letters, digits, spaces and basic punctuation (&nbsp;'&nbsp;-&nbsp;,&nbsp;.&nbsp;), and has a limit of 1000 characters. Website addresses can not be included.";
}

if (!empty($spa) && !($spa == "4" || $spa == "four")) {
    echo "You failed the spam test!";
    exit ();
}

// Assuming there's an error, refresh the page with error list and repeat the form

if ($error_msg) {
echo '<!DOCTYPE html>
<html lang="en">

<!-- Begin head items -->

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1">
<meta name="description" content="The Dark Fortress contact form. Use it to get in touch&#8230;" />

<link href="../styles/screen.css" rel="stylesheet" type="text/css" media="screen" />

<link rel="alternate" type="application/rss+xml"
title="thedarkfortress Command Briefing"
href="http://feeds.feedburner.com/ThedarkfortressCommandBriefing" />

<title>O dear! | The Dark Fortress</title>

<style type="text/css">
.hide {display:none;}
</style>

</head>

<!-- Begin body items -->

<body>
<div id="container">

<!-- Begin header items -->
echo file_get_contents("../components/header.php");

<!-- Begin main content items -->

<div id="content-container">

<!-- Begin content items -->

<div id="content">
<h1>O dear!</h1>

<p>Unfortunately, your message could not be sent. The form as you filled it out is displayed below. Make sure each field completed, and please also address any issues listed below:</p>

        <ul class="err">';
foreach ($error_msg as $err) {
echo '<li>'.$err.'/li>';
}
echo '</ul>
<form method="post" action="', $_SERVER['PHP_SELF'], '">
<label for="name">Name</label>
<input name="name" type="text" size="40" maxlength="60" id="name" value="'; if (isset($_POST["name"])) {echo $nam;}; echo '">
<label for="email">Email Address</label>
<input name="email" type="email" size="40" maxlength="60" id="email" value="'; if (isset($_POST["email"])) {echo $ema;}; echo '">
<label for="comm">Comments</label>
<textarea name="comments" rows="7" cols="50" id="comm">'; if (isset($_POST["comments"])) {echo $com;}; echo '</textarea>
<div class="hide">
<label for="spam">What is six plus four?</label>
<input name="spam" type="text" size="4" id="spam">
</div>
<input type="submit" name="submit" value="Send" class="button orange send" />
</form>

<div class="divider"><hr /></div>
<p><img src="../main_assets/isiah_page_sig_flat.png" alt="Isiah signature" /></p>
<p><strong>Chronicler Isiah,</strong> the 4th Battle Company, Dark Angels.</p>
</div>

<!-- Begin left nav items -->

<div id="leftnav">
echo file_get_contents("../components/hq_leftnav.php");
</div>

</div>
</div>

<!-- Begin footer items -->
echo file_get_contents("../components/footer.php");

<!-- Begin google analytics tracker items -->

<script type="text/javascript"> 
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); 
</script> 
<script type="text/javascript"> 
var pageTracker = _gat._getTracker("xxxxxx"); pageTracker._trackPageview(); 
</script>

</body>
</html>';
exit();
} 

$email_body = 
    "Name of sender: $nam\n\n" .
    "Email of sender: $ema\n\n" .
    "COMMENTS:\n\n" .
    "$com" ; 

// Assuming there's no error, send the email and redirect to Thank You page

if (isset($_REQUEST['comments']) && !$error_msg) {
mail ($your_email, $subject, $email_body, "From: $nam <$ema>" . "\r\n" . "Reply-To: $nam <$ema>");
header ("Location: $thanks_page");
exit();
}  
}

1 个答案:

答案 0 :(得分:1)

你只是像往常一样使用CSS ......

<?php
    // index.php
?>
<!doctype html>
<html>
    <head>
        <style type="text/css">
            .myClass {
                color: #f00;
            }
        </style>
    </head>
    <body>
        <?php
            include('myFile.php');
        ?>
    </body>
</html>
<?php
    // included myFile.php
    echo '<p class="myClass">Echoed content!</p>';

如果你最后得到一个没有内容的空白页面,那么你的PHP可能会有错误。确保启用错误报告,您将能够看到出现了什么问题。