[jQuery] Re: Problem with radio/checkboxes on ajax post
That would be because your $(':input', this) selector is selecting *every* input field in your form and adding it to your inputs array. One solution is to put a test in your each() function to see if the field is a checkbox or radio, and if it is, only add it to your inputs array if it's checked. On Dec 3, 11:24 am, e2e <[EMAIL PROTECTED]> wrote: > Hi, > > I use this function to submit my post requests. But now I have a > problem with radio buttons and checkboxes. It sends all button values > to server, so the last radio buttons' (even it's not checked) value > passes as value. checkboxes are same, even it's not checked, it sends > checked value. > > $("form#profile_form").submit(function() { > $("#users_profile h1").after(''); > $("#users_profile .errors").remove() > var inputs = []; > $(':input', this).each(function() { > inputs.push(this.name + '=' + unescape(this.value)); > }); > $.ajax({ > type : "POST", > data : inputs.join('&'), > url : "tasks.php?task=updateprofile", > success : function(msg) { > $("#users_profile .loading").remove(); > if(msg == "ok") { > $("#profile_form").remove(); > $("#users_profile h1").after(' class="message m_info">Profile > updated!'); > } else { > $("#users_profile h1").after(msg); > } > } > }); > return false; > });
[jQuery] Re: Problem with radio/checkboxes on ajax post
Changing my function to this solved my problem: $("form#profile_form").submit(function() { $("#users_profile h1").after(''); $("#users_profile .errors").remove() var inputs = []; $(':input', this).each(function() { if(this.name == "gender") { var val = $("[EMAIL PROTECTED]:checked").val(); inputs.push(this.name + '=' + unescape(val)); } else if(this.name == "private") { var val = $("[EMAIL PROTECTED]:checked").val(); inputs.push(this.name + '=' + unescape(val)); } else { inputs.push(this.name + '=' + unescape(this.value)); } }); $.ajax({ type : "POST", data : inputs.join('&'), url : "/sp/tasks.php?task=updateprofile", success : function(msg) { $("#users_profile .loading").remove(); if(msg == "ok") { $("#profile_form").remove(); $("#users_profile h1").after('Profiliniz güncellendi!'); } else { $("#users_profile h1").after(msg); } } }); return false; }); If there is a better way please tell me. On Dec 4, 1:20 am, Wizzud <[EMAIL PROTECTED]> wrote: > That would be because your $(':input', this) selector is selecting > *every* input field in your form and adding it to your inputs array. > One solution is to put a test in your each() function to see if the > field is a checkbox or radio, and if it is, only add it to your inputs > array if it's checked. > > On Dec 3, 11:24 am,e2e<[EMAIL PROTECTED]> wrote: > > > Hi, > > > I use this function to submit my post requests. But now I have a > > problem with radio buttons and checkboxes. It sends all button values > > to server, so the last radio buttons' (even it's not checked) value > > passes as value. checkboxes are same, even it's not checked, it sends > > checked value. > > > $("form#profile_form").submit(function() { > > $("#users_profile h1").after(''); > > $("#users_profile .errors").remove() > > var inputs = []; > > $(':input', this).each(function() { > > inputs.push(this.name + '=' + unescape(this.value)); > > }); > > $.ajax({ > > type : "POST", > > data : inputs.join('&'), > > url : "tasks.php?task=updateprofile", > > success : function(msg) { > > $("#users_profile .loading").remove(); > > if(msg == "ok") { > > $("#profile_form").remove(); > > $("#users_profile h1").after(' > class="message m_info">Profile > > updated!'); > > } else { > > $("#users_profile h1").after(msg); > > } > > } > > }); > > return false; > > });
[jQuery] Re: Problem with radio/checkboxes on ajax post
On Dec 4, 1:20 am, Wizzud <[EMAIL PROTECTED]> wrote: > That would be because your $(':input', this) selector is selecting > *every* input field in your form and adding it to your inputs array. > One solution is to put a test in your each() function to see if the > field is a checkbox or radio, and if it is, only add it to your inputs > array if it's checked. Is there a simple way to do that? > On Dec 3, 11:24 am,e2e<[EMAIL PROTECTED]> wrote: > > > Hi, > > > I use this function to submit my post requests. But now I have a > > problem with radio buttons and checkboxes. It sends all button values > > to server, so the last radio buttons' (even it's not checked) value > > passes as value. checkboxes are same, even it's not checked, it sends > > checked value. > > > $("form#profile_form").submit(function() { > > $("#users_profile h1").after(''); > > $("#users_profile .errors").remove() > > var inputs = []; > > $(':input', this).each(function() { > > inputs.push(this.name + '=' + unescape(this.value)); > > }); > > $.ajax({ > > type : "POST", > > data : inputs.join('&'), > > url : "tasks.php?task=updateprofile", > > success : function(msg) { > > $("#users_profile .loading").remove(); > > if(msg == "ok") { > > $("#profile_form").remove(); > > $("#users_profile h1").after(' > class="message m_info">Profile > > updated!'); > > } else { > > $("#users_profile h1").after(msg); > > } > > } > > }); > > return false; > > });
[jQuery] Re: Problem with radio/checkboxes on ajax post
You could try... $(':input',this).each(function(){ var me = $(this); if(!me.is(':checkbox, :radio') || this.checked){ inputs.push(this.name + '=' + unescape(me.val())); } }); On Dec 4, 8:44 am, e2e <[EMAIL PROTECTED]> wrote: > Changing my function to this solved my problem: > $("form#profile_form").submit(function() { > $("#users_profile h1").after(''); > $("#users_profile .errors").remove() > var inputs = []; > $(':input', this).each(function() { > if(this.name == "gender") { > var val = $("[EMAIL PROTECTED]:checked").val(); > inputs.push(this.name + '=' + unescape(val)); > } else if(this.name == "private") { > var val = $("[EMAIL PROTECTED]:checked").val(); > inputs.push(this.name + '=' + unescape(val)); > } else { > inputs.push(this.name + '=' + unescape(this.value)); > } > > }); > $.ajax({ > type : "POST", > data : inputs.join('&'), > url : "/sp/tasks.php?task=updateprofile", > success : function(msg) { > $("#users_profile .loading").remove(); > if(msg == "ok") { > $("#profile_form").remove(); > $("#users_profile h1").after(' class="message > m_info">Profiliniz güncellendi!'); > } else { > $("#users_profile h1").after(msg); > } > } > }); > return false; > }); > > If there is a better way please tell me. > > On Dec 4, 1:20 am, Wizzud <[EMAIL PROTECTED]> wrote: > > > That would be because your $(':input', this) selector is selecting > > *every* input field in your form and adding it to your inputs array. > > One solution is to put a test in your each() function to see if the > > field is a checkbox or radio, and if it is, only add it to your inputs > > array if it's checked. > > > On Dec 3, 11:24 am,e2e<[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > I use this function to submit my post requests. But now I have a > > > problem with radio buttons and checkboxes. It sends all button values > > > to server, so the last radio buttons' (even it's not checked) value > > > passes as value. checkboxes are same, even it's not checked, it sends > > > checked value. > > > > $("form#profile_form").submit(function() { > > > $("#users_profile h1").after(' > > class="loading">'); > > > $("#users_profile .errors").remove() > > > var inputs = []; > > > $(':input', this).each(function() { > > > inputs.push(this.name + '=' + unescape(this.value)); > > > }); > > > $.ajax({ > > > type : "POST", > > > data : inputs.join('&'), > > > url : "tasks.php?task=updateprofile", > > > success : function(msg) { > > > $("#users_profile .loading").remove(); > > > if(msg == "ok") { > > > $("#profile_form").remove(); > > > $("#users_profile h1").after(' > > class="message m_info">Profile > > > updated!'); > > > } else { > > > $("#users_profile h1").after(msg); > > > } > > > } > > > }); > > > return false; > > > });
[jQuery] Re: Problem with radio/checkboxes on ajax post
Thank you... On Dec 5, 1:39 am, Wizzud <[EMAIL PROTECTED]> wrote: > You could try... > > $(':input',this).each(function(){ > var me = $(this); > if(!me.is(':checkbox, :radio') || this.checked){ > inputs.push(this.name + '=' + unescape(me.val())); > } > }); > > On Dec 4, 8:44 am, e2e <[EMAIL PROTECTED]> wrote: > > > Changing my function to this solved my problem: > > $("form#profile_form").submit(function() { > > $("#users_profile h1").after(''); > > $("#users_profile .errors").remove() > > var inputs = []; > > $(':input', this).each(function() { > > if(this.name == "gender") { > > var val = $("[EMAIL PROTECTED]:checked").val(); > > inputs.push(this.name + '=' + unescape(val)); > > } else if(this.name == "private") { > > var val = $("[EMAIL PROTECTED]:checked").val(); > > inputs.push(this.name + '=' + unescape(val)); > > } else { > > inputs.push(this.name + '=' + unescape(this.value)); > > } > > > }); > > $.ajax({ > > type : "POST", > > data : inputs.join('&'), > > url : "/sp/tasks.php?task=updateprofile", > > success : function(msg) { > > $("#users_profile .loading").remove(); > > if(msg == "ok") { > > $("#profile_form").remove(); > > $("#users_profile h1").after(' > class="message > > m_info">Profiliniz güncellendi!'); > > } else { > > $("#users_profile h1").after(msg); > > } > > } > > }); > > return false; > > }); > > > If there is a better way please tell me. > > > On Dec 4, 1:20 am, Wizzud <[EMAIL PROTECTED]> wrote: > > > > That would be because your $(':input', this) selector is selecting > > > *every* input field in your form and adding it to your inputs array. > > > One solution is to put a test in your each() function to see if the > > > field is a checkbox or radio, and if it is, only add it to your inputs > > > array if it's checked. > > > > On Dec 3, 11:24 am,e2e<[EMAIL PROTECTED]> wrote: > > > > > Hi, > > > > > I use this function to submit my post requests. But now I have a > > > > problem with radio buttons and checkboxes. It sends all button values > > > > to server, so the last radio buttons' (even it's not checked) value > > > > passes as value. checkboxes are same, even it's not checked, it sends > > > > checked value. > > > > > $("form#profile_form").submit(function() { > > > > $("#users_profile h1").after(' > > > class="loading">'); > > > > $("#users_profile .errors").remove() > > > > var inputs = []; > > > > $(':input', this).each(function() { > > > > inputs.push(this.name + '=' + unescape(this.value)); > > > > }); > > > > $.ajax({ > > > > type : "POST", > > > > data : inputs.join('&'), > > > > url : "tasks.php?task=updateprofile", > > > > success : function(msg) { > > > > $("#users_profile .loading").remove(); > > > > if(msg == "ok") { > > > > $("#profile_form").remove(); > > > > $("#users_profile h1").after(' > > > class="message m_info">Profile > > > > updated!'); > > > > } else { > > > > $("#users_profile h1").after(msg); > > > > } > > > > } > > > > }); > > > > return false; > > > > });