我正在尝试为我的网站创建这个简单的电子邮件订阅表单。每次添加电子邮件时,我都希望输入框确认电子邮件已添加。这看起来很简单,但我无法动态地在输入框中回答答案。 我知道如何使用Javascript更改输入值,但它似乎没有包含在PHP中。
编辑:我对PHP不是很熟悉。我知道我不应该在我的函数中包含Javascript,它只是我想要实现的一个例子。
有更简单的方法吗?
谢谢!
<div id="email_form">
<form action="" method="post">
<input name="email" class="email" type="text" placeholder="Subscribe" id="emailinput"><button type="submit" name="SubmitButton" class="btn_email"><b>></b></button>
</form>
</div>
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$to = "myemail@email.com";
$from = "no-reply@email.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
echo '<script type="text/javascript">';
echo 'var elem = document.getElementById("emailinput");';
echo 'elem.value = "Submited!";';
echo '</script>';
}
else
{
echo 'It works but There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'It kinda works but there was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
?>
答案 0 :(得分:1)
在回复表单之前,请对提交内容进行处理。然后,如果电子邮件处理成功,当您回显输入框时,使用属性值=&#34;成功回显您的输入!&#34;
答案 1 :(得分:0)
谢谢大家,把PHP放在第一位并回应价值。我实际上是在使用占位符而不是值,所以它不可编辑。
首次在Stackoverflow上提问,绝对不会让这个伟大的社区感到失望。
以下是我的最终参考代码:
<?php
if ( isset($_POST['SubmitButton']) ) {
$to = "myemail@email.com";
$from = "no-reply@email.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
$message = "";
$value = "";
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
if (mail($to, $subject, $body, $headers, "-f " . $from)) {
$value = "Submited!";
} else {
$value = "Invalid Email";
}
} else {
$value = "Invalid Email";
}
}
?>
<div id="email_form">
<form action="" method="post">
<input name="email" class="email" type="text" id="emailinput" placeholder="<?php echo $value; ?>" />
<button type="submit" name="SubmitButton" class="btn_email">
<b>></b>
</button>
</form>
</div>