我一直在尝试将表单数据提交到数据库,并尝试使用ajax和php。
$(document).ready(function(){
$('#register input').hover(function()
{
$(this).popover('show')
});
$("#register").validate({
rules:{
email:{
required:true,
email: true
},
inquiry:"required"
},
messages:{
email:{
required:"Please enter your E-Mail Address",
email:"Please enter a valid E-Mail Address"
},
inquiry:"Select a reason for contacting us"
},
errorClass: "help-inline",
errorElement: "span",
highlight:function(element, errorClass, validClass) {
$(element).parents('.control-group').addClass('error');
},
unhighlight: function(element, errorClass, validClass) {
$(element).parents('.control-group').removeClass('error');
$(element).parents('.control-group').addClass('success');
},
submitHandler: function register() {
var status = $('#status');
var submitbtn = $('#submitbtn');
var data = {
email: $('#email').val(),
inquiry: $('#inquiry').val()
};
if (data.email == '' || data.inquiry == '') {
status.text('Fill out all of the form data');
return;
}
submitbtn.hide();
status.text('please wait...');
$.ajax({
type: 'post',
url: 'register.php',
data: data,
success: function(responseText) {
if (responseText != 'register_success') {
status.text(responseText);
submitbtn.show();
return;
}
window.scrollTo(0, 0);
$('#register').html('OK '+ data.email +', check your email at <u>'+ data.email +'</u> to complete the registration process.');
// ajax done
// do whatever & close the modal
}
});
return false; // required to block normal submit since you used ajax
}
});
});
这是php:
<?php
if(!empty($_POST['email']) && isset($_POST['email']) && !empty($_POST['inquiry']) && isset($_POST['inquiry']) )
{
// CONNECT TO THE DATABASE
include_once("db.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$email = mysqli_real_escape_string($db, $_POST['email']);
$inquiry = preg_replace('#[^a-z ]#i', '', $_POST['inquiry']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR EMAIL
$sql = "SELECT id FROM register WHERE email='$email' LIMIT 1";
$query = mysqli_query($db, $sql);
$email_check = mysqli_num_rows($query);
// FORM DATA ERROR HANDLING
if($email == "" || $inquiry == ""){
echo "Please fill in all the fields.";
exit();
} else if ($e_check > 0){
echo "That E-Mail Address is already in use";
exit();
} else {
// END FORM DATA ERROR HANDLING
// Begin Insertion of data into the database
// Add user info into the database table for the main site table
$sql = "INSERT INTO register (email, inquiry, ip)
VALUES('$email','$inquiry','$ip')";
$query = mysqli_query($db, $sql);
$uid = mysqli_insert_id($db);
// Create directory(folder) to hold each user's files(pics, MP3s, etc.)
if (!file_exists("user/$u")) {
mkdir("user/$u", 0755);
}
// Email the user their activation link
$to = "$email";
$from = "auto_responder@MyWebSite.com";
$subject = 'MyWebSite E-Mail Account Activation';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>MyWebSite Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.MyWebSite.com"><img src="http://www.MyWebSite.com/images/MyWebSiteEmailLogo.svg" width="36" height="30" alt="MyWebSite" style="border:none; float:left;"></a>E-Mail Activation</div><div style="padding:24px; font-size:17px;">Hello '.$email.',<br /><br /> <br /><br /><a href="http://www.MyWebSite.com/_/php_includes/activation.php?id='.$uid.'&u='.$u.'&email='.$email.'">Click here to activate your E-Mail</a><br /><br /><br /></b></div></body></html>';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
if(mail($to, $subject, $message, $headers)) {
} else {
}
echo "register_success";
exit();
}
exit();
}
?>
但是当我提交时,我得到一个php表单错误。
&lt;“请填写所有字段”&gt;
我做错了什么