javascript with onclick="check()" problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
helloise
Forum Commoner
Posts: 31
Joined: Tue Sep 21, 2010 4:20 am

javascript with onclick="check()" problem

Post by helloise »

i have the code:

Code: Select all

 foreach($product_names as $product_row) { ?>
            <tr>
                <td >&nbsp;</td><td width='200px'><?php echo  $product_row->getName();?></td>
                <td width='200px'><input type="checkbox" name="graphic[]" value="<?php echo  $product_row->getId();?>"  onclick="check()"/></td>
                <td width='200px'><input type="checkbox" name="text[]"    value="<?php echo  $product_row->getId();?>" onclick="check()"/></td>
            </tr>
            <?php } ?>
and a submit button on the page.
the i also have the javascript code for the onclick="check()" :

Code: Select all

<html>
<head>
    <script type="text/javascript">
        function check()
        {
            var graphics = document.getElementsByName("graphic[]");
             
            for(i=0;i<graphics.length;i++)
            {
                if(graphics[i].checked)
                {
                    if(document.getElementById('submit').style.display=='none')
                    {
                        document.getElementById('submit').style.display='block';
                    }
                }
            }
            var text = document.getElementsByName("text[]");
            for(i=0;i<text.length;i++)
            {
                if(text[i].checked)
                {
                    if(document.getElementById('submit').style.display=='none')
                    {
                        document.getElementById('submit').style.display='block';
                    }
                }
            }  
        }
    </script>
</head>
so what all this means is i have two arrays of checkboxes for each product. as soon as i check any checkbox the submit button appears. i click on the submit button and i go to the next form. the problem now comes in if i click on the "back" button to go back to the previous page via the bwrowser..the submit button is disabled althou all my checkboxes that i checked, are still checked....i want the submit button to show???

please help??
thanks
Last edited by helloise on Fri Jan 07, 2011 5:48 am, edited 2 times in total.
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: clicking browser "back" icon to go one page back problem

Post by spedula »

Create a function that checks if the checkbox is checked and un-hided the submit button. Apply this function to the onload property of the body tag.

<body onload="checkBox()">

Better idea than hiding the submit button would be to just disable it. But thats just like my opinion, man. :D

document.getElementById('submit_button').disabled = 1
helloise
Forum Commoner
Posts: 31
Joined: Tue Sep 21, 2010 4:20 am

Re: javascript with onclick="check()" problem

Post by helloise »

thanks!
the problem is if i click the browser's back button all my checked boxes are there but the submit button is hidden...so i must everytime CLICK on a testbox to see the submit button...thats not how i want it..if i hit the back button and the checked boxes are there...show the submit button WITHOUT haveing to click on a checkbox..

hope it makes sense :D
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: javascript with onclick="check()" problem

Post by spedula »

Yes, thats what I was getting at.

First off, when you press back are the check box's that were check still checked or is the form reset (no selections were saved) ?

If the selections were saved on back button press, which they should be.

You need to write up a function that checks if a checkbox is checked and if they are, display the submit button. Then call this function when the page loads by appending it to the body tag.

Something like this:

Code: Select all

// your checking checkbox function
function checkBox() 
{
   var text = document.getElementsByName("text[]");
   for(i=0;i<text.length;i++)
   {
       if(text[i].checked)
       {
           if(document.getElementById('submit').style.display=='none')
           {
               document.getElementById('submit').style.display='block';
           }
       }
   }  
}

// what your body tag should look like
<body onload="checkBox()">
This will tell the script to do the check and display the button every time the page load/reload/back button/etc...
Post Reply