I have been suffering from a solution of a problem for days. I would like to use jquery validation and ajax call without page refresh together. If I only use one of them, the code is working properly but together not. I would like a very simple, comprehensible code.
Could you help me where the problem can be? Thanks in advance.
Best regards, Atti
**teszt.php**
<!doctype html>
<html lang="hu">
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script>
$(document).ready(function(){
$('#form').validate({
rules: {
nev: {
required: true,
minlength: 2
}
},
messages: {
nev: {
required: "We need your email address to contact you",
minlength: 2
}
},
submitHandler: function(form) {
$('#button_id').click(function(){
var nev=$("#nev").val();
var action=$("#action").val();
$.ajax({
type: "post",
url: "php.php",
data: "nev="+nev+"&action="+action,
success:function(result){
$("#nevem").html(result);
}
});
});
form.submit();
}
});
});
</script>
</head>
<body>
<form id="form">
<input type="text" id="nev" name="nev">
<input type="hidden" id="action" name="action" value="addadd">
<input type="button" id="button_id" name="button_id" value="Mentés">
</form>
<div id="nevem"></div>
</body>
</html>
**php.php**
<?php
if ($_POST["action"] == "addadd") {
echo $_POST["nev"];
} else {
echo 987;
}
?>
Call it as such:
$('#button_id').click(function(){
$("#myform").submit(function(e) {
e.preventDefault();
}).validate({
rules: {
nev: {
required: true,
minlength: 2
}
},
messages: {
nev: {
required: "We need your email address to contact you",
minlength: 2
}
},
submitHandler: function(form) {
var nev=$("#nev").val();
var action=$("#action").val();
$.ajax({
type: "post",
url: "php.php",
data: "nev="+nev+"&action="+action,
success:function(result){
$("#nevem").html(result);
}
});
});
});
}); // no .submit() here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.