-1

我一直在为我们的努力,我只是无法让它工作,非常感谢您的帮助。

    <?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "contact@mysite.com";

/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$feedback_page = "contact.html";
$error_page = "error.html";
$thankyou_page = "thankyou.html";

/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$name = $_REQUEST['name'] ;
$email_address = $_REQUEST['email_address'] ;
$subject = $_REQUEST['subject'] ;
$comments = $_REQUEST['comments'] ;

/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
function isInjected($str) {
    $injections = array('(\n+)',
    '(\r+)',
    '(\t+)',
    '(%0A+)',
    '(%0D+)',
    '(%08+)',
    '(%09+)'
    );
    $inject = join('|', $injections);
    $inject = "/$inject/i";
    if(preg_match($inject,$str)) {
        return true;
    }
    else {
        return false;
    }
}

// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email_address'])) {
header( "Location: $feedback_page" );
}

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: $error_page" );
}

// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}

// If we passed all previous tests, send the email then redirect to the thank you page.
else {
    $eml = "Name: ".$name."\r\n"."Subject: ".$subject."\r\n"."Comments: "."\r\n".$comments;

mail( "$webmaster_email", "Visitor Contact",
 $eml, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>

好吧,您看到上面的 PHP,我想要的只是 php 文件将错误或成功字符串发送回 html 中,<h1> header </h1>而不是将我重定向到那些页面。

4

3 回答 3

2

代替

header();

require_once();

例如。

require_once($thankyou_page);

这将做的是获取文件的内容并从中读取。如果您只需要这样做,则无需重定向。

于 2013-08-02T03:59:32.883 回答
1

如果我正确理解了您的问题,您只需将成功或失败的状态传递给您的页面。而且由于您的页面具有扩展名,.html因此无法在其中运行 php 代码。因此,如果您想检查thankyouerror页面内的状态,好的解决方案是将文件扩展名重命名为 .php。

例如

您可以从标题发送状态

<?php
 $thankyou_page = 'thankyou_page.php'; //notice the extension
 header("location:$thankyou_page?status=1"); //send status code in your header

在你的thankyou_page.php

<?php
 if( isset($_GET['status']) && $_GET['status'] == 1){
    //success
    //do whatever you want
 }

您不能在 .html 文件中运行 PHP,因为除非您告诉它,否则服务器不会将其识别为有效的 PHP 扩展。为此,您需要在您的 Web 根目录中创建一个 .htaccess 文件并将这一行添加到其中:

AddType application/x-httpd-php .htm .html

这将告诉 Apache 将具有 .htm 或 .html 文件扩展名的文件作为 PHP 文件处理。

来源:如何将 PHP 代码/文件添加到 HTML(.html) 文件?

警告: 感谢@Fred

提到 AddType application/x-httpd-php .htm .html 是它会改变“所有” .htm/.html 文件的功能。所以不是“一个”文件,而是“所有”文件 .htm/.html

于 2013-08-02T04:28:40.570 回答
0

正如 Izodn 所说..您可能想要做的是包含错误或成功页面的 html(error.html,thankyou.html)。

但是,据我了解.. 在您希望显示特定错误或成功消息的页面中有一个 h1 元素。

因此,在您在上面发布的联系表单处理程序代码中。您需要一个变量来存储您希望在错误/感谢页面中显示的消息。

就像是:

....
$message = "Thank You"; // default message will be "Thank You" if no errors

// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
   $message = "Error: You must enter an e-mail address and comment!";
   require_once($error_page);
   exit;
}

// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
   $message = "Error: Injection attempt discovered!";
   require_once($error_page);
   exit;
}

... send email

require_once($thankyou_page);

出口在那里,因此在显示错误页面后,将不再运行代码(否则它将继续发送电子邮件并显示感谢页面。

因此,然后将您的thankyou.html 和error.html 页面更改为thankyou.php 和error.php,这样我们就可以使用PHP 打印出$message;

在这些页面中,您可以输入以下内容:

<h1><?php echo $message; ?></h1>

编辑:刚刚看到你对 Izodon 的回复。

所以,你不能只是“发送一个字符串”到一个 html 页面(编辑:这并不完全正确,你可以使用 javascript 来获取结果并操纵 DOM 来显示消息,但这可能不适合这种情况) . 包含联系表单的 html 页面是静态的(不会更改),当您提交表单时,您将被带到包含表单处理代码(您发布的内容)的 php 页面。现在,一旦你在那个 php 页面,有两种可能性:要么让 php 输出一些 html,要么将用户重定向到另一个页面。

如果你想让它“看起来”就像你没有离开页面(点击提交并且除了显示错误消息之外没有任何变化),你可以在一个 php 文件中完成所有操作。您可以使用 $_SERVER['PHP_SELF'] 将表单提交给自身,并使用 $_POST 使用代码来检测表单是否已提交。如果表单已提交,您运行处理代码,该代码可能会设置一个包含错误或成功消息的 $message 变量。然后您将拥有您的 html 代码,并在其中使用 php 回显您的 $message 变量。

于 2013-08-02T04:20:07.380 回答