简体   繁体   English

为什么我的 PHP 代码不起作用,HTML 网站?

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

Ok so i made a HTML website for a school project and i tried incorporating into it a " Contact Us " section in which u give your name, email and a message to be sent to my adress.好的,所以我为一个学校项目创建了一个 HTML 网站,我尝试在其中加入一个“联系我们”部分,您在其中提供您的姓名 email 和一条要发送到我地址的消息。 I can't seem to find any error into the code.我似乎无法在代码中找到任何错误。 When i am trying to send the info, instead of executing the php, it opens a new white Web Page and no mail is sent.当我尝试发送信息时,它没有执行 php,而是打开一个新的白色 Web 页面并且没有发送邮件。

**That's how the Page looks like and what happens when i Submit: https://imgur.com/a/1mLaIa4 ** ( as u can see the files are located in the same folder so that's not a problem ) **这就是页面的外观以及我提交时会发生什么: https://imgur.com/a/1mLaIa4 **(您可以看到文件位于同一文件夹中,所以这不是问题)

That's my code HTML :那是我的代码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>

And here is the PHP :这是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");
    
    
    ?>

When you load the pages into your browser via file:// , the PHP script will not be executed by the PHP interpreter.当您通过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 form, it would end up posting to http://localhost/column-form-handler.php (instead of file://column-form-handler.php or whatever). 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 )。

Doing this means that the webserver will pass requests for.php files to the PHP interpreter, which will execute them, and pass the result of executing the code (as opposed to just the raw source code) back to the webserver which then returns it to the client (ie browser).这样做意味着网络服务器会将对 .php 文件的请求传递给 PHP 解释器,解释器将执行它们,并将执行代码的结果(而不仅仅是原始源代码)传递回网络服务器,然后将其返回给客户端(即浏览器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM