简单的 PHP 联系表

问题描述 投票:0回答:1

我需要一些帮助。大约十年前,我为网站联系表单编写了一个不错的 PHP 邮件脚本。显然,它已经过时了,现在在某些地方不起作用。我需要一些帮助来弄清楚为什么这个新的开始根本不起作用。它甚至没有显示基本页面,错误,什么也没有,只是一个白屏。现在所有内容都在这一页上,我将在接下来附上。请告诉我你看到了什么!谢谢!

<?php

$errors = [];
$errorMessage = '';

if (!empty($_POST)) {
   $name = $_POST['name'];
   $email = $_POST['email'];
   $message = $_POST['message'];

   if (empty($name)) {
       $errors[] = 'Name is empty';
   }

   if (empty($email)) {
       $errors[] = 'Email is empty';
   } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $errors[] = 'Email is invalid';
   }

   if (empty($message)) {
       $errors[] = 'Message is empty';
   }

   if (empty($errors)) {
       $toEmail = '[email protected]';
       $emailSubject = 'New email from your contact form';
       $headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=utf-8'];
       $bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
       $body = join(PHP_EOL, $bodyParagraphs);

       if (mail($toEmail, $emailSubject, $body, $headers)) {

           $errorMessage = "Thanks! We'll get right back to you!";
       } else {
           $errorMessage = 'Oops, something went wrong. Please try again later';
       }

   } else {

       $allErrors = join('<br/>', $errors);
       $errorMessage = "<p style='color: red;'>{$allErrors}</p>";
   }
}

?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Ebony and Ivory Music Studio - General Contact</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="fb:app_id" content="">
<meta property="og:title" content="Ebony and Ivory Music Studio - General Contact">
<meta property="og:type" content="website">
<meta property="og:image" content="http://ebonyandivorymusic.org/images/1897.jpg">    
<meta property="og:url" content="http://ebonyandivorymusic.org">
<meta property="og:description" content="Barbara Roth, of Cincinnati, Colerain, Groesbeck, Cheviot, Montfort Heights, Anderson, Clermont, Batavia, Price Hill, Ohio, piano and violin teacher with 50 years experience.">
<meta name="description" content="Barbara Roth, of Cincinnati, Colerain, Groesbeck, Cheviot, Montfort Heights, Anderson, Clermont, Batavia, Price Hill, Ohio, piano and violin teacher with 50 years experience.">
<meta name="author" content="Nathan Roth" >
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Custom styles for this template -->
<link href="css/newnav86.css" rel="stylesheet">
<link href="css/w38.css" rel="stylesheet">
<style>
body,h1,h2,h3,h4,h5,h6 {font-family: "Lato", sans-serif}
.w3-bar,h1,button {font-family: "Montserrat", sans-serif}
.fa-anchor,.fa-coffee {font-size:200px}
        form { width: 100%; }
        form p { margin: 0px 0px 25px 20px; }
        label {
            display: block;
            font-family: "Calibri", "Times New Roman", Times, serif;
            font-size: 1em;
            line-height: 1.421875em;
            color: #FEF8E6;
        }
        textarea {
            width: 380px;
            height: 150px;
        }
        @media screen and (max-width: 769px) { textarea {
            width: 240px;
            height: 120px;
        } }
        .textInput { width: 300px; }
        @media screen and (max-width: 769px) { .textInput { width: 125px; } }
        .sendButton { border:1px solid #000000!important; color: #FEF8E6; background-color: #666666; }
        .sendButton:hover { border:1px; color:#000000; background-color: #FEF8E6; }
        .warning {
            font-family: "Calibri", "Times New Roman", Times, serif;
            font-size: 1em;
            line-height: 1.21875em;
            font-weight: bold;
            color: #FF0000;
        }
        .success {
            font-family: "Calibri", "Times New Roman", Times, serif;
            font-size: 1em;
            line-height: 1.21875em;
            font-weight: bold;
            color: #6E1410;
        }
        .welcome {
            font-family: "Calibri", "Times New Roman", Times, serif;
            font-size: 1.3em;
            line-height: 1.7875em;
            font-weight: bold;
            color: #FEF8E6;
        }
        form .website{ display:none; } /* hide because is spam protection */
</style>
<script>google recaptcha script</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-89258336-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-xxxxxxx-1');
</script>


</head>

<body>
    
<!-- Navbar -->
<?php include("includes/newnavnavigation.inc.php"); ?>

<div class="content">
<!-- Header -->
<header id="header" class="w3-container w3-center">
  <h1 class="w3-jumbo"><img src="images/header.jpg" alt="Ebony and Ivory Music Studio Header Image"></h1>
</header>

<div class="w3-row-padding w3-padding-64 w3-container" style="margin-top: -60px; margin-left: 0px;">
  <div class="w3-content" style="margin-left: 0px;">
    <div class="w3-twothird" style="margin-left: 0px;">
    
            <h2>Thank you for visiting! Ask a general question or contact me to set up an introductory lesson!</h2>
            
            <?php
            if (!empty($errors)) {
               $allErrors = join('<br/>', $errors);
               $errorMessage = "<p style='color: red;'>{$allErrors}</p>";
            }
            echo((!empty($errorMessage)) ? $errorMessage : '')
            ?>
            
            <form method="post" name="contact" id="contact" class="w3-container w3-card-4">
            <p><input name="website" type="text" class="website"></p>
              <p>
                <label for="name">Name:</label>
                <input placeholder="Enter your name" name="name" type="text" value="<?= $inputs['name'] ?? '' ?>" class="textInput" id="name">
              </p>
              <p>
                <label for="email">Email:</label>
                <input placeholder="Enter your email" name="email" type="text" value="<?= $inputs['email'] ?? '' ?>" class="textInput" id="email">
              </p>
              <p>
                <label for="message">Message:</label>
                <textarea placeholder="Write your message" name="message" id="question" cols="25" rows="5"><?= $inputs['message'] ?? '' ?></textarea>
              </p>

            <label for="nickname" aria-hidden="true" class="website"> Nickname
            <input type="text" name="nickname" id="nickname" class="website" tabindex="-1" autocomplete="off">
            </label>

              <p>
                <input type="submit" name="send" id="send" class="sendButton" value="Click to Send Message">
              </p>
            </form>
            
            <br><br>
            
            <img class="centerimage" src="images/facebook_1475422948378.jpg" alt="Image of Barb Teaching">
            
            </div>

    <div class="w3-third w3-center" style="margin-top: 60px;">
      <span style="margin-left: 0px;"><a href="https://lessons.com/oh/cincinnati/violin-lessons/" title="lessons.com" target="_blank"><img src="//cdn.lessons.com/assets/images/tmp/lessons-2018.png" width="100" alt="lessons.com"></a></span><br><br> 
                    <img style="width: 70%; border: 2px solid #6E1410;" src="images/1884.jpg" alt="Headshot for Contact Webpage">
    </div>
  </div>
</div>

<!-- Footer -->
<?php include("includes/newnavfooter.inc.php"); ?>

</div>
 <script src="//cdnjs.cloudflare.com/ajax/libs/validate.js/0.13.1/validate.min.js"></script>
 <script>


     const constraints = {
         name: {
             presence: { allowEmpty: false }
         },
         email: {
             presence: { allowEmpty: false },
             email: true
         },
         message: {
             presence: { allowEmpty: false }
         }
     };

     const form = document.getElementById('contact');
     form.addEventListener('submit', function (event) {

         const formValues = {
             name: form.elements.name.value,
             email: form.elements.email.value,
             message: form.elements.message.value
         };


         const errors = validate(formValues, constraints);
         if (errors) {
             event.preventDefault();
             const errorMessage = Object
                 .values(errors)
                 .map(function (fieldValues) {
                     return fieldValues.join(', ')
                 })
                 .join("\n");

             alert(errorMessage);
         }
     }, false);
 </script>
<script>
// Used to toggle the menu on small screens when clicking on the menu button
function myFunction() {
    var x = document.getElementById("navDemo");
    if (x.className.indexOf("w3-show") == -1) {
        x.className += " w3-show";
    } else { 
        x.className = x.className.replace(" w3-show", "");
    }
}
</script>

</body>
</html>

这发生在一个免费托管平台(000webhost)上,该平台由于多种原因而臭名昭著。我猜他们声称他们全面强制执行 PHP v8,这就是破坏我原创的原因。不过有趣的是,我在 GoDaddy 的多个地方使用了原始的表单/脚本,它在那里仍然工作得很好...现在迁移到这个主机上的另一个免费主机(奖励空间),它使用户能够从 V5 等版本降级 php。我只需要让一些东西发挥作用...

php html forms email contact-form
1个回答
-1
投票

免费托管平台 (000webhost) 全面强制执行 PHP v8,这就是破坏您原始代码的原因。迁移到另一个免费主机

© www.soinside.com 2019 - 2024. All rights reserved.