我正在学习jquery ajax php帖子,我收到了成功消息,但数据没有保存在我的数据库中,页面在线请在下面的链接中查看。
http://marlz13.net78.net/Feedback%20AJAX/
这是我的Send JS脚本:
$(document).ready(function() {
$('form #response').hide();
$('#submit').click(function(e) {
// prevent forms default action until
// error check has been performed
e.preventDefault();
// grab form field values
var valid = '';
var required = ' is required.';
var name = $('form #name').val();
var email = $('form #email').val();
var message = $('form #message').val();
var honeypot = $('form #honeypot').val();
var humancheck = $('form #humancheck').val();
// perform error checking
if (name = '' || name.length <= 2) {
valid = '<p>Your name' + required +'</p>';
}
if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<p>Your email' + required +'</p>';
}
if (message = '' || message.length <= 5) {
valid += '<p>A message' + required + '</p>';
}
if (honeypot != 'http://') {
valid += '<p>Spambots are not allowed.</p>';
}
if (humancheck != '') {
valid += '<p>A human user' + required + '</p>';
}
// let the user know if there are erros with the form
if (valid != '') {
$('form #response').removeClass().addClass('error')
.html('<strong>Please correct the errors below.</strong>' +valid).fadeIn('fast');
}
// let the user know something is happening behind the scenes
// serialize the form data and send to our ajax function
else {
$('form#response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');
var formData = $('form').serialize();
submitForm(formData);
}
});
});
// make our ajax request to the server
function submitForm(formData) {
$.ajax({
type: 'POST',
url: 'feedback.php',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data) {
$('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
.html(data.msg).fadeIn('fast');
if ($('form #response').hasClass('success')) {
setTimeout("$('form #response').fadeOut('fast')", 5000);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('form #response').removeClass().addClass('error')
.html('<p>There was an<strong> ' + errorThrown +
'</strong> error due to a<strong> ' + textStatus +
'</strong> condition.</p>').fadeIn('fast');
},
complete: function(XMLHttpRequest, status) {
$('form')[0].reset();
}
});
};
这是PHP脚本
<?php
// Establishing connection with server..
$connection = mysql_connect("databaseserver", "username", "password");
// Selecting Database
$db = mysql_select_db("db", $connection);
sleep(2);
//Sanitize incoming data and store in variable
$name = trim(stripslashes(htmlspecialchars($_POST['name'])));
$email = trim(stripslashes(htmlspecialchars($_POST['email'])));
$message = trim(stripslashes(htmlspecialchars($_POST['message'])));
$humancheck = $_POST['humancheck'];
$honeypot = $_POST['honeypot'];
if ($honeypot == 'http://' && empty($humancheck)) {
//Validate data and return success or error message
$error_message = '';
$reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";
if (!preg_match($reg_exp, $email)) {
$error_message .= "<p>A valid email address is required.</p>";
}
if (empty($name)) {
$error_message .= "<p>Please provide your name.</p>";
}
if (empty($message)) {
$error_message .= "<p>A message is required.</p>";
}
if (!empty($error_message)) {
$return['error'] = true;
$return['msg'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>".$error_message;
echo json_encode($return);
exit();
} else {
//Insert query
mysql_query("INSERT into formdata (name, email, desc) values ('$name', '$email', '$message')");
//connection closed
mysql_close($connection);
$return['error'] = false;
$return['msg'] = "<p>Thanks for your feedback " .$name .".</p>";
echo json_encode($return);
}
} else {
$return['error'] = true;
$return['msg'] = "<h3>Oops! There was a problem with your submission. Please try again.</h3>";
echo json_encode($return);
}
?>
这是我的HTM的首脑
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AJAX form tutorial using jQuery and PHP</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="ajax_submit.js"></script>
</head>
答案 0 :(得分:0)
检查
<?php
// Establishing connection with server..
$connection = mysql_connect("databaseserver", "username", "password");
// Selecting Database
$db = mysql_select_db("db", $connection);
sleep(2);
//Sanitize incoming data and store in variable
$name = trim(stripslashes(htmlspecialchars($_POST['name1'])));
$email = trim(stripslashes(htmlspecialchars($_POST['email1'])));
$message = trim(stripslashes(htmlspecialchars($_POST['message1'])));
//$humancheck = $_POST['humancheck'];
//$honeypot = $_POST['honeypot'];
//if ($honeypot == 'http://' && empty($humancheck))
//{
//Validate data and return success or error message
$error_message = '';
$reg_exp = "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";
if (!preg_match($reg_exp, $email)) {
$error_message .= "<p>A valid email address is required.</p>";
}
if (empty($name)) {
$error_message .= "<p>Please provide your name.</p>";
}
if (empty($message)) {
$error_message .= "<p>A message is required.</p>";
}
if(empty($error_message)==true)
{
$return['error'] = true;
$return['msg'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>".$error_message;
echo json_encode($return);
exit();
} else {
//Insert query
mysql_query("insert into formdata (name, email, desc) values ('$name', '$email', '$message')");
//connection closed
mysql_close($connection);
$return['error'] = false;
$return['msg'] = "<p>Thanks for your feedback " .$name .".</p>";
echo json_encode($return);
}
?>
答案 1 :(得分:0)
就像我在评论中所说的那样,DESC是mysql中的一个保留字,所以你应该像下面一样把它放在后面。
您还可以查看mysql_error()的错误。
$save = mysql_query("INSERT into formdata (name, email, `desc`) values ('$name', '$email', '$message')");
if(!$save) {
$return["mysql_err"] = mysql_error();
}