| | |
using javascript to manipulate checkboxes that use PHP arrays
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 15
Reputation:
Solved Threads: 0
Hello!
I was wondering if I could get some assistance with something that is probably easy for even an amateur javascript coder, but, I just can't seem to get after trying every iteration of code I can imagine.
The deal is, is that I'm trying to put together a PHP search form, with checkboxes for countries (USA, CAN, MEX), and a set of checkboxes for each state in each country, including an "ALL STATES" checkbox for each country.
I'm trying to input form controls where, if someone checks the "ALL STATES" box for any country, it will automatically uncheck any state checkboxes for that country. However, since the checkbox name attributes need to go to PHP as an array, I can't remove the square brackets in the name (I tried escaping the characters with up to 2 backslashes, but that didn't help).
When I tried referring to the checkboxes via getElementById, that also failed. My code for the script as well as the form (abbreviated for convenience and sanity's sake) is below.
If anyone can point out to me where I'm going wrong, I'd appreciate it!
...and now the form and checkbox fields:
I was wondering if I could get some assistance with something that is probably easy for even an amateur javascript coder, but, I just can't seem to get after trying every iteration of code I can imagine.
The deal is, is that I'm trying to put together a PHP search form, with checkboxes for countries (USA, CAN, MEX), and a set of checkboxes for each state in each country, including an "ALL STATES" checkbox for each country.
I'm trying to input form controls where, if someone checks the "ALL STATES" box for any country, it will automatically uncheck any state checkboxes for that country. However, since the checkbox name attributes need to go to PHP as an array, I can't remove the square brackets in the name (I tried escaping the characters with up to 2 backslashes, but that didn't help).
When I tried referring to the checkboxes via getElementById, that also failed. My code for the script as well as the form (abbreviated for convenience and sanity's sake) is below.
If anyone can point out to me where I'm going wrong, I'd appreciate it!
javascript Syntax (Toggle Plain Text)
<script language="JavaScript"> <!-- Begin function Check() { var USA_0 = document.getElementById("Search_Origin_State_USA_0"); var USA_1 = document.getElementById("Search_Origin_State_USA_1"); if(USA_0.checked == true) { USA_1.checked = false ; } else { USA_1.checked = true ; } } // End --> </script>
...and now the form and checkbox fields:
html Syntax (Toggle Plain Text)
<form action="test.php" method="post" name="load_search" target="_parent"> <input type="checkbox" name="Search_Origin_State_USA[%]" value="%" onClick="Check()" id="Search_Origin_State_USA_0" /> All US States <input type="checkbox" name="Search_Origin_State_USA[AK]" value="AK" id="Search_Origin_State_USA_1" /> AK</label>
Hi jay,
here's a simple demo that converts all checked items, into an array of length, using a single hidden field:
startpage
and in your test.php:
-essential
here's a simple demo that converts all checked items, into an array of length, using a single hidden field:
startpage
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css_level21" media="screen"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>http://www.daniweb.com :: DHTML JavaScript / AJAX</title> <style type="text/css"> /* <![CDATA[ */ td, th { background-color : #f0f0f0; font : bold normal normal 80%/1.5 Verdana, Arial, sans-serif; color : #405060; letter-spacing : 3px; border : 1px solid #ccc; padding : .500em 1em .500em 1em; } table { width : auto; border : 1px solid #405060; border-collapse : collapse; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ var form; var Check = ( function() { var hfield = (( hfield = document.getElementById("arr")) ? hfield : arr ); var element = ( function( index ) { var index = index || 0; var ids = "Search_Origin_State_USA_"; var obj; return obj = (( obj = document.getElementById( ids + index )) ? obj : document.all[ ids + index ] ); } ); var getState = [ ]; var count = 0; for( x = 1; element( x ); x++ ) { if( element( x ).type === "checkbox" ) { if ( element( 0 ).checked ) { element( x ).checked = 0; } else if ( element( x ).checked ) { getState[ count ] = element( x ).value; count++; continue; } } } hfield.value = getState.join(","); alert( hfield.value ); // output >>> } ); onload = function() { if( "load_search" in document ) { form = load_search; } else { (( form = document.getElementById("load_search")) ? form : form = load_search ); } var chb0 = (( chb0 = document.getElementById("Search_Origin_State_USA_0")) ? chb0 : Search_Origin_State_USA_0 ) chb0.onclick = Check; form.onsubmit = Check; }; // ]]> </script> </head> <body> <div id="main"> <form id="load_search" action="test.php" method="post"> <table id="table" frame="void" rules="none" summary="JavaScript :: Live Demo!" cellspacing="4" cellpadding="4"> <tr> <th><label for="Search_Origin_State_USA_0">All US States :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_0" name="Search_Origin_State_USA_0" value="ALL" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_1">AK :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_1" name="Search_Origin_State_USA_1" value="AK" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_2">CH :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_2" name="Search_Origin_State_USA_2" value="CH" /></td> </tr> </table> <div><input type="hidden" id="arr" name="arr" value="" /><input type="submit" value="- submit -" /></div> </form> </div> </body> </html>
and in your test.php:
php Syntax (Toggle Plain Text)
<?php $jArray = new Array( $_POST['arr'] ); /* gathered from Javascipt Array */ ?>
-essential
Last edited by essential; Aug 10th, 2009 at 6:19 am.
•
•
Join Date: Jun 2009
Posts: 15
Reputation:
Solved Threads: 0
Thanks for the suggestion! Now, I tried it, but it didn't have any effect on checkbox behavior. Is your suggestion intended to do so, or is it intended to allow me to create checkboxes that don't require square brackets in the name attribute, thus allowing me to use my own code to affect the boxes' behavior?
Hi jay,
it's been tested under Opera8, and working smoothly on my expected output. If your code works, then maybe i need to apply a little re-adjustment on the lines, so that it will be able to support your browser. I'll post back when i'm done to it...
it's been tested under Opera8, and working smoothly on my expected output. If your code works, then maybe i need to apply a little re-adjustment on the lines, so that it will be able to support your browser. I'll post back when i'm done to it...
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
•
•
Join Date: Jun 2009
Posts: 15
Reputation:
Solved Threads: 0
•
•
•
•
Hi jay,
it's been tested under Opera8, and working smoothly on my expected output. If your code works, then maybe i need to apply a little re-adjustment on the lines, so that it will be able to support your browser. I'll post back when i'm done to it...
I'm currently using FireFox 3.5.2, if that's any help.
I was able to use my own code (see below), to affect the checkboxes behavior, alongside yours, to create the array needed to execute the MySQL query on test.php (it also pops up the results of the javascript- but I did need to include the onClick"" attribute to the "ALL STATES" box.
If I try to run the query Omitting my code, a blank pop-up does come up if I check the "ALL STATES" box, and none of the selected state boxes gets unchecked.
javascript Syntax (Toggle Plain Text)
<script language="JavaScript"> <!-- Begin function Check() { if(document.load_search.Search_Origin_State_USA_0.checked == true) { document.load_search.Search_Origin_State_USA_1.checked = false ; document.load_search.Search_Origin_State_USA_2.checked = false ; document.load_search.Search_Origin_State_USA_3.checked = false ; document.load_search.Search_Origin_State_USA_4.checked = false ; document.load_search.Search_Origin_State_USA_5.checked = false ; } } // End --> </script>
html Syntax (Toggle Plain Text)
<label for="Search_Origin_State_USA_0"> <input type="checkbox" name="Search_Origin_State_USA_0" value="%" id="Search_Origin_State_USA_0" onClick="Check()" /> All US States</label> </td> </tr> <tr> <td><label for="Search_Origin_State_USA_1"> <input type="checkbox" name="Search_Origin_State_USA_1" value="AK" id="Search_Origin_State_USA_1" /> AK</label></td> <td><label for="Search_Origin_State_USA_2"> <input type="checkbox" name="Search_Origin_State_USA_2" value="AL" id="Search_Origin_State_USA_2" /> AL</label></td> <td><label for="Search_Origin_State_USA_3"> <input type="checkbox" name="Search_Origin_State_USA_3" value="AR" id="Search_Origin_State_USA_3" /> AR</label></td> <td><label for="Search_Origin_State_USA_4"> <input type="checkbox" name="Search_Origin_State_USA_4" value="AZ" id="Search_Origin_State_USA_4" /> AZ</label></td> <td><label for="Search_Origin_State_USA_5"> <input type="checkbox" name="Search_Origin_State_USA_5" value="CA" id="Search_Origin_State_USA_5" />
Last edited by jay.barnes; Aug 10th, 2009 at 12:52 pm.
Hi,
here is the finalized version of the code, and should be able to penetrate all modern browsers on the market, including all versions of IE.
exept from the older version of the ( ns modes ) that does not understand the
NOTE: please run the entire document first, before you apply new changes over its lines'.
regards
-essential
here is the finalized version of the code, and should be able to penetrate all modern browsers on the market, including all versions of IE.
exept from the older version of the ( ns modes ) that does not understand the
getElementById method. javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css_level21" media="screen"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>http://www.daniweb.com :: DHTML JavaScript / AJAX</title> <style type="text/css"> /* <![CDATA[ */ td, th { background-color : #f0f0f0; font : bold normal normal 80%/1.5 Verdana, Arial, sans-serif; color : #405060; letter-spacing : 3px; border : 1px solid #ccc; padding : .500em 1em .500em 1em; } table { width : auto; border : 1px solid #405060; border-collapse : collapse; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ var mode = (( document.all && !document.getElementById ) ? 1 : 0 ); var elem = function( xid ) { var xid = xid; var obj = [ document.getElementById( xid ), document.all[ xid ] ][ mode ]; if ( obj ) { return obj; } return false; }; var addEvent = function( elm, evt, fun ) { var elm = elm; var evt = evt; var func = fun; if ( mode ) { if ( elm ) { elm.attachEvent( "on" + evt, fun ); return; } alert( "event is not supported: on" + evt ); return false; } else if ( !mode ) { if ( elm ) { elm.addEventListener( evt, fun, false ); return; } alert( "event is not supported: on" + evt ); return false; } elm[ "on" + evt ] = fun; }; var check = function() { var pref = "Search_Origin_State_USA_"; addEvent( elem( pref + 0 ), "click", function() { var allStates = elem( pref + 0 ) || this; if ( allStates.checked ) { for ( x = 1; !!( elem( pref + x )); x++ ) { elem( pref + x ).checked = 0; } delete x; return; } } ); addEvent( elem( "load_search" ), "submit", function() { var count = 0; var selectedStates = new Array(); for ( x = 1; !!( elem( pref + x )); x++ ) { if ( elem( pref + x ).checked ) { selectedStates[ count ] = elem( pref + x ).value; count++; } } delete x; elem("arr").value = selectedStates; alert( elem("arr").value ) // hidden field value. alert( "\nArray length: " + selectedStates.length + "\nSelected States: " + selectedStates.join(", ") ); // Tests Output >>> } ); }; addEvent( this || window, "load", check ); // ]]> </script> </head> <body> <div id="main"> <form id="load_search" action="test.php" method="post"> <table id="table" frame="void" rules="none" summary="JavaScript :: Live Demo!" cellspacing="4" cellpadding="4"> <tr> <th><label for="Search_Origin_State_USA_0">All US States :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_0" name="Search_Origin_State_USA_0" value="ALL" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_1">AK :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_1" name="Search_Origin_State_USA_1" value="AK" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_2">CH :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_2" name="Search_Origin_State_USA_2" value="CH" /></td> </tr> </table> <div><input type="hidden" id="arr" name="arr" value="" /><input type="submit" value="- submit -" /></div> </form> </div> </body> </html>
NOTE: please run the entire document first, before you apply new changes over its lines'.
regards
-essential
Last edited by essential; Aug 10th, 2009 at 2:38 pm.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
•
•
Join Date: Jun 2009
Posts: 15
Reputation:
Solved Threads: 0
Sorry, that didn't seem to do it either. This, however, does seem to work:
with HTML:
That seeming to work properly, I have just two questions:
1. is there any way you might be able to strip the code from your suggested script that affects the checkbox behavior, and simply leave the code that allows me to create the array?
2. Do you have any suggestions on how I might create a loop in the javascript so I don't need to do 52 different lines (one for each state + DC + 'all states'?
I really gotta start reading up on Javascript... :-p
javascript Syntax (Toggle Plain Text)
<script language="JavaScript"> <!-- Begin function UncheckUSAStates() { status==status; if(document.load_search.Search_Origin_State_USA_0.checked == true) { document.load_search.Search_Origin_State_USA_0.checked = true ; document.load_search.Search_Origin_State_USA_1.checked = false ; document.load_search.Search_Origin_State_USA_2.checked = false ; document.load_search.Search_Origin_State_USA_3.checked = false ; document.load_search.Search_Origin_State_USA_4.checked = false ; document.load_search.Search_Origin_State_USA_5.checked = false ; document.load_search.Search_Origin_State_USA_6.checked = false ; document.load_search.Search_Origin_State_USA_7.checked = false ; document.load_search.Search_Origin_State_USA_8.checked = false ; document.load_search.Search_Origin_State_USA_9.checked = false ; document.load_search.Search_Origin_State_USA_10.checked = false ; document.load_search.Search_Origin_State_USA_11.checked = false ; document.load_search.Search_Origin_State_USA_12.checked = false ; document.load_search.Search_Origin_State_USA_13.checked = false ; document.load_search.Search_Origin_State_USA_14.checked = false ; document.load_search.Search_Origin_State_USA_15.checked = false ; document.load_search.Search_Origin_State_USA_16.checked = false ; document.load_search.Search_Origin_State_USA_17.checked = false ; document.load_search.Search_Origin_State_USA_18.checked = false ; document.load_search.Search_Origin_State_USA_19.checked = false ; document.load_search.Search_Origin_State_USA_20.checked = false ; document.load_search.Search_Origin_State_USA_21.checked = false ; document.load_search.Search_Origin_State_USA_22.checked = false ; document.load_search.Search_Origin_State_USA_23.checked = false ; document.load_search.Search_Origin_State_USA_24.checked = false ; document.load_search.Search_Origin_State_USA_25.checked = false ; document.load_search.Search_Origin_State_USA_26.checked = false ; document.load_search.Search_Origin_State_USA_27.checked = false ; document.load_search.Search_Origin_State_USA_28.checked = false ; document.load_search.Search_Origin_State_USA_29.checked = false ; document.load_search.Search_Origin_State_USA_30.checked = false ; document.load_search.Search_Origin_State_USA_31.checked = false ; document.load_search.Search_Origin_State_USA_32.checked = false ; document.load_search.Search_Origin_State_USA_33.checked = false ; document.load_search.Search_Origin_State_USA_34.checked = false ; document.load_search.Search_Origin_State_USA_35.checked = false ; document.load_search.Search_Origin_State_USA_36.checked = false ; document.load_search.Search_Origin_State_USA_37.checked = false ; document.load_search.Search_Origin_State_USA_38.checked = false ; document.load_search.Search_Origin_State_USA_39.checked = false ; document.load_search.Search_Origin_State_USA_40.checked = false ; document.load_search.Search_Origin_State_USA_41.checked = false ; document.load_search.Search_Origin_State_USA_42.checked = false ; document.load_search.Search_Origin_State_USA_43.checked = false ; document.load_search.Search_Origin_State_USA_44.checked = false ; document.load_search.Search_Origin_State_USA_45.checked = false ; document.load_search.Search_Origin_State_USA_46.checked = false ; document.load_search.Search_Origin_State_USA_47.checked = false ; document.load_search.Search_Origin_State_USA_48.checked = false ; document.load_search.Search_Origin_State_USA_49.checked = false ; document.load_search.Search_Origin_State_USA_50.checked = false ; document.load_search.Search_Origin_State_USA_51.checked = false ; } } // End --> </script> <script type="text/javascript"> // <![CDATA[ var form; var UncheckUSAStates = ( function() { var hfield = (( hfield = document.getElementById("arr")) ? hfield : arr ); var element = ( function( index ) { var index = index || 0; var ids = "Search_Origin_State_USA_"; var obj; return obj = (( obj = document.getElementById( ids + index )) ? obj : document.all[ ids + index ] ); } ); var getState = [ ]; var count = 0; for( x = 1; element( x ); x++ ) { if( element( x ).type === "checkbox" ) { if ( element( 0 ).checked ) { element( x ).checked = 0; } else if ( element( x ).checked ) { getState[ count ] = element( x ).value; count++; continue; } } } hfield.value = getState.join(","); alert( hfield.value ); // output >>> } ); onload = function() { if( "load_search" in document ) { form = load_search; } else { (( form = document.getElementById("load_search")) ? form : form = load_search ); } var chb0 = (( chb0 = document.getElementById("Search_Origin_State_USA_0")) ? chb0 : Search_Origin_State_USA_0 ) chb0.onclick = Check; form.onsubmit = Check; }; // ]]> </script>
with HTML:
HTML Syntax (Toggle Plain Text)
<label for="Search_Origin_State_USA_0"> <input type="checkbox" name="Search_Origin_State_USA_0" value="%" id="Search_Origin_State_USA_0" onClick="UncheckUSAStates()" /> All US States</label> </td> </tr> <tr> <td><label for="Search_Origin_State_USA_1"> <input type="checkbox" name="Search_Origin_State_USA_1" value="AK" id="Search_Origin_State_USA_1" /> AK</label></td> <td><label for="Search_Origin_State_USA_2"> <input type="checkbox" name="Search_Origin_State_USA_2" value="AL" id="Search_Origin_State_USA_2" /> AL</label></td> <td><label for="Search_Origin_State_USA_3"> <input type="checkbox" name="Search_Origin_State_USA_3" value="AR" id="Search_Origin_State_USA_3" /> AR</label></td> <td><label for="Search_Origin_State_USA_4"> <input type="checkbox" name="Search_Origin_State_USA_4" value="AZ" id="Search_Origin_State_USA_4" /> AZ</label></td> <td><label for="Search_Origin_State_USA_5"> <input type="checkbox" name="Search_Origin_State_USA_5" value="CA" id="Search_Origin_State_USA_5" /> CA</label></td> <td><label>
That seeming to work properly, I have just two questions:
1. is there any way you might be able to strip the code from your suggested script that affects the checkbox behavior, and simply leave the code that allows me to create the array?
2. Do you have any suggestions on how I might create a loop in the javascript so I don't need to do 52 different lines (one for each state + DC + 'all states'?
I really gotta start reading up on Javascript... :-p
I wonder why it didnt work -- hmm, that's pretty wierd for a powerful browser, ( not-to-output-anything from the code) like with the one that you are using right now.
Regarding about your questions:
1- if that's what you preferred to used. Then you should maintain those ids on the lines and get your arrays' on a normal stage in your test.php page, which will not require editing of a single line.
2- i think we will be needing some workout on this one. ( walk-the-dom-procedure can be implemented or looping with some arrays of ids can be implemented too. )
ok, i'll start all over and i'll fix your code.
- Thanks for the info, of letting me know the parts of the code that does work.
Will post back later...
-essential
Regarding about your questions:
1- if that's what you preferred to used. Then you should maintain those ids on the lines and get your arrays' on a normal stage in your test.php page, which will not require editing of a single line.
2- i think we will be needing some workout on this one. ( walk-the-dom-procedure can be implemented or looping with some arrays of ids can be implemented too. )
ok, i'll start all over and i'll fix your code.
- Thanks for the info, of letting me know the parts of the code that does work.
Will post back later...
-essential
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Hi again jay,
lets have another try for this document!
As you can see, i've included a new functionality that remebers all checked items in the field, so that it will be able to bring back state of the checked items when you unchecked the All States option.
This code is tested under OPERA and working smoothly on expected output.
Hope that your browser would be able to adapt the code below:
-essential
lets have another try for this document!
As you can see, i've included a new functionality that remebers all checked items in the field, so that it will be able to bring back state of the checked items when you unchecked the All States option.
This code is tested under OPERA and working smoothly on expected output.
Hope that your browser would be able to adapt the code below:
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/css" href="#css_level21" media="screen"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <title>http://www.daniweb.com :: DHTML JavaScript / AJAX</title> <style type="text/css"> /* <![CDATA[ */ td, th { background-color : #f0f0f0; font : bold normal normal 80%/1.5 Verdana, Arial, sans-serif; color : #405060; letter-spacing : 3px; border : 1px solid #ccc; padding : .500em 1em .500em 1em; } table { width : auto; border : 1px solid #405060; border-collapse : collapse; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ var Element = ( function( xtag, ids ) { this.ids = ids; this.xtag = (( this.xtag = document.getElementsByTagName( xtag )) ? this.xtag : this.xtag = document.all.tags( xtag ))[ this.ids ]; } ); // PROTOTYPING Element.prototype = { getElement : ( function( xtags ) { this.xtags = xtags; return (( this.xtags = this.xtag.getElementsByTagName( xtags )) ? this.xtags : this.xtags = this.xtag.all.tags( xtags )); } ) }; var UncheckUSAStates = ( function() { var pref = "Search_Origin_State_USA_"; var form = new Element("form", "load_search"); for ( var x = 1; !!( form.getElement("input")[ x ] ); x++ ) { if ( form.getElement( "input" )[ x ].type === "checkbox" ) { if ( form.getElement("input")[ String( pref + 0 ) ].checked ) { form.getElement( "input" )[ x ].checked = 0; } else { if ( form.getElement( "input" )[ x ].hasAttribute("checked")) { form.getElement( "input" )[ x ].checked = form.getElement( "input" )[ x ].defaultChecked; } } continue; } } } ); onload = function() { var frm = new Element( "form", "load_search" ); var input; for ( var y = 1; ( input = frm.getElement("input")[ y ] ); y++ ) { if ( input.type === "checkbox" ) { input.onclick = ( function() { if ( this.checked ) { this.setAttribute( "checked", "checked" ); } else { if ( this.hasAttribute( "checked" )) { this.removeAttribute("checked"); } return false; } } ); } } }; // ]]> </script> </head> <body> <div id="main"> <form id="load_search" action="test.php" method="post"> <table id="table" frame="void" rules="none" summary="JavaScript :: Live Demo!" cellspacing="4" cellpadding="4"> <tr> <th><label for="Search_Origin_State_USA_0">All US States :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_0" name="Search_Origin_State_USA_0[%]" value="%" onclick="UncheckUSAStates();" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_1">AK :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_1" name="Search_Origin_State_USA[AK]" value="AK" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_2">AL :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_2" name="Search_Origin_State_USA[AL]" value="AL" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_3">AR :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_3" name="Search_Origin_State_USA[AR]" value="AR" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_4">AZ :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_4" name="Search_Origin_State_USA[AZ]" value="AZ" /></td> </tr> <tr> <th><label for="Search_Origin_State_USA_5">CA :</label></th> <td><input type="checkbox" id="Search_Origin_State_USA_5" name="Search_Origin_State_USA[CA]" value="CA" /></td> </tr> </table> <div><input type="hidden" id="arr" name="arr" value="" /><input type="submit" value="- submit -" /></div> </form> </div> </body> </html>
-essential
Last edited by essential; Aug 11th, 2009 at 4:37 am.
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
![]() |
Similar Threads
- how do i pass arrays in javascript to php using POST method (JavaScript / DHTML / AJAX)
- access JavaScript function return value from PHP (JavaScript / DHTML / AJAX)
- How use php array with javascript array (PHP)
- How to call a PHP function from Javascript and return the results back into Javascrip (PHP)
- Javascript Function return value into PHP or not ?? Please help !! (JavaScript / DHTML / AJAX)
- Accessing HTML checkboxes with php and javascript (PHP)
- PHP Arrays - I can't do em! (PHP)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Exception handling statement.
- Next Thread: showing and hiding divs on click
| Thread Tools | Search this Thread |
acid2 ajax ajaxexample ajaxjspservlets array browser captcha captchaformproblem cart checkbox child class close codes date debugger decimal dependent disablefirebug dom editor element embed engine enter events explorer ext file firefox focus form forms frameworks getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl listbox maps marquee masterpage math matrixcaptcha media menu mp4 object onerror onmouseoutdivproblem onmouseover onreadystatechange parent paypal pdf php position post programming prototype rated redirect runtime safari scale scriptlets scroll search security session shopping size software star starrating stars synchronous toggle unicode variables web webservice wysiwyg \n





