Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    batfastad
    Guest

    Question form input name containing []

    Ok I've got an HTML form (name=xrefselector), to post to a PHP script

    It consists of about 300 checkboxes and various other form controls.
    All the checkboxes have the same name="" attribute taginput[], with the value="" attribute varying between them.

    I have to have them called taginput[] with square brackets because when the form data gets sent to my PHP script, the checkbox data is already in an array which is exactly what I need.

    Why's this in the JavaScript forum?

    Well I've got the following javascript function...

    Code:
    function checkCount(form) {
    	var total = 0;
    	var max = form.taginput.length;
    
    	for (var idx = 0; idx < max; idx++) {
    		if (eval("document.xrefselector.taginput[" + idx + "].checked") == true) {
    			total += 1;
    		}
    	}
    
    	document.xrefselector.checkboxcounter.value = total + " selected";
    }
    And

    Code:
     onClick="checkCount(this.form)"
    An onClick event handler on each of the checkboxes

    I have a readonly text field (name=checkboxcounter) which acts as a counter, counting how many checkboxes are selected, which is updated after every click on a checkbox.

    This is the problem...
    The script works fine if all the checkboxes are named taginput, but I need to name them taginput[]

    Though calling them taginput[] then gives me JavaScript errors - when I replace taginput in my JavaScript code with taginput[]

    So on two lines in particular...
    Code:
    	var max = form.taginput.length;
    ...becomes...
    	var max = form.taginput[].length;
    
    ...and...
    
    		if (eval("document.xrefselector.taginput[" + idx + "].checked") == true) {
    
    ...becomes...
    		if (eval("document.xrefselector.taginput[][" + idx + "].checked") == true) {
    Which give me JavaScript errors

    Can anyone see a way round this?
    The name of the checkboxes needs to be taginput[] with square brackets, but is there anyway I can escape those in my JavaScript, but still have the JavaScript working?


    Any ideas?

    Thanks

    Ben

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    This should work for you mate:

    Code:
    function checkCount(f) {
    	var counter = 0;
    	for(var i=0; f.elements[i]; i++) {
    		if(f.elements[i].tagName.toLowerCase() == "input" && f.elements[i].type == "checkbox" && f.elements[i].checked) {
    			counter++;
    		}
    	}
    	
    	document.xrefselector.checkboxcounter.value = counter + " selected";
    }
    However I should point out that using non alphanumeric characters (apart from underscores) in form element names can be hazardous and presents all sorts of problems. There's all sorts of ways around it - often best to replace characters that could potentially cause a problem with something less volatile (eg all whitespaces with underscores, removing square brackets, etc) or simply removing them altogether.

    php != html != javascript

    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    batfastad
    Guest
    That is awesome!!!

    Works straight off.


    Yeah I know it's not recommended to do that, but it's a really convenient way to get checkbox data into its own array for processing by PHP.

    In this case these pages are just being used on a local intranet.

    Thanks for the awesome help mate!!!


    Ben

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    However I should point out that using non alphanumeric characters (apart from underscores) in form element names can be hazardous and presents all sorts of problems.
    Only when it's about the first character. And, of course, should be avoid the usual submit separators (& | % ... aso.) which might counfuse the CGI code. For the , let's say, "neutral" special characters (such as underscore), the safest way is to use the full (square brackets) notation;

    document.forms['form_name'].elements['element_name']
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Kor
    Only when it's about the first character. And, of course, should be avoid the usual submit separators (& | % ... aso.) which might counfuse the CGI code. For the , let's say, "neutral" special characters (such as underscore), the safest way is to use the full (square brackets) notation;

    document.forms['form_name'].elements['element_name']
    Yeah mate, I know what you mean and agree, however with any reserved characters in form/variable/object/etc names then it does make work slightly trickier for subsequent coders to easily read. It works, and the same rules and logic apply, but for cleanliness sake I have always found it is better to steer clear of them.

    I guess it is "each to their own", but you rarely see unusual characters in form/variable/object/etc names in top notch code. It's a personal preference really of mine.

    Ultimately, I guess, the code works (thank you for the compliment batfastad) and that's what's really important =)

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •