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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunbin
    New Member
    • Feb 2007
    • 7

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

    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="retur n ajaxFunction(do cument.getEleme ntById('formid' ));")

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

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

    [CODE=javascript]for (i=0; i < theForm.length; i++)
    {
    if(theForm.elem ents[i].type == "text") { //Handle Textbox's
    formdata = formdata + theForm.element s[i].name + "=" + escape(theForm. elements[i].value) + "&";
    }
    else if(theForm.elem ents[i].type == "checkbox" ) { //Handle checkbox's
    formdata = formdata + theForm.element s[i].name + "=" + theForm.element s[i].value + "&";
    }
    [/CODE]
    --------
    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
    Last edited by acoder; Mar 10 '08, 02:58 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • sunbin
      New Member
      • Feb 2007
      • 7

      #3
      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.elem ents[i].type == "checkbox" && theForm.element s[i].checked == true){ //Handle checkbox's
      formdata = formdata + theForm.element s[i].name + "=" + theForm.element s[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 ???

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        theForm.element s[i].checked should be enough. Also remember to use encodeURICompon ent around each name and value.

        Comment

        Working...