带有jquery风格的php联系人表单
这是我的第一篇文章,非常新。我有一个联系人表单,它通过按一个按钮以jquery打开。当我使用php提交此表单时,该框再次关闭,因此您只能看到“查看表单”按钮。当我点击此按钮时,方框/表格再次打开,尽管表格已提交-我希望方框保持打开状态,以显示“感谢您提交etc”消息-任何帮助都将不胜感激。谢谢以下是我的jquery和php:带有jquery风格的php联系人表单,php,jquery,forms,post,Php,Jquery,Forms,Post,这是我的第一篇文章,非常新。我有一个联系人表单,它通过按一个按钮以jquery打开。当我使用php提交此表单时,该框再次关闭,因此您只能看到“查看表单”按钮。当我点击此按钮时,方框/表格再次打开,尽管表格已提交-我希望方框保持打开状态,以显示“感谢您提交etc”消息-任何帮助都将不胜感激。谢谢以下是我的jquery和php: (function() { $('html').addClass('js'); var contactForm = { container: $('
(function() {
$('html').addClass('js');
var contactForm = {
container: $('#contact'),
config: {
effect: 'slideToggle',
speed: 500
},
init: function(config) {
$.extend(this.config, config);
$('<button></button>', {
text: 'view form'
})
.insertAfter( '.show-form' )
.on( 'click', this.show );
},
show: function() {
var cf = contactForm,
container = cf.container,
config = cf.config;
if ( container.is(':hidden') ) {
contactForm.close.call(container);
container[config.effect](config.speed);
}
},
close: function() {
var $this = $(this), // #contact
config = contactForm.config;
if ( $this.find('span.close').length ) return;
$('<span class=close>X</span>')
.prependTo(this)
.on('click', function() {
// this = span
$this[config.effect](config.speed);
})
}
};
contactForm.init({
// effect: 'fadeToggle',
speed: 300
});
})();
<form>
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Error - Please check your details";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("myemail@hotmail.co.uk", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for your query.";
}
}
else
{//if "email" is not filled out, display the form
echo "<form method='post' action='thanks.php'>
Email: <input name='email' type='text' /><br /><br />
Subject: <input name='subject' type='text' /><br /><br />
Message:<br />
<textarea name='message' rows='5' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
(函数(){
$('html').addClass('js');
var contactForm={
容器:$(“#联系人”),
配置:{
效果:“滑动切换”,
速度:500
},
初始化:函数(配置){
$.extend(this.config,config);
$('', {
文本:“查看表单”
})
.insertAfter(“.show form”)
.on('click',this.show);
},
show:function(){
var cf=联系方式,
容器=cf.container,
config=cf.config;
if(container.is(':hidden')){
contactForm.close.call(容器);
容器[config.effect](配置速度);
}
},
关闭:函数(){
var$this=$(this),//#联系
config=contactForm.config;
if($this.find('span.close').length)返回;
$('X')
.prependTo(本)
.on('单击',函数()){
//这个=跨度
$this[config.effect](配置速度);
})
}
};
contactForm.init({
//效果:“fadeToggle”,
航速:300
});
})();
使用ajax()提交表单,以便在提交后显示消息。。。。使用默认提交将刷新页面。。看看Jquery验证程序插件。这允许您在提交表单之前验证表单,并使用submitHandler显示感谢消息。顺便说一句:如果您将Javascript和PHP拆分为两个“代码块”,阅读您的帖子会更容易。谢谢。对不起,我不知道如何将它分成不同的块,但我会确保在下次之前尽我最大的努力找出答案。干杯