473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

sunbin
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
3 2573
acoder
16,027 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
sunbin
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
16,027 Expert Mod 8TB
theForm.elements[i].checked should be enough. Also remember to use encodeURIComponent around each name and value.
Mar 12 '08 #4

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

Similar topics

4
by: Eric | last post by:
Hey Everyone.. I have a form that has approximately 7 text fields and 1 checkbox. Generally when this form is submitted(to itself BTW) it works fine, however, when the checkbox is only field...
13
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div...
5
by: Tyler Style | last post by:
Hullo - looking for a little advice here. I have a form on a page in one domain submitting to a cgi in another domain. Weirdly, on some Windows XP systems, a form on the page fails to submit/post...
2
by: jimi_xyz | last post by:
Sorry if this isn't the correct group, i don't think there is a group for straight HTML. I am trying to create a type of search engine. There are two radio buttons at the top, in the middle there...
10
by: Danny | last post by:
Hi all, I am having some odd problems with AJAX on Firefox (1.5). When I use GET as the request method everything works ok, but when I do a POST the remote function doesn't get the parameters I...
2
by: GustavoBrust | last post by:
Hello everyone. I'm having a problem with a search I want to make. It's like this: The user can choose about 15 checkboxes to be his search pattern. According to the combination of...
3
by: noballack | last post by:
I've got a problem, I'm working with Ajax in a web with a form with a list of checkbox added to the form via an Ajax.Updater method. These added checkboxs are not been sended by the form if I use...
1
by: bdbeames | last post by:
I have a page with google maps up and running. When a user clicks on a location from the map I would like to populate a drop down with data from the database related to that location. I have it up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.