我想知道是否有人可以帮助我。
首先,我很抱歉,我对 JavaScript 和 jQuery 比较陌生,所以也许这是一个非常愚蠢的问题。
在这里和这里使用这些教程,我将这个页面放在一起,以允许用户将记录添加到 MySQL 数据库,但我对表单“验证”和 jQuery“提交”消息有点困难。
如果使用选择上面的链接,然后在页面加载后,选择“保存”,您将看到正确的字段验证已激活,但尽管出现验证错误,但“位置已保存”消息会出现在页面底部,页面刷新,将记录保存到数据库。
显然这不应该发生,但我在加入“验证”和“提交”消息时遇到了很大的困难。独立它们工作正常,但正如你所看到的,一旦它们在一起就不行。
下面的代码处理“保存记录”和页面刷新
更新 - 下面的工作解决方案
<script>
jQuery(document).ready(function(){
jQuery("#addlocation").validationEngine();
$("#addlocation").bind("jqv.field.result", function(event, field, errorFound, prompText){ console.log(errorFound) })
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#addlocation').submit(function(){
//check the form is not currently submitting
if($(this).data('formstatus') !== 'submitting'){
//setup variables
var form = $(this),
formData = form.serialize(),
formUrl = form.attr('action'),
formMethod = form.attr('method'),
responseMsg = $('#saverecordresponse');
//add status data to form
form.data('formstatus','submitting');
//show response message - waiting
responseMsg.hide()
.addClass('response-waiting')
.text('Please Wait...')
.fadeIn(200);
//send data to server for validation
$.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('formstatus','idle');
});
},3000)
});
});
}
});
}
//prevent form from submitting
return false;
});
});
</script>
这是在选择“保存”按钮时调用的“saverecord.php”脚本。
<?php
//sanitize data
$userid = mysql_real_escape_string($_POST['userid']);
$locationname = mysql_real_escape_string($_POST['locationname']);
$returnedaddress = mysql_real_escape_string($_POST['returnedaddress']);
//validate email address - check if input was empty
if(empty($locationname)){
$status = "error";
$message = "You didn't enter a name for this location!";
}
else if(!preg_match('/^$|^[A-Za-z0-9 _.,]{5,35}$/', $locationname)){ //validate email address - check if is a valid email address
$status = "error";
$message = "You have entered an invalid Location Name!";
}
else{
$query = mysql_query("INSERT INTO `table` (userid, locationname, returnedaddress) VALUES ('$userid', '$locationname', '$returnedaddress')");
if($query){ //if insert is successful
$status = "success";
$message = "Location Saved!";
}
else { //if insert fails
$status = "error";
$message = "I'm sorry, there has been a technical error! Please try again. If problems persist please contact Map My Finds support.";
}
}
//return json response
$data = array(
'status' => $status,
'message' => $message
);
echo json_encode($data);
exit;
?>
我只是想知道是否有人可以看看这个,让我知道我哪里出错了。
非常感谢和亲切的问候