初始化JavaScript脚本中的联系人表单 [英] initialize javascript in php script for a contact form
问题描述
< form action =mail.phpmethod =POST >
< input type =textid =namename =nameplaceholder =Namerequired>< br />
< input type =textid =emailname =emailplaceholder =Mailrequired>< br />
< button name =submittype =submitid =submit> send< / button>
< / form>
用一个简单的php邮件程序
<?php
$ name = $ _POST ['name'];
$ email = $ _POST ['email'];
$ message = $ _POST ['message'];
$ formcontent =Von:$ name \\\
Nachricht:$ message;
$ recipient =mail@mail.com;
$ subject =$ betreff;
$ mailheader =Von:$ email \r\\\
;
mail($ recipient,$ subject,$ formcontent,$ mailheader)或者die(Error!);
回声谢谢你! //需要删除;
?>
使用简单的javascript将按钮更改为'发送'
var send = document.getElementById('submit');
if(send){
send.onclick = function(){
this.innerHTML ='sending';
是否可以将按钮innerhtml更改为发送
PHP脚本成功完成后?另外我该如何避免重定向到mail.php?
首先它应该显示 send
。点击发送
后应该会出现,并且当mail.php不会发送时,应该出现在按钮中。
任何想法或建议?
最好的问候
dennym
编辑:进一步的进展和解决方案联系表单错误通过jquery提交ajax
是否有可能将按钮innerhtml更改为在PHP
脚本已成功完成?
如果您重新加载相同的模板,您可以将按钮呈现为发送
。你必须跟踪表单的状态,尽管在后端。 YOu可以改变你的表单,通过JavaScript使用Ajax提交。收到成功的回应后,您可以更改innerHTML。
另外我怎样才能避免重定向到mail.php?
请勿提交表单。使用javascript收集值并发出ajax请求。
Hi there working on a simple contact form.
<form action="mail.php" method="POST">
<input type="text" id="name" name="name" placeholder="Name" required><br />
<input type="text" id="email" name="email" placeholder="Mail" required><br />
<textarea name="message" placeholder="Nachricht" required></textarea><br />
<button name="submit" type="submit" id="submit">send</button>
</form>
with a simple php mailer
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="Von: $name \n Nachricht: $message" ;
$recipient = "mail@mail.com";
$subject = "$betreff";
$mailheader = "Von: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" //need to be removed;
?>
Im using a simple javascript for changing the button to 'sending'
var send = document.getElementById('submit');
if(send) {
send.onclick = function () {
this.innerHTML = 'sending';
}
}
Is it possible to change the button innerhtml to sent
after the php script is successfully done? Also how can I avoid the redirection to the mail.php?
First it should show the send
. After clicking sending
should appear and when the mail.php is dont sent
should appear in the button.
Any ideas or suggestions?
best regards dennym
Edit: Further Progress and solution error on contact form submit via jquery ajax
Is it possible to change the button innerhtml to sent after the php script is successfully done?
If you reload the same template you can have the button rendered as sent
. You have to track the state of the form though on the backend. YOu could change your form to submit through javascript using ajax. After a successful response is received you can change the innerHTML.
Also how can I avoid the redirection to the mail.php?
Don't submit the form. Use javascript to collect values and make an ajax request.
这篇关于初始化JavaScript脚本中的联系人表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!