【问题标题】:PHP echo in a HTML PageHTML页面中的PHP回显
【发布时间】:2016-09-09 17:54:23
【问题描述】:

我有一个联系人。html 页面上有一个表单。表单操作转到 .php 页面来处理电子邮件,没什么特别的。在那个页面上我有:

<?php
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

$FirstName = check_input($_REQUEST['FirstName']);
$LastName = check_input($_REQUEST['LastName']);
$email = check_input($_REQUEST['email']);
$phone = check_input($_REQUEST['phone']);
$message = check_input($_REQUEST['message']);
$human = check_input($_REQUEST['human']);
$webpackage = check_input($_REQUEST['webpackage']);
$webdesign = check_input($_REQUEST['webdesign']);
$customdesign = check_input($_REQUEST['customdesign']);

if ($human == 5) {

$to = "****.com";
$subject = "From ****";

$body = " From: $FirstName  $LastName\n\n E-Mail: $email\n\n Phone:     $phone\n\n Message:\n\n $message\n\n Web Package:$webpackage\n\n Web     Design:$webdesign\n\n Custom Design:$customdesign";


mail ($to, $subject, $body);
header('location: index.html');
}
else {
$result="<div class=\"alert alert-danger\">Sorry there was an error sending     your message. Please go back and check your anti-spam answer</div>";
}

?>

我有一个等于 5 的简单框,我正在检查它的值。这有效,并发送了包含所有信息的电子邮件。 BUT 如果不等于 5,那么问题就出现了。该页面转到我的action.php 页面并且是空白的。

我在contact.html页面上的html是:

    <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                    <?php echo($result); ?>
                </div>
            </div>

使用它通过表单访问我的action.php 页面。其他的都是 .html:

 <form class="form-horizontal" id="contact-form" method="post" action="/action.php">

有没有办法做到这一点?我有一个解决方法,我只是 echo 来自 .php 页面的错误。如果!=5 有效,但不完全是我想要做的。你可能会说,我不懂 PHP。

【问题讨论】:

  • 可能您遗漏的部分代码出错,脚本停止。
  • “页面转到我的 .php 页面”是什么意思?您如何从处理电子邮件的页面转到显示 DIV 的页面?
  • 我认为您需要发布整个脚本,从您提供的 sn-ps 中无法判断您真正在做什么。
  • 希望对@Barmar 有所帮助。我的错误,我假设每个人都知道我有时在想什么。将表单发布到上面的 action.php。当给出正确答案 5 时效果很好。它的 else 不起作用。
  • My html on the contact.html page is: 那么....为什么你的html页面包含PHP代码?

标签: php html forms email


【解决方案1】:

您可以在$_SESSION[] 数组中设置一个变量,并在您的“else”部分使用Header() 重定向到显示您存储的值的页面。

查看其他已回答问题中的示例:

displaying a message after redirecting the user to another web page

【讨论】:

    【解决方案2】:
    1. 使用以下代码更新您的 else 部分:

       } else {
           header('location: contact.html?status=error');
       }
      
    2. 现在检查您的contact.html 页面上是否设置了get 方法。如果是,则设置并显示您的 $result 值。

      <?php 
        if(isset($_GET['status']) && $_GET['status']=='error' ) { 
      
        $result="<div class=\"alert alert-danger\">Sorry there was an error sending     your message. Please go back and check your anti-spam answer</div>";           
      
      } ?>
      
    3. 在 contact.html 上检查 $result 是否有价值并打印出来:)

    【讨论】:

      【解决方案3】:

      像这样在你的action.php中添加一个指向contact.html的重定向

      else {
      $result="Sorry there was an error sending your message. Please go back and check your anti-spam answer"; 
      $result=str_replace(" ","%20",$result);
      header('location: contact.html?result=$result'); 
      } 
      

      然后用GET在contact.html中得到结果

      $result= $_GET['result'];
      

      理想情况下,在收到结果后,在目标 Contact.html 页面中对结果进行 html 标记。这消除了通过 http 传递 html 的细微差别

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-16
        • 2012-11-05
        • 2012-03-11
        相关资源
        最近更新 更多