0

我刚刚安装了一个新的 PHP 联系表单,但它不起作用。我已经尝试修复它,但我不知道如何修复它。有人可以看看它并给我一个修复吗?PHP代码:

      $to = 'email@hotmail.com';
      $subject = 'Contact';
      $contact_submitted = 'Your message has been sent.';


      function email_is_valid($email) {
        return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i',$email);
      }
      if (!email_is_valid($to)) {
        echo '<p style="color: red;">You must set-up a valid (to) email address before this contact page will work.</p>';
      }
      if (isset($_POST['contact_submitted'])) {
        $return = "\r";
        $youremail = trim(htmlspecialchars($_POST['your_email']));
        $yourname = stripslashes(strip_tags($_POST['your_name']));
        $yourmessage = stripslashes(strip_tags($_POST['your_message']));
        $contact_name = "Name: ".$yourname;
        $message_text = "Message: ".$yourmessage;
        $user_answer = trim(htmlspecialchars($_POST['user_answer']));
        $answer = trim(htmlspecialchars($_POST['answer']));
        $message = $contact_name . $return . $message_text;
        $headers = "From: ".$youremail;
        if (email_is_valid($youremail) && !eregi("\r",$youremail) && !eregi("\n",$youremail) && $yourname != "" && $yourmessage != "" && substr(md5($user_answer),5,10) === $answer) {
          mail($to,$subject,$message,$headers);
          $yourname = '';
          $youremail = '';
          $yourmessage = '';
          echo '<p style="color: blue;">'.$contact_submitted.'</p>';
        }
        else echo '<p style="color: red;">Please enter your name, a valid email address, your message and the answer to the simple maths question before sending your message.</p>';
      }
      $number_1 = rand(1, 9);
      $number_2 = rand(1, 9);
      $answer = substr(md5($number_1+$number_2),5,10);
    ?>
    <form id="contact" action="contact.php" method="post">
      <div class="form_settings">
        <p><span>Name</span><input class="contact" type="text" name="your_name" value="<?php echo $yourname; ?>" /></p>
        <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="<?php echo $youremail; ?>" /></p>
        <p><span>Message</span><textarea class="contact textarea" rows="5" cols="50" name="your_message"><?php echo $yourmessage; ?></textarea></p>
        <p style="line-height: 1.7em;">To help prevent spam, please enter the answer to this question:</p>
        <p><span><?php echo $number_1; ?> + <?php echo $number_2; ?> = ?</span><input type="text" name="user_answer" /><input type="hidden" name="answer" value="<?php echo $answer; ?>" /></p>
        <p style="padding-top: 15px"><span>&nbsp;</span><input class="submit" type="submit" name="contact_submitted" value="send" /></p>
      </div>
    </form>`

有人可以帮助我吗?我得到的错误:http: //i50.tinypic.com/2hfpe7m.jpg

4

2 回答 2

1

PHP 命令显示在您的页面中这一事实清楚地表明 PHP 要么没有安装,要么没有正确配置。确保它已启动并正在运行,并且您的文件具有.php扩展名。

您的源代码也缺少开头的<?php. 它可能只是您的问题中缺少的,但请确保它存在于您的文件中。

于 2013-02-01T13:33:30.923 回答
0

您缺少开始的 PHP 标记。

只需添加:

<?php

在脚本的顶部。

于 2013-02-01T13:47:06.033 回答