PHP代码显示空白页面

时间:2016-09-17 13:06:05

标签: php

我想从我的html页面发送一封包含输入数据的电子邮件。我正在使用下一个PHP脚本:

<?php
if((isset($_POST['budget']))&&(isset($_POST['type']))) {
    $email_to = "design.er@icloud.com";
    $email_subject = "Design";

    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['budget']) ||
        !isset($_POST['type'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $email = $_POST['email']; // required
    $budget = $_POST['budget']; // required
    $type = $_POST['type']; // required
    $comments = $_POST['comments']; // not required
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    $string_exp = "/^[A-Za-z .'-]+$/";

  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 .= "Email: ".clean_string($email)."\n";
    $email_message .= "Budget: ".clean_string($budget)."\n";
    $email_message .= "Type: ".clean_string($type)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- THERE MY PHP CODE, I REMOVED IT ESPECIALLY FOR STACKOVERFLOW, BUT IT EXIST -->

<?php
}
?>

但它不起作用。当我点击提交时 - 它会显示一个空白页面。 我无法理解问题所在。

此外,我尝试了下一个脚本,它运行得很好:

  <?php
     $to = "xyz@somedomain.com";
     $subject = "This is subject";

     $message = "<b>This is HTML message.</b>";
     $message .= "<h1>This is headline.</h1>";

     $header = "From:abc@somedomain.com \r\n";
     $header .= "Cc:afgh@somedomain.com \r\n";
     $header .= "MIME-Version: 1.0\r\n";
     $header .= "Content-type: text/html\r\n";

     $retval = mail ($to,$subject,$message,$header);

     if( $retval == true ) {
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
  ?>

2 个答案:

答案 0 :(得分:0)

没有任何表现,因为没有什么可展示的。脚本中唯一一个努力显示内容的地方是在没有正确的post变量的情况下访问页面(在这种情况下显示AllowAny方法的内容)。在这个脚本的最底部有一个died()的html评论,那里什么也没有。

答案 1 :(得分:0)

一个绝对空白的页面(即在浏览器中查看页面源时甚至没有输出)通常意味着发生了PHP错误并关闭了错误报告。在这种情况下,您应该查看服务器的日志文件,如果可以的话。 (它可能在某些托管平台上不可用。)

这种白死病的常见错误&#34;是PHP脚本中的语法错误,但你的似乎没问题。但是,某些主机提供商禁用某些功能,如mail()功能,以避免发送垃圾邮件的脚本。在这种情况下,mail()函数可能会导致内部错误,从而在此时停止脚本执行。由于这是在脚本中打印任何HTML代码之前,这可能是导致错误的原因。