簡體   English   中英

無法定位問題.php 聯系頁面

[英]Cannot locate problem with .php Contact page

我相信這最終會成為一個愚蠢的問題,但請聽我說完。

我是一名新手開發人員,我正在使用 html、css(帶 Bootstrap)構建網站,並且由於該網站必須有聯系表單頁面,所以我在那里使用 php。 最初是在我開發網站頁面時,包括聯系頁面的前端。 我所有的頁面都是.html。 現在,按照在線教程,我已將 php 添加到 contact.html 以將內容中的聯系人發送到我選擇的 email,我已將文件名從 contact.html 更改為 contact.php 並且我已經放置了 ALL將網站文件放入我的 C 驅動器中 MAMP 的 htdocs 文件夾中,以便進行測試。 但是,由於切換到 contact.php,我無法查看該頁面。 相反,我收到 http 錯誤 500。請注意,所有 .html 文件都按應有的方式呈現。

毫無疑問,我在某個地方犯了一個基本的錯誤,但是非常感謝任何幫助解決這個問題的幫助。

<!DOCTYPE html>

<?php
$message_sent = 'false';
if(isset($_POST['email']) && $_POST['email'] != ''){

    if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){

        $userName = $_POST['name'];
        $userEmail = $_POST['email'];
        $message = $_POST['message'];
        $messageMark = $_POST['mark'];

        $to = "redacted_email@outlook.com";
        $body = "";

        $body .+ "From: ".$userName. "\r\n";
        $body .= "Email: ".userEmail. "\r\n";
        $body .= "Message: ".$message. "\r\n";
        $body .= "Marketing: ".$messageMark. "\r\n";

        mail($to, $body);

        $message_sent = true;
    }
}
?>

<html lang="en">
<main>
<?php
    if($message_sent):
?>

<h2>Thank you, we shall be in touch soon.</h2>

<?php
    else:
?>

<section id="contact">
<div class="container-lg">
    <div class="">
        <h1 class="title">Contact Us</h1> 
        <hr>
        <div class="row justify-content-center my-5">
            <div class="col-lg-6">
                <form class="form" name="form" autocomplete="off" method="POST" action="contactus.php">
                            
                    <label class="form-label" for="name" style="color: white;">Name:</label>
                    <input class="form-control mb-2" id="name" type="text" name="name" required>  
                                        
                    <label class="form-label mt-3" for="email" style="color: white;">Email: <span class="tooltip" data-tooltip="Please enter your email address">?</span></label>
                    <input class="form-control" id="email" type="email" placeholder="e.g. name:example.com" name="email" required>
                                            
                    <div class="form-floating mb-5  mt-5">
                        <textarea id="query" class="form-control" name="message" style="height: 250px; overflow-y: visible;"></textarea>
                        <label for="query" class="textlab">Type your message here: <span class="tooltip" data-tooltip="Type your message here">?</span></label>
                    </div>
                    
                    <div class="form-floating mb-5  mt-5">
                        <textarea id="query" class="form-control" name="mark" style="height: 100px;"></textarea>
                        <label for="query" class="textlab">How did you hear about us?:<span class="tooltip" data-tooltip="Just a few words on how you found our website and heard about our club">?</span></label>
                    </div>
                    <p>
                        <!--Instert recaptcha here? -->
                    </p>
                    <div class="mb-4 text text-center">
                        <button type="submit" id="conbtn" class="btn btn-light btn-lg btn-block">Send Now!</button>
                    </div>
                </form>
            </div>
        </div>  
    </div>
</div>
</section>
</main

為了清楚起見,這里是我的 contact.php 頁面中的全部代碼,它通過 MAMP 生成 HTTP ERROR 500 響應:

<!DOCTYPE html>

<?php
$message_sent = false;
if(isset($_POST['email']) && $_POST['email'] != ''){

    if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){

        $userName = $_POST['name'];
        $userEmail = $_POST['email'];
        $message = $_POST['message'];
        $messageMark = $_POST['mark'];

        $to = "troy_a_w_easson@outlook.com";
        $body = "";

        $body .= "From: ".$userName. "\r\n";
        $body .= "Email: ".$userEmail. "\r\n";
        $body .= "Message: ".$message. "\r\n";
        $body .= "Marketing: ".$messageMark. "\r\n";

        mail($to, $subject, $body):

        $message_sent = true;
    }
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/x-icon" href="images/LMRCupsc-removebg2.png">
<link 
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" 
rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"> 
</script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Beau+Rivage&display=swap" 
rel="stylesheet">
<link href='//fonts.googleapis.com/css?family=Signika+Negative:300,400,600' 
rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="CSS/contact.css">
<title>Largs Model Boat Club | Contact Us</title>
 </head>

<body>
<header>
    <nav class="navbar navbar-expand-sm navbar-dark">
        <div class="container-fluid">
            <h1>Largs Model Boat Club</h1>
            <a class="navbar-brand" href="index.html"><img src="images/LMRCupsc- 
   removebg2.png" alt="Largs Model Boat Club Logo" style="width:200px;"></a>
            <ul class="navbar-nav">
                  <li class="nav-item">
                    <a class="nav-link" href="gallery.html">Gallery</a>
                  </li>
                 <li class="nav-item">
                    <a class="nav-link" href="history.html">History</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="news.html">News & Events</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="aboutlargs.html">About Largs</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link active" href="contactus.php">Contact 
   Us</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="index.html">Home</a>
                  </li>
            </ul>
        </div>
    </nav>
</header>

<main>
<?php
    if($message_sent):
?>

<h2>Thank you, we shall be in touch soon.</h2>

<?php
    else:
?>

<section id="contact">
<div class="container-lg">
    <div class="">
        <h1 class="title">Contact Us</h1> 
        <hr>
        <div class="row justify-content-center my-5">
            <div class="col-lg-6">
                <form class="form" name="form" autocomplete="off" method="POST" 
 action="contactus.php">
                            
                    <label class="form-label" for="name" style="color: 
  white;">Name:</label>
                    <input class="form-control mb-2" id="name" type="text" 
  name="name" required>  
                                        
                    <label class="form-label mt-3" for="email" style="color: 
 white;">Email Address: <span class="tooltip" data-tooltip="Please enter your 
  email address">?</span></label>
                    <input class="form-control" id="email" type="email" 
  placeholder="e.g. name:example.com" name="email" required>

                    <label class="form-label" for="subject" style="color: 
  white;">Subject:</label>
                    <input class="form-control mb-2" id="subject" type="text" 
  name="subject"> 
                                            
                    <div class="form-floating mb-5  mt-5">
                        <textarea id="query" class="form-control" name="message" 
  style="height: 250px; overflow-y: visible;"></textarea>
                        <label for="query" class="textlab">Type your message 
 here: <span class="tooltip" data-tooltip="Type your message here">?</span> 
 </label>
                    </div>
                    
                    <div class="form-floating mb-5  mt-5">
                        <textarea id="query" class="form-control" name="mark" 
   style="height: 100px;"></textarea>
                        <label for="query" class="textlab">How did you hear 
  about us?:<span class="tooltip" data-tooltip="Just a few words on how you 
  found our website and heard about our club">?</span></label>
                    </div>
                    <p>
                        <!--Instert recaptcha here? -->
                    </p>
                    <div class="mb-4 text text-center">
                        <button type="submit" id="conbtn" class="btn btn-light 
   btn-lg btn-block">Send Now!</button>
                    </div>
                </form>
            </div>
        </div>  
    </div>
</div>
</section>

 <a href="https://en.wikipedia.org/wiki/Longship"><img 
 src="images/vikinggif.gif" id="vikinganim" alt="row, row, row your boat" 
  style="width: 300px; margin-left: 425px; margin-right: auto;"></a>

  </main>   

  <div class="bg-image"></div>  

  <footer>
<p id="foot_statement">Largs Model Boat Club &copy; 2022 | Designed & Developed 
by <a href="https://www.eassonweb.com" id="weblink"> EassonWeb.com</a></p><br>
<a href="https://en.wikipedia.org/wiki/International_maritime_signal_flags"><img 
src="images/LMBC_alphapng.png" id="LMBC_flags" style="width: 400px;"></a>
<div class="container-fluid">
    <ul class="nav navbar-expand-sm" id="footer-menu">
        <li class="nav-item">
            <a class="nav-link" href="gallery.html">Gallery</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="history.html">History</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="news.html">News & Events</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="aboutlargs.html">About Largs</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="index.html">Home</a>
        </li>
    </ul>
</div>
</footer>   
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  </body>
  
  </html>

UPD:這里的主要問題是mail function。查看文檔: https://www.php.net/manual/en/function.mail.php它需要 3 個參數:

string $to,
string $subject,
string $message

但是在您的代碼中只傳遞了 2 個參數: mail($to, $body);

錯別字:

您可以查看突出顯示的代碼並看到$body.= "Email: ".userEmail. "\r\n"; $body.= "Email: ".userEmail. "\r\n"; 看起來不同而不是它的鄰居。

userEmail缺少變量$ :)


$message_sent = 'false'; => $message_sent = false;


$body.+ "From: ".$userName. "\r\n"; => $body.= "From: ".$userName. "\r\n"; $body.= "From: ".$userName. "\r\n";

.+你必須在這一行中使用.=$body.+ "From: ".$userName. "\r\n"; $body.+ "From: ".$userName. "\r\n";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM