简体   繁体   English

使用PHP将表单数据发送到电子邮件

[英]Sending form data to E-mail using PHP

this is my first experience with PHP so sorry for the simple question. 这是我第一次使用PHP,对这个简单的问题感到抱歉。 I have a form in my website and I'm trying to send the user data to my E-mail (this means: when the user finish to fill the fields and click on "submit", I want to receive an E-mail with all the data he entered). 我的网站上有一个表格,我正在尝试将用户数据发送到我的电子邮件中(这意味着:当用户完成填写字段并单击“提交”时,我想收到一封电子邮件,其中包含他输入的所有数据)。 After sending the data, I want that instead of the form field and submit button, a message "Thank you for contact us" will appear. 发送数据后,我希望显示的是“谢谢您与我们联系”而不是表单字段和“提交”按钮。 So long I tried to do it this way: 很久以来,我试图通过这种方式做到这一点:

My form: 我的表格:

    <form id="contactForm" method="post" action="contact.php" onsubmit="return validateForm()">
            <input type="text" name="name" id="name" placeholder="Full Name">

            <input type="email" name="email" id="email" placeholder="E-mail Address">

            <input type="text" name="phone" id="phone" placeholder="Phone Number">

            <input type="text" name="company" id="company" placeholder="Company">

        <textarea name="message" id="message" placeholder="Write Your Message Here..."></textarea>

        <input type="submit" value="SEND" id="submitForm">
    <div id="formValidationAlerts">
</form>

My PHP code. 我的PHP代码。

<?php

if($_POST["submit"]) {
    $recipient="minorb@gmail.com";
    $subject="Message from Webtopia";
    $sender=$_POST["name"];
    $senderEmail=$_POST["email"];
    $senderPhone=$_POST["phone"];
    $senderCompany=$_POST["company"];
    $message=$_POST["message"];

    $mailBody="Name: $sender\nEmail: $senderEmail\nPhone: $senderPhone\nCompany: $senderCompany\n\n$message";

    mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

$thankYou="<p>Thank you! Your message has been sent.</p>";
}

?>

How can I continue from here? 我怎样才能从这里继续? So long, when I clicked on submit I just move to the contact.php page and see my PHP code at the browser as plain text. 很长一段时间,当我单击“提交”时,我仅移至contact.php页面,并在浏览器中以纯文本形式看到了我的PHP代码。

Thanks 谢谢

The problem isn't in your code, but in the server set-up. 问题不在于您的代码,而在于服务器设置。 The PHP code not being run means either HTTP server (Apache, nginx, etc.) or PHP server misconfiguration. PHP代码未运行意味着HTTP服务器(Apache,nginx等)或PHP服务器配置错误。 Look for advices on running MAMP. 寻找有关运行MAMP的建议。

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

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