简体   繁体   English

HTML PHP电子邮件提交表单

[英]HTML PHP email submit form

I'm trying to create a PHP form that allows users to insert their email address and it automatically sends me an email with their email address. 我正在尝试创建一个PHP表单,该表单允许用户插入其电子邮件地址,并自动向我发送一封包含其电子邮件地址的电子邮件。 something like a subscription. 像订阅。

what I have so far is this: 我到目前为止所拥有的是:

<form  action="" method="post">
    <input type="email" name="email" placeholder="Enter your email address" /><br>
</form>

I found this PHP sample that I believe answers my problem, but I have no idea how to call it from my HTML. 我发现我相信这个PHP示例可以解决我的问题,但是我不知道如何从HTML调用它。

<?php
if(isset($_POST['email'])){
$email = $_POST['email'];
$to = 'myemail@something.com';
$subject = 'new subscriber';
$body = '<html> 
            <body>
                <p>Email:<br>'.$email.'</p>
            </body>
        </html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset-utf-8";

$send = mail($to, $subject, $body, $headers);
if($send){
    echo '<br>';
    echo 'thanks';
}else{
    echo 'error';
}
}
?>

There's insufficient code for me to be able to answer completely, but the one thing that comes immediately to my mind is not leaving action="" empty. 没有足够的代码让我无法完全回答,但是我立即想到的一件事就是没有将action=""空着。 Try $_SERVER['PHP_SELF'] variable, it should print the path to the file that is currently running so you'll be presented with the same page, but with data in $_POST you'll send. 尝试$_SERVER['PHP_SELF']变量,它应该将路径打印到当前正在运行的文件,这样您将看到相同的页面,但是将发送$_POST数据。 You can try it like this: 您可以这样尝试:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="email" name="email" placeholder="Enter your email address" /><br>
</form>

If you wish to send data to the same file like this, please make sure your PHP code is in the same file as the HTML structure of your form. 如果您希望像这样将数据发送到同一文件,请确保您的PHP代码与表单的HTML结构位于同一文件中。 It may make things easier if you put your PHP code first, so you can exit; 如果您首先放置PHP代码,可能会使事情变得更容易,从而可以exit; from the file (not displaying the form anymore) telling the user that the message has been sent or that the error has occured. 从文件(不再显示表单)中告诉用户消息已发送或错误已发生。

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

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