John Smith John Smith - 9 months ago 25
Javascript Question

JQuery Ajax send data from form not working

My script should prevent the page to be refreshed or redirected, but when i click submit button it redirects me.

Error Message from JavaScript console


Uncaught TypeError: $(...).val(...).len is not a function


myScript.js

$( document ).ready(function() {
console.log( "Ready!" );
//Submitting the form data
$("#user-form").submit(function(e){
if($('#fName').val().len() != "" && $('#lName').val().len() != "" && $('#nickname').val().len() != "") {
$.ajax({
type: "POST",
url: "user-process.php",
data: $("#user-form").serialize(),
success: function(data){
$('#user-form')[0].reset();
console.log(data); // show response from the php script.
}
});
}
else {
alert("Please fill the fields.")
}
e.preventDefault();
});

setInterval(function(){ $(`#table`).load('info.php #table'); }, 1000); //Loading table data without refreshing page.

});

Answer Source

"len()" is not a javascript function, you can modify your code to be :

if( $('#fName').val() && $('#lName').val() && $('#nickname').val() )

and will work