我创建了一个表单来在数据库中添加记录,因此插入记录和所有记录都运行良好。
我想对表单进行验证,现在发生了什么,虽然我已根据需要设置了字段,然后在单击提交按钮后表单正在提交,空白值正在插入数据库。
我已将此链接用于验证:
A simple jQuery form validation script
但不知何故它不适合我。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Post</title>
</head>
<body>
<form class="postForm" id="postForm" method="post" action="addPost.php">
<fieldset>
<legend>Please add the details below </legend>
<p>
<label for="title">Title (required, at least 2 characters)</label>
<input id="title" name="title" minlength="2" type="text" required>
</p>
<p>
<label for="url">URL (required)</label>
<input id="url" type="url" name="url" required>
</p>
<p>
<label for="desc">Description (required, at least 2 characters)</label>
<input id="desc" name="desc" minlength="2" type="text" required>
</p>
<p>
<label for="keywords">Keywords (eg:#facebook)(required, at least 2 characters)</label>
<input id="keywords" name="keywords" minlength="2" type="text" required>
</p>
<p>
<label for="urlType">Select Url Type :(required)</label>
<select name="urlType" id="urlType">
<option value="">Select Url Type...</option>
<option value="0">Server Image</option>
<option value="1">Server Video</option>
<option value="2">YouTube Video</option>
<option value="3">Vimeo Video</option>
<option value="4">Facebook Image</option>
<option value="5">Facebook Video</option>
<option value="6">Instagram Image</option>
<option value="7">Instagram Video</option>
<option value="-1">Other</option>
</select>
</p>
<p>
<label for="postType"> Select Post Type :(required)</label>
<select name="postType" id="postType">
<option value="">Select Post Type...</option>
<option value="0">Normal</option>
<option value="1">Featured</option>
<option value="2">Sponsored</option>
</select>
</p>
<p>
<label for="category"> Select Category :(required)</label>
<select name="category" id="category">
<option value="0">Select Category...</option>
</select>
</p>
<p>
<input type="hidden" name="action_type" id="action_type_id"/>
<input type="hidden" name="id" id="p_id"/>
<!-- <a href="javascript:void(0);" class="btn btn-warning" onclick="$('#postForm').slideUp();">Cancel</a>
<a href="javascript:void(0);" class="btn btn-success" onclick="userAction('add')">Add User</a>-->
<input type="button" name="Submit" id="Submit" value="Submit" onclick="userAction('add')">
</p>
</fieldset>
<div class="result" id="result"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
getCategories();
});
function getCategories() {
$.ajax({
type: "POST",
url: 'getCategories.php',
dataType: 'text',
async: false,
cache: false,
success: function (result) {
$('#category').html(result);
}
});
}
function userAction(type,id){
var statusArr = {add:"added",edit:"updated",delete:"deleted"};
if (type == 'add') {
$('#action_type_id').val(type);
$('#p_id').val(id);
}
$.ajax({
type: 'POST',
url: 'addPost.php',
data: $('#postForm').serialize(),
success:function(report){
// replace data to report
$(".result").html(report);
$("#postForm").trigger('reset');
}
});
}
</script>
</form>
<script>
$("#postForm").validate();
</script>
</body>
</html>
请帮助,谢谢..
答案 0 :(得分:1)
需要参考jquery validate
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
然后需要初始化插件,演示示例 -
$('#postForm').validate({
submitHandler: function (form) { // for demo
userAction('add');
}
});
删除您正在提交的onclick
,需要从submitHandler调用userAction
答案 1 :(得分:0)
提交按钮的类型应该是“提交”。 希望这会有所帮助。