0

我想重用/重新设计我以前使用过的 php 表单,但使用了一个 iframe 来调用 php 文件。我被告知这是不好的做法并停止使用 iframe;但是,当我将 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>

进程.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']);
    }
}
?>
4

1 回答 1

0

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

您说您将其移至“html 文件”。那是“somefile.html”吗?请记住,通常 .html 不设置为由 Web 服务器由 PHP 解析。尝试将其重命名为“somefile.php”。

于 2012-08-05T00:02:09.450 回答