467,081 Members | 1,159 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

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


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.          }
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,

Mar 9 '08 #1
  • viewed: 2297
3 Replies
Expert Mod 8TB
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
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
Expert Mod 8TB
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.

Similar topics

2 posts views Thread by jimi_xyz@hotmail.com | last post: by
2 posts views Thread by GustavoBrust | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.