提交按钮以下载PHP文件,而不是以联系表的形式将数据发送到电子邮件

问题描述

我正在尝试为我的网站制作联系表单,但是当我按Submit时,将下载PHP文件而不是运行它。我正在使用Chrome。我认为存在语法错误,但是我一直在删除添加和填充内容,即使没有语法错误,它仍会下载文件而不是运行它,是的...这是PHP的确切名称文件

HTML代码

<div class="form1" id="form" data-aos="fade-down" data-aos-once="true" data-aos-delay="200">
        <div class="row">
            <h2>We're happy to hear from you !</h2>
        </div>
        <form class="form" method="POST" action="form.PHP">
            <div class="contact-us">
                <h2>Contact Us !</h2>
            </div><hr>
            <div class="parent">
                <div class = "Name">
                    <label for = "fname">First Name:</label>
                    <input type = "text" maxlength="15" name = "fname"   placeholder="First name" required>
                </div>
                <div class = "Name">
                    <label for = "lname">Last Name:</label>
                    <input type = "text" maxlength="20" name = "lname"  placeholder="Second name" required>
                </div>
            </div>
                
            <div class = "email">
                <label for = "email">Email:<br></label>
                <input type = "email" minlength="8" name = "email"  placeholder="Your email" required>
            </div>
            
            <p class = "comment">
                <label for="message">Comments:<br></label>
                <textarea id="message" name="message" cols="10" rows="5" placeholder="Your Message"></textarea>
            </p>
            <div class="submit">
                <input  type="submit" class="submit-btn"  value="Send Us"  >
            </div>
        </form>

PHP代码

<?PHP
    if(isset($_POST['submit']))
    {
        $fname = $_POST['fname'];// Get fName value from HTML Form
        $lname = $_POST['lname']; // Get lName value from HTML Form
        $email_id = $_POST['email']; // Get Email Value
        $msg = $_POST['message']; // Get Message Value
        $to = "[email protected]"; // You can change here your Email
        $subject = "'$name' has been sent a mail"; // This is your subject
         
        // HTML Message Starts here
        $message ="
        <html>
            <body>
                <table style='width:600px;'>
                    <tbody>
                        <tr>
                            <td style='width:150px'><strong>FName: </strong></td>
                            <td style='width:400px'>$fname</td>
                        </tr>
                        <tr>
                            <td style='width:150px'><strong>LName: </strong></td>
                            <td style='width:400px'>$lname</td>
                        </tr>
                        <tr>
                            <td style='width:150px'><strong>Email ID: </strong></td>
                            <td style='width:400px'>$email_id</td>
                        </tr>
                        <tr>
                            <td style='width:150px'><strong>Message: </strong></td>
                            <td style='width:400px'>$msg</td>
                        </tr>
                    </tbody>
                </table>
            </body>
        </html>
        ";
        // HTML Message Ends here
         
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
 
        // More headers
        $headers .= 'From: Admin <[email protected]>' . "\r\n"; // Give an email id on which you want get a reply. User will get a mail from this email id
         
        if(mail($to,$subject,$message,$headers)){
            // Message if mail has been sent
            echo "<script>
                    alert('Mail has been sent Successfully.');
                </script>";
        }
 
        else{
            // Message if mail has been not sent
            echo "<script>
                    alert('EMAIL Failed');
                </script>";
        }
    }
?>

解决方法

如果可以将其添加到您的apache config文件中

AddType application/x-httpd-php .php 

那将解决您的问题

如何--https://www.phusionpassenger.com/library/install/apache/working_with_the_apache_config_file.html

还要确保您在服务器环境(如wamp或xamp)上运行代码。如果您离线工作。