PHP逻辑的复选框,我有2个表格和2个MySQL表格 [英] PHP Logic for a Checkbox where I have 2 forms and 2 MySQL tables
问题描述
-
一个简单的'注册新闻'电子邮件和按钮输入。 联系人的表单包含姓名,电子邮件,信息。我也有一个复选框允许注册这个表单的新闻。
称为测试:
http://butterflyepidemic.com/test/
我不知道如何设置复选框的逻辑,以便两种表单都表现自己。如果订阅者使用注册表单,然后想向我们发送消息,联系表单根本不会让他们进展:
'此电子邮件地址已被注册'
。理想情况下,我认为我们应该接受消息,而不管之前的注册或复选框状态。反馈总是有正面的说:
'您的消息已被收到'
或
'您的邮件已收到。您的电子邮件地址已经在这里注册了
,如果不是两个正面信息:
< blockquote>
'您的邮件已收到,您已注册'
我是新手反应形式。我最初是基于本教程开发的:
http://net.tutsplus.com/tutorials/javascript-ajax/building-a-sleek-ajax-signup-form/
它使用JS,jQuery,PHP,MySQL,JSON。但现在我有了更多东西的第二种形式。我能够同时获得两种表格来成功检查两个表的两个表,但现在它的反馈逻辑是错误的。 (编辑:我的意思是用于'contact'形式的PHP)
用于'注册'表单的HTML:
< form id =newsletter-signupaction =?action = signupmethod =post>
< label for =signup-email>注册新闻&事件:其中/标签>
< input type =emailname =signup-emailid =signup-emailplaceholder =Your email here ...>< / input>
< input type =submitname =signup-buttonid =signup-buttonvalue =Sign Me Up!>< / input>
< p id =注册响应>< / p>
< / form>
和HTML代表'contact'形式:
< form id =contact-formaction =?action = contactmethod =post>
< legend>联系我们:< / legend>
< label for =email>您的电子邮件地址:*< / label>
< input type =emailname =contact-emailid =contact-emailplaceholder =您的电子邮件地址...必填>< / input>
< label for =name>您的名称:*< / label>
< input type =namename =contact-nameid =contact-nameplaceholder =Your name here ...required>< / input>
< label for =message>您的讯息:*< / label>
< textarea id =contact-textareaname =contact-textareaplaceholder =在此输入您的信息...rows =8cols =35required>< / textarea>
< label for =checkbox>订阅电子报?< / label>
< input type =checkboxname =contact-checkboxid =contact-checkboxvalue =1>< / input>
< p id =contact-response>< / p>
< input type =submitname =contact-buttonid =contact-button>< / input>
< / form>
以下是注册表单的PHP:
<?php
//表单1 - 注册
//电子邮件注册ajax调用
if(isset($ _ GET ['action']] )&& $ _GET ['action'] =='注册'){
mysql_connect('***','***','***');
mysql_select_db('***');
//净化数据
$ email = mysql_real_escape_string($ _ POST ['signup-email']);
//验证电子邮件地址 - 检查输入是否为空
if(空($ email)){
$ status ='error';
$ message ='您没有输入电子邮件地址!';
}
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)){//验证电子邮件地址 - 检查是否为有效的电子邮件地址
$ status ='error';
$ message ='您输入了无效的电子邮件地址!';
$ {b
$ b $ existingSignup = mysql_query(SELECT * FROM`signups`,`contact` WHERE signup_email_address ='$ email'or contact_email_address ='$ email' ); (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 ='您已注册!';
}
else {
$ status ='error';
$ message =糟糕,出现技术错误!您还没有注册。;
}
}
else {
$ status ='error';
$ message ='此电子邮件地址已被注册!';
//返回JSON响应
$ data = array(
'status'=> $ status,
'消息'=> $消息
);
echo json_encode($ data);
出口;
}
和PHP的联系表单如下所示:
/ *联系表格* /
// ajax调用
if(isset($ _ GET ['action'])&& $ _GET ['action'] =='contact'){
mysql_connect('***','***','***');
mysql_select_db('***');
//净化数据
$ email = mysql_real_escape_string($ _ POST ['contact-email']);
//验证电子邮件地址 - 检查输入是否为空
if(空($ email)){
$ status ='error';
$ message ='您没有输入电子邮件地址!';
}
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 )){//验证电子邮件地址 - 检查是否是有效的电子邮件地址
$ status =error;
$ message =您输入的电子邮件地址无效!;
}
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);
//数据库插入代码
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'); $'$'
$ b $ insertContact = mysql_query(INSERT INTO联系人(contact_email_address,contact_date,contact_time,contact_name,contact_message,contact_checkbox)VALUES('$ email','$ date','$ time','$ name , '$消息', '$复选框'));
if($ insertContact){
$ status ='success';
$ message ='您的邮件已收到';
}
else if($ insertContact&& $ $ checkbox = $ _POST ['contact-checkbox']){
$ status ='success';
$ message =您的邮件已收到,您已注册;
}
else {
$ status ='error';
$ message =糟糕,出现了技术错误!;
}
}
else {
$ status ='error';
$ message ='此电子邮件地址已被注册!';
//返回JSON响应
$ data = array(
'status'=> $ status,
'message'=> $ message
);
echo json_encode($ data);
出口;
}
?>
JS用于'注册'表单:
<$ ()函数(){
/ $($#$)$(文档).ready(函数(){
$('#newsletter-signup' / check the form is not currently submitting
if($(this).data('formsstatus')!=='submitting'){
//设置变量
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method'),
responseMsg = $('#signup-response');
//添加状态数据以形成
form.data('formsstatus','submitting');
//显示响应消息 - 等待
responseMsg.hide()
.addClass('response-waiting')
.text('Please Wait ...')
.fadeIn(200);
//发送数据到服务器进行验证
$ .ajax({
url:formUrl,
type:formMethod,
data:formData,
success:function(data){
//设置变量
var responseData = jQuery.parseJSON(数据),
klass ='';
//响应条件
switch(responseData.status){
case'error':
klass ='response-error';
休息;
case'success':
klass ='response-success';
休息;
$ b $ //显示响应消息
responseMsg.fadeOut(200,function(){
$(this).removeClass('response-waiting')
.addClass(klass)
.text(responseData.message)
.fadeIn(200,function(){
//设置超时以隐藏响应消息
setTimeout( function(){
responseMsg.fadeOut(200,function(){
$(this).removeClass(klass);
form.data('formsstatus','idle');
});
},3000)
});
});
}
});
}
//防止表单提交
返回false;
});
})
最后是'contact'形式的JS(几乎相同至于'注册'):
$(document).ready(function(){
$('#' ($(this).data('formsstatus')!==')()函数(){
//检查表单不是正在提交
if提交'){
//设置变量
var form = $(this),
formData = form.serialize(),$ b $ formUrl = form.attr( 'action'),
formMethod = form.attr('method'),
responseMsg = $('#contact-response');
//将状态数据添加到形式
form.data('formsstatus','submitting');
//显示响应消息 - 等待
responseMsg.hide()
.addClass('等待')
.text('请稍等...')
.fadeIn(200);
//发送数据到服务器
$ .ajax({
url:formUrl,
type:formMethod,
data:formData,
success:function(data){
//设置变量
var responseData = jQuery.parseJSON(数据),
klass ='';
//响应条件
switch(responseData.status){
case'error':
klass ='response-error';
休息;
case'success':
klass ='response-success';
休息;
$ b $ //显示响应消息
responseMsg.fadeOut(200,function(){
$(this).removeClass('response-waiting')
.addClass(klass)
.text(responseData.message)
.fadeIn(200,function(){
//设置超时以隐藏响应消息
setTimeout( function(){
responseMsg.fadeOut(200,function(){
$(this).removeClass(klass);
form.data('formsstatus','idle');
});
},3000)
});
});
}
});
}
//防止表单提交
返回false;
});
})
这就是代码。我想了解如何设置逻辑以提供适当的反馈,如上面代码中的理想示例。我会很高兴与任何我可能需要研究的关键字/链接/示例。感谢Paul。
这适用于我:
1:在phpmyadmin中创建第三个MySQL表,名称为'contact_only',用于只想发送消息的人(无需注册)
CREATE TABLE`contact_only`($ b $``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默认'0',
`contact_only_date`日期DEFAULT NULL,
`contact_only_time` time DEFAULT NULL,
PRIMARY KEY('contact_only_id`)
)ENGINE = MyISAM AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
第二步:在'contact'表单php中,使用'if&否则如果'根据用户是否勾选复选框给出适当的反馈
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'); $'$'
$ b $ insertContact = mysql_query(INSERT INTO联系人(contact_email_address,contact_date,contact_time,contact_name,contact_message,contact_checkbox)VALUES('$ email','$ date','$ time','$ name , '$消息', '$复选框'));
if($ insertContact){
$ status ='success';
$ message ='您的邮件已收到并且您已注册';
}
}
else {
$ checkbox = $ _POST ['contact-checkbox'];
$ message = $ _POST ['contact-textarea'];
$ name = $ _POST ['contact-name'];
$ date = date('Y-m-d');
$ time = date('H:i:s'); $'$'
$ b $ insertContact = mysql_query(INSERT INTO联系人(contact_email_address,contact_date,contact_time,contact_name,contact_message,contact_checkbox)VALUES('$ email','$ date','$ time','$ name , '$消息', '$复选框'));
$ status ='success';
$ message ='收到消息。注意 - 此电子邮件地址已被订阅!';
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'); $'$'
$ b $ 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 , '$消息', '$复选框'));
if($ insertContactOnly){
$ status ='success';
$ message ='您的邮件已收到';
}
}
else {
$ status ='error';
$ message =糟糕,出现了技术错误!;
}
我很满意这种方式,因为它通常会给出比负面反馈更积极的信息,总是合适的。以下是此解决方案的链接:
http:// butterflyepidemic .com / test /
如果您知道如何让它更快或更高效,请告诉我!谢谢,Paul
Paul here. I have 2 forms:
a simple 'Signup for news' input for email, and button.
a 'Contact' form with name, email, message. I also have a checkbox to allow sign up to news from this form.
You can see them here on a page called test: http://butterflyepidemic.com/test/
I'm not sure how to set up the logic for the checkbox so that both forms are behaving themselves. If a subscriber uses the signup form, and then wants to send us a message, the contact form simply won't let them progress:
'this email address has already been registered'
. Ideally I'm thinking we should just accept the message regardless of previous registration, or checkbox status. The feedback would always have something positive to say:
'your message has been received'
or
'your message has been received. Your email's already signed up here'
, if not two positive messages:
'your message has been received and you have been signed up'
I'm new to responsive forms. I initially based my development on this tutorial:
http://net.tutsplus.com/tutorials/javascript-ajax/building-a-sleek-ajax-signup-form/
which uses JS, jQuery, PHP, MySQL, JSON. But now I've a 2nd form with more stuff. I was able to get both forms to check both tables for both fields successfully, but now it's feedback logic is wrong. (edit: I mean the PHP for 'contact' form)
HTML for 'signup' form:
<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>
and HTML for 'contact' form:
<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>
Here's the PHP for the signup form:
<?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;
}
and PHP for the contact form follows:
/*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 for 'signup' form:
$(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;
});
})
and finally the JS for the 'contact' form (pretty much the same as for 'signup'):
$(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;
});
})
That's it for the code. I would like to learn how I can set up the logic to give appropriate feedback as in the ideal examples above the code please. I would be happy with any keywords/links/examples of what I may need to research. Thanks, Paul.
This works for me:
Step 1: Create a third MySQL table called 'contact_only' in phpmyadmin for people who want to message only (no sign-up)
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;
Step 2: in the 'contact' form php, use 'if & else if' to give appropriate feedback based on whether user ticked the checkbox or not
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!";
}
I am happy with this way because it generally gives more positive than negative feedback, and is always appropriate. Here is the link to this solution:
http://butterflyepidemic.com/test/
If you know how to make it faster or more efficient, please let me know! Thanks, Paul
这篇关于PHP逻辑的复选框,我有2个表格和2个MySQL表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!