Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2004, 04:04 PM
sam sam is offline
Authorized User
 
Join Date: Jun 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default php and javascript checkbox conflict

I created a page which creates an array of checkbox values:
Code:
<tr valign="top"> 
  <td width="230" class="newstext">
      <input name="checkbox[]" type="checkbox" class="form_white" value="Awarness Program for one month $900.00">
      <span class="newstext">One Month $900</span><br> 
      <input name="checkbox[]" type="checkbox" class="form_white" value="Awarness Program for one one week $225.00">
      <span class="newstext">One Week $225</span>
  </td>
  <td>
      <input name="checkbox[]" type="checkbox" class="form_white" value="Keep the Health Hotline running for one week $350.00">
      <span class="newstext">One Week $350</span><br> 
      <input name="checkbox[]" type="checkbox" class="form_white" value="Keep the Health Hotline running for one day $50.00">
      <span class="newstext">One Day $50</span> 
  </td>
</tr>
my problem occurred when I wanted to check on the client side if any thing was checked off both
Code:
      var boxct = document.form1.checkbox.length
      var bCB = false
      for(var ct=0; ct < boxct; ct++)
        {
            if(document.form1.checkbox[ct].checked)
                {
                    bCB = true;
                }
        }

and
Code:
      var boxct = document.form1.checkbox[].length
      var bCB = false
      for(var ct=0; ct < boxct; ct++)
        {
            if(document.form1.checkbox[][ct].checked)
                {
                    bCB = true;
                }
        }

got an error.
How does one deal with this?
Thanks,
Sam
__________________
~~~~~~~~~~~~~~~~~~~~~~~
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://ebcpro.com.com
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where EveryBusinessCounts
~~~~~~~~~~~~~~~~~~~~~~~
 
Old June 8th, 2004, 04:28 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Find and Replace all 'checkbox' with 'elements' and I think it will work.

Snib

<><
 
Old June 8th, 2004, 05:06 PM
sam sam is offline
Authorized User
 
Join Date: Jun 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

>Find and Replace all 'checkbox' with 'elements' and I think it will work.

not following what you mean do you mean
name="elements[]"
var boxct = document.form1.elements[].length?
thanks,
sam
 
Old June 8th, 2004, 05:28 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Checkboxes aren't arrays on the client-side the empty bracket syntax will create an array on the server-side, but with JS you have to take a different approach since JS will only access a checkbox individually.

One way of handling this is to fill in the numbers

<input name="checkbox[0]"...

<input name="checkbox[1]"...

<input name="checkbox[2]"...

And then modify your JS code accordingly, you have to reference the checkbox by its name specifically. Don't remember the JS syntax but maybe something like this:

if (document.getElementsByName('checkbox[' + counter + ']').checked == TRUE)
{
...

HTH!

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old June 8th, 2004, 05:41 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I'm not sure if what you're talking about will work, Rich. I have never heard of getElementsById.

What I was saying was to modify the code like so:

      var boxct = document.form1.elements.length;
      var bCB = false;
      for(var ct=0; ct < boxct; ct++)
        {
            if(document.form1.elements[ct].checked)
                {
                    bCB = true;
                }
        }

Snib

<><
 
Old June 8th, 2004, 05:50 PM
sam sam is offline
Authorized User
 
Join Date: Jun 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks Snib it worked:
Code:
      var boxct = document.form1.elements.length
      var bCB = false
      for(var ct=0; ct < boxct; ct++)
        {
            if(document.form1.elements[ct].checked && document.form1.elements[ct].type == 'checkbox')
                {
                                        bCB = true;
                }
        }
Thanks.
 
Old June 8th, 2004, 06:01 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I used 'getElementsByName' and not 'getElementsById' and just because you haven't heard of it doesn't mean it won't work ;). getElementsByName looks at the 'name' attribute to access the element.

Re: the elements property.
To get around possible conflicts with other checkboxes (not related to those you want to access) I would also check that the checkbox name is 'checkbox[]'.






Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old June 8th, 2004, 06:06 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Rich:

Sorry, I misread your code. I've been making mistakes like this all day

Anyway like you said, I would check if the name is "checkbox[]" or that the type is "checkbox", if some have a different name.

HTH,

Snib

<><





Similar Threads
Thread Thread Starter Forum Replies Last Post
Construct checkbox with Javascript BananaJim Javascript How-To 4 April 30th, 2012 02:32 AM
CheckBox enable in asp 2.0 with javascript bbsdev Javascript How-To 0 July 18th, 2006 12:27 PM
PHP Checkbox joanncae BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 15 May 25th, 2006 04:54 AM
php - checkbox msrinivas PHP How-To 0 March 7th, 2006 03:17 AM
Apache - PHP conflict dpchambe BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 April 23rd, 2004 10:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.