我正在尝试为我的网站创建简单的电子邮件订阅表单。每次添加电子邮件时,我都希望输入框确认电子邮件已添加。这看起来很简单,但我找不到在输入框中动态回显答案的方式。 我知道如何用Javascript改变输入值,但它似乎并没有工作,一旦包含在PHP中。使用PHP和Javascript更改输入值
编辑:我不是很熟悉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 = "[email protected]";
$from = "[email protected]";
$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'] . ')';
}
}
?>
与PHP相呼应的JavaScript似乎只是如此优雅,它的网站应该建的方式。 – adeneo
我知道这不是正确的做法,这就是为什么我要求帮助。 – Skaddict
[**丁,我们活着服务**](http://jsfiddle.net/3T2b6/) – adeneo