联系人表单在删除iframe后在浏览器中显示PHP


Contact form displays PHP in browser after removing iframe

我想重用/重新设计以前使用过的php表单,但使用了一个调用php文件的iframe。我被告知这是不好的做法,并停止使用iframes;但是,当我将php移到html页面时,php代码会显示在表单的文本字段中。

我在这里检查了几页php线程,问题似乎通常是忘记关闭php标记或主机不支持php。我没有从工作版本更改php本身,我的主机支持php 5.3.6版本。

编辑:将文件更改为.php会清除浏览器中的php,但表单不会发送。

文本字段中显示的PHP如下:

<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>
<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>
<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?>

Contact.html页面代码:

    <div id="contact-form" class="clearfix">
<?php session_start(); ?>
<?php
include("spam_check.php");
?>
            <h2> You can contact us by filling out the form below.</h2>
            <?php
            //init variables
            $cf = array();
            $sr = false;
            if(isset($_SESSION['cf_returndata'])){
                $cf = $_SESSION['cf_returndata'];
                $sr = true;
            }
            ?>
            <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
                <li id="info">There were some problems with your form submission:</li>
                <?php 
                if(isset($cf['errors']) && count($cf['errors']) > 0) :
                    foreach($cf['errors'] as $error) :
                ?>
                <li><?php echo $error ?></li>
                <?php
                    endforeach;
                endif;
                ?>
<?php
if (isset($_POST['real'])) {
  // error condition, redisplay form
}
?>
            </ul>
            <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for your message! We will get back to you ASAP!</p>
            <form method="post" action="process.php">
                <label for="name">Name: <span class="required">*</span></label>
                <input type="text" id="name" name="name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" placeholder="Your Name" required autofocus />
                <label for="email">Email: <span class="required">*</span></label>
                <input type="email" id="email" name="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" placeholder="yourname@example.com" required />
                <label for="message">Message: <span class="required">*</span></label>
                <textarea id="message" name="message" placeholder="Your message must be greater than 20 characters." required data-minlength="20"><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>
<label style="display:block;position:absolute;left:-9999px" class="real">
  Please leave this checkbox blank
  <input type=checkbox name=real value=1>
</label>
                <span id="loading"></span>
                <input type="submit" value="SEND" id="submit-button" />
                <p id="req-field-desc"><span class="required">*</span> indicates a required field</p>          
            </form>
            <?php unset($_SESSION['cf_returndata']); ?>
        </div>

Process.php:

<?php
if( isset($_POST) ){
    //form validation vars
    $formok = true;
    $errors = array();
    //sumbission data
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $date = date('d/m/Y');
    $time = date('H:i:s');
    //form data
    $name = $_POST['name']; 
    $email = $_POST['email'];
    $message = $_POST['message'];
    //validate form data
    //validate name is not empty
    if(empty($name)){
        $formok = false;
        $errors[] = "You have not entered a name";
    }
    //validate email address is not empty
    if(empty($email)){
        $formok = false;
        $errors[] = "You have not entered an email address";
    //validate email address is valid
    }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $formok = false;
        $errors[] = "You have not entered a valid email address";
    }
    //validate message is not empty
    if(empty($message)){
        $formok = false;
        $errors[] = "You have not entered a message";
    }
    //validate message is greater than 20 charcters
    elseif(strlen($message) < 20){
        $formok = false;
        $errors[] = "Your message must be greater than 20 characters";
    }
    //send email if all is ok
    if($formok){
        $headers = "From: name@email.com" . "'r'n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
        $emailbody = "<p>You have received a new message from the form on your website.</p>
                      <p><strong>Name: </strong> {$name} </p>
                      <p><strong>Email Address: </strong> {$email} </p>
                      <p><strong>Message: </strong> {$message} </p>
                      <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
        mail("name@email.com","New Enquiry",$emailbody,$headers);
    }
    //what we need to return back to our form
    $returndata = array(
        'posted_form_data' => array(
            'name' => $name,
            'email' => $email,
            'telephone' => $telephone,
            'enquiry' => $enquiry,
            'message' => $message
        ),
        'form_ok' => $formok,
        'errors' => $errors
    );

    //if this is not an ajax request
    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
        //set session variables
        session_start();
        $_SESSION['cf_returndata'] = $returndata;
        //redirect back to form
        header('location: ' . $_SERVER['HTTP_REFERER']);
    }
}
?>

查看页面源代码-其他PHP代码也在那里吗?如果php不能正常工作,那么所有的代码都会显示出来,而不仅仅是表单字段中的一些位。

你说你把它移到了一个"html文件"里。那是"somefile.html"吗?请记住,通常情况下,网页服务器不会将.html设置为由PHP解析。尝试将其重命名为"somefile.php"。