保羅在這裏。我有兩種形式:PHP邏輯的複選框,我有2個表格和2個MySQL表格
一個簡單的'註冊新聞'輸入的電子郵件和按鈕。
帶姓名,電子郵件,信息的'聯繫'表單。我也有一個複選框允許註冊此表單中的新聞。
你可以在這裏看到他們在一個頁面調用測試: 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;
});
})
這它代碼。我想了解如何設置邏輯以提供適當的反饋,就像代碼上面的理想示例一樣。我會很高興與任何我可能需要研究的關鍵字/鏈接/例子。謝謝,保羅。
這是代碼**的**牆。你能把它縮小到你遇到困難的地方嗎? –
是的,在PHP聯繫表單中,從'code' $ existingContact = mysql_query(「SELECT * FROM'signups','contact' WHERE signup_email_address ='$ email'或contact_email_address ='$ email'」); 'code' – Paul
你的問題到底是什麼?什麼是適當的反饋?你得到的反饋有什麼問題(如果有的話)? – Tarsis