By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,677 Members | 1,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,677 IT Pros & Developers. It's quick & easy.

problems when submitting 'Checkboxes with the same name' with Ajax Post and PHP

sunbin
P: 7
Hi,

I am having in a Trouble when working with dynamic checkboxes (i.e. checkboxes with the same name, e.g. <input type="checkbox" name = "check[]" value="dynamic integer value">)

I have submitted an HTML form (which contains dynamic textboxes and dynamic checkboxes with a Post Button(input type="button")) by using Ajax function. (onClick="return ajaxFunction(document.getElementById('formid'));")

In the ajax function, i have collected all form in a single javascript variable named 'formdata' as shown below :

------------

Expand|Select|Wrap|Line Numbers
  1. for (i=0; i < theForm.length; i++)
  2.     {
  3. if(theForm.elements[i].type == "text") {  //Handle Textbox's
  4. formdata = formdata + theForm.elements[i].name + "=" + escape(theForm.elements[i].value) + "&";                      
  5.  }
  6. else if(theForm.elements[i].type == "checkbox" ) {  //Handle checkbox's
  7.                  formdata = formdata + theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
  8.          }
  9.  
--------
i have passed this 'formdata' variable as an argument to the ajax function, which post all form data to a processing php page (i.e. process.php) and this page processes the data and send back HTTPResponse to the first page which contains that html form.

I am getting that all ajax Request and Response are working properly, but i m regularly receiving (in process.php) all checked and "UNCHECKED" both checkboxes' name and value in the Post data array, here i expect to receive ONLY CHECKED checkboxes in that Post data array.

I think, my javascript codes (shown above) is giving such unwanted result, Could you plz suggest me, how can i solve this problem ???

Thanks in advance,

Sunbin
Mar 9 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
If you only want to send checked checkboxes, check the checked property of each checkbox before adding it to be submitted. In a normal submit, this is done automatically, but when sending manually, you will have to make the checks yourself.
Mar 10 '08 #2

sunbin
P: 7
How can i check the checked property of each checkbox, I tried by adding this code in the my javascript function before submitting to the PHP handler:

.....

else if(theForm.elements[i].type == "checkbox" && theForm.elements[i].checked == true){ //Handle checkbox's
formdata = formdata + theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
}

....
I have also tried by placing "True" / true / 1 but not yet received proper result. could you plz post the javascript for checking checkbox property before submitting to the PHP handler ???
Mar 11 '08 #3

acoder
Expert Mod 15k+
P: 16,027
theForm.elements[i].checked should be enough. Also remember to use encodeURIComponent around each name and value.
Mar 12 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.