Your question is a bit confusing, so I'll try to explain with examples the two ways to submit a form.
In the first example the form is:
Validate by the function
validate()
Submitted through the function
$( "#myform" ).submit()
$('#press').on('click', function(e){
e.preventDefault();
var val = validate();
if(val == true){
var r = confirm("Submit form!")
var txt;
if (r == true) {
txt = "You pressed OK!";
//$( "#myform" ).submit(); //use this for submitting the form
} else {
txt = "You pressed Cancel!";
}
alert(txt); //this line of code for test reasons
}
else{
alert("input fields empty");
}
});
function validate(){
var val = true;
var teste1 = $("#input1").val();
var teste2 = $("#input2").val();
if(teste1== "" || teste1 == null){
var val = false;
//some css and jquery to change the input color
}
if(teste2 == "" || teste2 == null){
var val = false;
//some css and jquery to change the input color
}
return val;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id = "myform" action="test.php" method="post">
<input type = "text" id ="input1" value = "">
<input type = "text" id ="input2" value = "">
<button id = "press" type="button">Click Me!</button>
</form>
This type of submission will refresh the page. For the page not refresh you need to use ajax.
In your trigger function you need to replace $( "#myform" ).submit();
.
var teste1 = $("#input1").val();
var teste2 = $("#input2").val();
$.ajax({
method: "POST",
url: "test.php",
datatype:"Json",
data:{
"data1": teste1,
"data2": teste2
},
success: function(result){
alert( "result");
}
});
This way send the data in a Json array. This way you can have server feedback through the success function.