I\'m validating for duplicate names by using jquery+Ajax. Everything is working fine except that the form is not submitting once everything returns true
What
I would check it with jQuery validator at run time, not on submit but another approach is to make a Rest Style call. I think it's not necessary to POST a full form just to check 1 field.
$('#form1').submit(function(){
//Check not empty
if(!$('#shelf_name').val()) {
alert('Shelf name is required');
$('#shelf_name').focus();
} else {
//Valiate Rest style call
$.getJSON("check-duplicate-shelf-name.php/".concat($('#shelf_name').val()), function(data) {
//If you only have 2 states I would return boolean to faster check ex: if(!data){
if(data == 'stop') {
// working if duplicate name is found
alert('Shelf name already exists');
} else {
alert('else working?'); // alert box is showing up if name is not duplicate
$('#form1').submit(); // but after alert, this line not executing
}
});
}
return false;
});
When I said on comments that you can post your form manually not using $(this).submit(); I refer to:
$.ajax({
url: "./myurl",
cache: false,
type: "POST",
data: $("#form1").serialize(),
success: function(){
console.log("Submit successful");
}
});
Dont try to much check get simple button and affffd onclick event in that event put the code below
html code
<input type="button" value="submit" onclick="formsubmit();">
javascript code
function formsubmit() {
var name = $('#shelf_name').val();
if (name == '') {
alert('Shelf name is required');
$('#shelf_name').focus();
} else {
$.ajax({
type: 'post',
url: 'check-duplicate-shelf-name.php',
data: {
'name': name
},
context: this,
success: function (data) {
if (data == 'stop') {
alert('Shelf name already exists'); // working if duplicate name is found
} else {
alert('else working?'); // alert box is showing up if name is not duplicate
$('#form1').submit()
}
}
});
}
}
Enjoy it's Working..
document.createElement('form').submit.call(document.formname);
You can benefit from the callbacks
in this case. Just separate the code like below:
function with the callback that will return an object {status: 'true or false', message: 'Some text'}
function validity(callback){
var name = $('#shelf_name').val();
if(name == '')
{
return callback({status: false, message: 'Shelf name is required'});
}
else
{
$.ajax({
type:'post',
url:'check-duplicate-shelf-name.php',
data:{'name':name},
context:this,
success:function(data)
{
if(data == 'stop')
{
return callback({status: false, message: 'Shelf name already exists'});
}
else
{
return callback({status: true, message: 'else working?'});
}
}
});
}
//just for safety :))
return callback({status: false, message: 'something went wrong completely'});
});
This returns the result from the AJAX call. It will wait for AJAX to complete and return the corresponding branch result of if
...
.. and you can now use it as this:
$('#form1').submit(function(){
validity(function(result){
if (result.status) { //status is true
return true;
}
else
{
alert(result.message);
$('#shelf_name').focus();
return false;
}
});
});
... or just combine them like :
$('#form1').submit(function(){
// definition
function validity(callback){
var name = $('#shelf_name').val();
if(name == '')
{
return callback({status: false, message: 'Shelf name is required'});
}
else
{
$.ajax({
type:'post',
url:'check-duplicate-shelf-name.php',
data:{'name':name},
context:this,
success:function(data)
{
if(data == 'stop')
{
return callback({status: false, message: 'Shelf name already exists'});
}
else
{
return callback({status: true, message: 'else working?'});
}
}
});
}
//just for safety :))
return callback({status: false, message: 'something went wrong completely'});
});
//usage
validity(function(result){
if (result.status) { //status is true
return true;
}
else
{
alert(result.message);
$('#shelf_name').focus();
return false;
}
});
});
Hope this helps
Completely due to scope
Where you have
alert('else working?'); // alert box is showing up if name is not duplicate
this.submit(); // but after alert, this line not executing
this is referring to the ajax object, not the form. This is because it is now inside another function.
Im adding $form = $(this);
before the ajax call to declare a variable you can use inside the callback.
Try this:
$('#form1').submit(function( e ){
e.peventDefault();
var name = $('#shelf_name').val();
if(name == '')
{
alert('Shelf name is required');
$('#shelf_name').focus();
}
else
{
$form = $(this);
$.ajax({
type:'post',
url:'check-duplicate-shelf-name.php',
data:{'name':name},
context:this,
success:function(data)
{
if(data == 'stop')
{
alert('Shelf name already exists'); // working if duplicate name is found
}
else
{
alert('else working?'); // alert box is showing up if name is not duplicate
$form.submit(); // but after alert, this line not executing
}
}
});
}
return false;
});
UPDATE: I may have been completely out of it yesterday. Try adding e.preventDefault(); just above the submit call. Additionally, add the e variable in the function() definition. Check the updated code above.
Here is some documentation on preventDefault(): http://api.jquery.com/event.preventDefault/