我一直在阅读教程,但仍然看不出我做错了什么。这应该是一个简单的反馈表单,将作者,电子邮件,评论和时间戳输入数据库。
以下是反馈表:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feedback</title>
<style type="text/css">
fieldset{float:center; width:50%; padding:6px;}
label{float:left; width:10%; font:12px Arial, Helvetica, sans-serif; padding:5px}
textarea{font:13px Arial, Helvetica, sans-serif;}
input{margin-bottom:5px;}
div#sent{display:none; font:12px Arial, Helvetica, sans-serif;}
</style>
</head>
<body>
<div id="sent">Thank You we appreciate your comments</div>
<div id="form" align="center">
<form id="feedback" action="#">
<fieldset>
<label>Name:</label>
<input type="text" name="name" /><br />
<label>email:</label>
<input type="text" name="email" /><br />
<label>Comment:</label><br />
<textarea name="comment" rows="4" cols="40"></textarea><br />
<input type="submit" name="submit" value="Send" />
<input type="reset" name="reset" value="Reset" />
</fieldset>
</form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></>
</script> <script type = "text/javascript">
$(function(){
var expressions = {
name:/^[a-z]+[\.-']|\s)?[a-z]+$/i,
email:/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/,
comment:/^\w+/
};
$('form#feedback').submit(function(e){
e.preventDefault();
var valid=true;
for(var i=0; i<this.elements.length; i++){
var field= this.elements[i];
if(field.type=='text'||field.type=='textarea'){
if(!expressions[field.name].test($(field).val())){
alert('Oops! Mistake in '+field.name.replace('_',' ').toUpperCase()+'.');
field.focus();
valid=false;
return false;
}
}
}
If(valid){
$.post('enter.php'; $(this).serialise(), function(data){
if(data.sucess{
$(div#form).fadeOut();
$(div#sent).fadeIn();
}else{
alert('We had a problem submitting the page');
}
}, 'json');
return false; }
});
});
</script>
</body>
</html>
这是处理代码:
$remove= array('submit','reset');
$accepted= array('name','email', 'comment');
foreach($data as $field=> $value){
if(in_array($field,$remove)){
unset($data[$field]);
} elseif (!in_array($field, $accepted)){
header('Location:feedback.php');
}
}
$expressions = array(
name=>"~^[a-z]+[\.-']|\s)?[a-z]+$~i",
email=>'~^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$~',
comment=>'~^\w+~'
);
foreach($data as $field=> $value){
if(!preg_match($expressions[$field], $value)) {
header('Location: feedback.php');
}
}
require_once('connections\connect2.php');
$response =(object) null;
$Stmt= $db->stmt_init();
$query='INSERT INTO feedback(name, email, comment, submitted)
VALUES(?,?,?,NOW())';
if($Stmt->prepare($query)){
$Stmt-> bind_param('sss',
$data['name'],
$data['email'],
$data['comment']);
$response->success=$stmt->execute();
}else {
$response-> false;
}
echo json_encode($response);
?>
任何人都可以看到为什么这不起作用?表单不提交,没有任何内容进入数据库,单击提交时填充的表单没有消息/更改。
答案 0 :(得分:0)
您需要检查一些事项。