簡體   English   中英

為什么我的 PHP 代碼不起作用,HTML 網站?

[英]Why is my PHP Code not Working , HTML website?

好的,所以我為一個學校項目創建了一個 HTML 網站,我嘗試在其中加入一個“聯系我們”部分,您在其中提供您的姓名 email 和一條要發送到我地址的消息。 我似乎無法在代碼中找到任何錯誤。 當我嘗試發送信息時,它沒有執行 php,而是打開一個新的白色 Web 頁面並且沒有發送郵件。

**這就是頁面的外觀以及我提交時會發生什么: https://imgur.com/a/1mLaIa4 **(您可以看到文件位於同一文件夾中,所以這不是問題)

那是我的代碼HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Contact Page</title>
<link rel="stylesheet" href="style2.css" type="text/css" media="screen" />
<link rel="icon" href="icon2.png" type="image/x-icon"/>

</head>
<body>


<div class="container">
  <div style="text-align:center">
    <h2>Contact Us</h2>
    <p>Leave a message or a suggestion:</p>
  </div>
  <div class="row">
    <div class="column">
      <img src="gif3.gif" style="width:100%;">
    </div>
    <div class="column">
      <form id="column-form" method="post" action="column-form-handler.php">
        <label for="fullname">Full Name</label>
        <input type="text" id="fullname" name="fullname" placeholder="Your full name.." required>
        
        <label for="email">Email</label>
        <input type="email" id="email" name="email" placeholder="Your email adress.." required>
       
       <label for="country">Country</label>
        <select id="country" name="country" >
          <option value="australia">Australia</option>
          <option value="canada">Canada</option>
          <option value="usa">USA</option>
        </select>
        
        <label for="subject">Subject</label>
        <textarea id="subject" name="subject" placeholder="Write something.." style="height:170px"></textarea>
        <input type="submit" value="Submit">
      </form>
    </div>
  </div>
</div>

</body>
</html>

這是PHP

<?php
     
    $name = $_POST['fullname'];
    $visitor_email = $_POST['email'];
    $message = $_POST['subject'];
    
    $email_from = 'abcdefgh@gmail.com';
    $email_subject = "Website Messages";
    $email_body = "User Name: $name.\n".
                     "User Email: $visitor_email.\n".
                       "User Message: $message.\n";
                       
    $to = "efghhieig@gmail.com";
    $headers = "From: $email_from \r\n";
    $headers = "Reply-To: $visitor_email \r\n";
    mail($to,$email_subject,$email_body,$headers);
    header("Location: contact.html");
    
    
    ?>

當您通過file://將頁面加載到瀏覽器中時,PHP 解釋器將不會執行 PHP 腳本。

You need a webserver running on your machine - eg Apache or IIS - which is configured to serve PHP files, and then you need to go to the page in your browser like http://localhost/contact.html so that when you submit the形式,它最終會發布到http://localhost/column-form-handler.php (而不是file://column-form-handler.php )。

這樣做意味着網絡服務器會將對 .php 文件的請求傳遞給 PHP 解釋器,解釋器將執行它們,並將執行代碼的結果(而不僅僅是原始源代碼)傳遞回網絡服務器,然后將其返回給客戶端(即瀏覽器)。

暫無
暫無

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

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