如何发送表单而不刷新页面? [英] How to send a form without refreshing the page?
问题描述
我不知道PHP。
我不希望用户转到 http://www.example.com /feedback-thanks.html
发送表单后。我想要的是发送按钮下方的文本或任何不刷新页面。
我删除了 header(Location :http://www.example.com/feedback-thanks.html);
但我没有收到电子邮件,用户被重定向到feedback.php ... :(
html
< form method =postaction =feedback.php>
< input name =emailtype =email/>
< textarea name =messageid =feedback -textareaautofocus =autofocusrequired =required>< / textarea>
< / form>
feedback.php
<?php
$ email = $ _REQUEST ['email'];
$ message = $ _REQUEST [消息'];
mail(abcde@gmail.com,Subject Here,
$ message,From:$ email);
header(Location :http://www.example.com/fee dback-thanks.html);
?>
您必须使用 $ .ajax()
为此
这是我在我的一个应用程序
$('#submitSearch')。click(function(){
var formData = {
searchData:$('#searchData').val(),
};
$ .ajax({
url:siteUrl +' fetch / search',
type:POST,
data:formData,
cache:true,
beforeSend:function(){
jQuery('#main ')的.html(' < d iv class =loading>< img src ='+ siteUrl +'resources / imgs / ajax-loader.gifalt =正在加载.../>< / div>');
},
success:function(data){
jQuery('#main')。empty();
jQuery('#main')。append(data);
},
error:function(x,e){
if(x.status == 0){
alert('您正在离线!! \\\
请检查你的网络。');
} else if(x.status == 404){
alert('Requested URL not found。');
} else if(x.status == 500){
alert('Internel Server Error。');
} else if(e =='parsererror'){
alert('Error.\\\
Parsing JSON Request failed。');
} else if(e =='timeout'){
alert('Request Time out。');
} else {
alert('Unknow Error.\\\
'+ x.responseText);
}
}
});
返回false;
});
I don't know PHP.
I don't want the user to go to http://www.example.com/feedback-thanks.html
after sending the form. What I want is text below the sent button or anything without refreshing the page.
I removed header( "Location: http://www.example.com/feedback-thanks.html" );
but i don't receive the e-mail and the user is redirected to feedback.php... :(
html
<form method="post" action="feedback.php">
<input name="email" type="email"/>
<textarea name="message" id="feedback-textarea" autofocus="autofocus" required="required" ></textarea>
<button type="submit" id="feedback-button-send">send</button>
</form>
feedback.php
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
mail( "abcde@gmail.com", "Subject Here",
$message, "From: $email" );
header( "Location: http://www.example.com/feedback-thanks.html" );
?>
You will have to use $.ajax()
for that
- http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
- http://www.sitepoint.com/ajax-jquery/
this is what I use in one of my apps
$('#submitSearch').click(function() {
var formData = {
searchData: $('#searchData').val(),
};
$.ajax({
url: siteUrl + 'fetch/search',
type: "POST",
data: formData,
cache: true,
beforeSend:function(){
jQuery('#main').html('<div class="loading"><img src="' + siteUrl + 'resources/imgs/ajax-loader.gif" alt="Loading..." /></div>');
},
success: function(data) {
jQuery('#main').empty();
jQuery('#main').append(data);
},
error:function(x,e){
if(x.status==0){
alert('You are offline!!\n Please Check Your Network.');
}else if(x.status==404){
alert('Requested URL not found.');
}else if(x.status==500){
alert('Internel Server Error.');
}else if(e=='parsererror'){
alert('Error.\nParsing JSON Request failed.');
}else if(e=='timeout'){
alert('Request Time out.');
}else {
alert('Unknow Error.\n'+x.responseText);
}
}
});
return false;
});
这篇关于如何发送表单而不刷新页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!