一个简单的“注册新闻”输入电子邮件和按钮。
包含姓名,电子邮件,消息的“联系”表单。我还有一个复选框,允许从此表单注册新闻。
您可以在名为test的页面上看到它们: http://butterflyepidemic.com/test/
我不确定如何设置复选框的逻辑,以便两个表单都表现自己。如果订阅者使用注册表单,然后想要向我们发送消息,那么联系表单就不会让他们进展:
'此电子邮件地址已注册'
。理想情况下,我认为我们应该接受该消息,无论以前的注册或复选框状态如何。反馈总是有一些积极的说法:
'您的邮件已收到'
或
'您的讯息已收到。您的电子邮件已经在此处注册了
,如果不是两条正面信息:
'您的邮件已收到并且您已注册'
我是响应式表格的新手。我最初的开发基于本教程:
http://net.tutsplus.com/tutorials/javascript-ajax/building-a-sleek-ajax-signup-form/
使用JS,jQuery,PHP,MySQL,JSON。但现在我有了更多东西的第二种形式。我能够让两个表单成功检查两个字段的表,但现在它的反馈逻辑是错误的。 (编辑:我的意思是'联系'形式的PHP)
用于'注册'表单的HTML:
<form id="newsletter-signup" action="?action=signup" method="post">
<label for="signup-email">Sign up for news & events:</label>
<input type="email" name="signup-email" id="signup-email" placeholder="Your email here..."></input>
<input type="submit" name="signup-button" id="signup-button" value="Sign Me Up!"></input>
<p id="signup-response"></p>
</form>
和'联系'表单的HTML:
<form id="contact-form" action="?action=contact" method="post">
<legend>Contact us:</legend>
<label for="email">Your email: *</label>
<input type="email" name="contact-email" id="contact-email" placeholder="Your email here..." required></input>
<label for="name">Your Name: *</label>
<input type="name" name="contact-name" id="contact-name" placeholder="Your name here..." required></input>
<label for="message">Your Message: *</label>
<textarea id="contact-textarea" name="contact-textarea" placeholder="Type your message here..." rows = "8" cols = "35" required></textarea>
<label for="checkbox">Subscribe to Newsletter?</label>
<input type="checkbox" name="contact-checkbox" id="contact-checkbox" value="1"></input>
<p id="contact-response"></p>
<input type="submit" name="contact-button" id="contact-button"></input>
</form>
以下是注册表单的PHP:
<?php
//form 1 - signup
//email signup ajax call
if(isset($_GET['action'])&& $_GET['action'] == 'signup'){
mysql_connect('***','***','***');
mysql_select_db('***');
//sanitize data
$email = mysql_real_escape_string($_POST['signup-email']);
//validate email address - check if input was empty
if(empty($email)){
$status = 'error';
$message = 'You did not enter an email address!';
}
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
$status = 'error';
$message = 'You have entered an invalid email address!';
}
else {
$existingSignup = mysql_query("SELECT * FROM `signups`, `contact` WHERE signup_email_address='$email' OR contact_email_address='$email'");
if(mysql_num_rows($existingSignup) < 1){
$date = date('Y-m-d');
$time = date('H:i:s');
$insertSignup = mysql_query("INSERT INTO signups (signup_email_address, signup_date, signup_time) VALUES ('$email','$date','$time')");
if($insertSignup){
$status = 'success';
$message = 'you have been signed up!';
}
else {
$status = 'error';
$message = "Oops, there's been a technical error! You have not been signed up.";
}
}
else {
$status = 'error';
$message = 'This email address has already been registered!';
}
}
//return JSON response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
}
和联系表格的PHP如下:
/*Contact Form*/
//ajax call
if(isset($_GET['action'])&& $_GET['action'] == 'contact'){
mysql_connect('***','***','***');
mysql_select_db('***');
//sanitize data
$email = mysql_real_escape_string($_POST['contact-email']);
//validate email address - check if input was empty
if(empty($email)){
$status = 'error';
$message = 'You did not enter an email address!';
}
else if(!preg_match('/^[^\W][a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\@[a-zA-Z0-9_]+(\.[a-zA-Z0-9_]+)*\.[a-zA-Z]{2,4}$/', $email)){ //validate email address - check if is a valid email address
$status = "error";
$message = "You have entered an invalid email address!";
}
else {
$existingContact = mysql_query("SELECT * FROM `signups`, `contact` WHERE signup_email_address='$email' OR contact_email_address='$email'");
if(mysql_num_rows($existingContact) < 1){
//mysql_free_result($existingContact);
//database insert code
if ( isset($_POST['contact-checkbox']) ) {
$checkbox = $_POST['contact-checkbox'];
}
else {
$checkbox = 0;
}
$message = $_POST['contact-textarea'];
$name = $_POST['contact-name'];
$date = date('Y-m-d');
$time = date('H:i:s');
$insertContact = mysql_query("INSERT INTO contact (contact_email_address, contact_date, contact_time, contact_name, contact_message, contact_checkbox) VALUES ('$email','$date','$time','$name','$message','$checkbox')");
if($insertContact){
$status = 'success';
$message = 'your message has been received';
}
else if ($insertContact && $checkbox = $_POST['contact-checkbox']){
$status = 'success';
$message = "your message has been received and you have been signed up";
}
else {
$status = 'error';
$message = "Oops, there's been a technical error!";
}
}
else {
$status = 'error';
$message = 'This email address has already been registered!';
}
}
//return the JSON response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
}
?>
JS'注册'表单:
$(document).ready(function(){
$('#newsletter-signup').submit(function(){
//check the form is not currently submitting
if($(this).data('formsstatus') !== 'submitting'){
//setup variables
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method'),
responseMsg = $('#signup-response');
//add status data to form
form.data('formsstatus','submitting');
//show response message - waiting
responseMsg.hide()
.addClass('response-waiting')
.text('Please Wait...')
.fadeIn(200);
//send data to server to be validated
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
success:function(data){
//setup variables
var responseData = jQuery.parseJSON(data),
klass = '';
//response conditional
switch(responseData.status){
case 'error':
klass = 'response-error';
break;
case 'success':
klass = 'response-success';
break;
}
//show reponse message
responseMsg.fadeOut(200,function(){
$(this).removeClass('response-waiting')
.addClass(klass)
.text(responseData.message)
.fadeIn(200,function(){
//set timeout to hide response message
setTimeout(function(){
responseMsg.fadeOut(200,function(){
$(this).removeClass(klass);
form.data('formsstatus','idle');
});
},3000)
});
});
}
});
}
//prevent form from submitting
return false;
});
})
最后是'联系'表单的JS(与'注册'几乎相同):
$(document).ready(function(){
$('#contact-form').submit(function(){
//check the form is not currently submitting
if($(this).data('formsstatus') !== 'submitting'){
//setup variables
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method'),
responseMsg = $('#contact-response');
//add status data to form
form.data('formsstatus','submitting');
//show response message - waiting
responseMsg.hide()
.addClass('response-waiting')
.text('Please Wait...')
.fadeIn(200);
//send data to server
$.ajax({
url: formUrl,
type: formMethod,
data: formData,
success:function(data){
//setup variables
var responseData = jQuery.parseJSON(data),
klass = '';
//response conditional
switch(responseData.status){
case 'error':
klass = 'response-error';
break;
case 'success':
klass = 'response-success';
break;
}
//show reponse message
responseMsg.fadeOut(200,function(){
$(this).removeClass('response-waiting')
.addClass(klass)
.text(responseData.message)
.fadeIn(200,function(){
//set timeout to hide response message
setTimeout(function(){
responseMsg.fadeOut(200,function(){
$(this).removeClass(klass);
form.data('formsstatus','idle');
});
},3000)
});
});
}
});
}
//prevent form from submitting
return false;
});
})
这就是代码。我想学习如何设置逻辑以提供适当的反馈,如代码上方的理想示例所示。我会对我可能需要研究的任何关键字/链接/示例感到满意。谢谢,保罗。
答案 0 :(得分:0)
这对我有用:
步骤1:在phpmyadmin中为想要仅发送消息(无需注册)的人创建第三个名为“contact_only”的MySQL表
CREATE TABLE `contact_only` (
`contact_only_id` int(10) NOT NULL AUTO_INCREMENT,
`contact_only_email_address` varchar(250) DEFAULT NULL,
`contact_only_name` varchar(250) DEFAULT NULL,
`contact_only_message` varchar(3000) DEFAULT NULL,
`contact_only_checkbox` tinyint(1) NOT NULL default '0',
`contact_only_date` date DEFAULT NULL,
`contact_only_time` time DEFAULT NULL,
PRIMARY KEY (`contact_only_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
第2步:在“联系”表格中,使用'if&amp;否则,如果'根据用户是否勾选了复选框提供适当的反馈
if ( isset($_POST['contact-checkbox']) ) {
$existingContact = mysql_query("SELECT * FROM `signups`, `contact` WHERE signup_email_address='$email' OR contact_email_address='$email'");
if(mysql_num_rows($existingContact) < 1){
$checkbox = $_POST['contact-checkbox'];
$message = $_POST['contact-textarea'];
$name = $_POST['contact-name'];
$date = date('Y-m-d');
$time = date('H:i:s');
$insertContact = mysql_query("INSERT INTO contact (contact_email_address, contact_date, contact_time, contact_name, contact_message, contact_checkbox) VALUES ('$email','$date','$time','$name','$message','$checkbox')");
if($insertContact){
$status = 'success';
$message = 'your message has been received and you have been signed up';
}
}
else {
$checkbox = $_POST['contact-checkbox'];
$message = $_POST['contact-textarea'];
$name = $_POST['contact-name'];
$date = date('Y-m-d');
$time = date('H:i:s');
$insertContact = mysql_query("INSERT INTO contact (contact_email_address, contact_date, contact_time, contact_name, contact_message, contact_checkbox) VALUES ('$email','$date','$time','$name','$message','$checkbox')");
$status = 'success';
$message = 'Message received. Note - this email address has already been subscribed!';
}
}
else if (isset($_POST['contact-checkbox']) == false){
$checkbox = 0;
$message = $_POST['contact-textarea'];
$name = $_POST['contact-name'];
$date = date('Y-m-d');
$time = date('H:i:s');
$insertContactOnly = mysql_query("INSERT INTO contact_only (contact_only_email_address, contact_only_date, contact_only_time, contact_only_name, contact_only_message, contact_only_checkbox) VALUES ('$email','$date','$time','$name','$message','$checkbox')");
if ($insertContactOnly) {
$status = 'success';
$message = 'your message has been received';
}
}
else {
$status = 'error';
$message = "Oops, there's been a technical error!";
}
我对这种方式感到满意,因为它通常比负面反馈更积极,而且总是合适的。以下是此解决方案的链接:
http://butterflyepidemic.com/test/
如果您知道如何更快或更高效,请告诉我们!谢谢,保罗