Jump to content

[SOLVED] some problems with checkbox


lcy

Recommended Posts

Hi there. Hope to get some help with checkbox from you guys...thanks in advance.

 

i have this as my checkboxes:

 

<?php while($row = mysql_fetch_array($result)) {  >

          <input type="checkbox" name="company_code[]" value="<?php echo "".$row['company_code']."";?>">

          <?php echo $row['company_code']; ?><br><?php  } ?>

 

Now I'm having three problems here:

i. to validate if no checkbox is checked

ii.to apply the check all and uncheck all function

iii.after the checkboxes are checked and the button submit is checked, the page return to itself. Here i need the checked checkbos to be remained checked.

 

How can i do all these with the checkbox i'm having. Appreciate any help given. Thanks again. :)

Link to comment
Share on other sites

You can validate it with JS on submit by doing the following:

function checkform ( form )
{

select = -1;
for (i=form.company_code.length-1; i > -1; i--) {
if (form.company_code[i].checked) {
select = i; i = -1;
}
}
if (select == -1) {
alert("You must select something")
return false;
}
  return true ;
}

Then Query your db for the values selected after insert and echo accordingly

Link to comment
Share on other sites

When you submit a checkbox it will send the value in the attribute value if and only if it is checked.

 

<?php while ($row = mysql_fetch_assoc($result)): ?>
  <input type="checkbox" name="company_code[]" value="<?php print $row['company_code']; ?>"
    <?php ((isset($_POST['company_code']) && in_array($row['company_code'], $_POST['company_code'])) ? ' checked="checked"' : ''); ?>>
  <?php print $row['company_code']; ?>
<?php endwhile; ?>

Link to comment
Share on other sites

Thanks for the solutions provided. I tried but they doesn't work. For the remained checking the checked checkbox, i've try another way and it's working.

 

<?php while($row = mysql_fetch_array($result)){ ?>

        <input type="checkbox" name="company_code[]" value="<?php echo "".$row['company_code'].""; ?>"

        <?php if($company_code!=NULL){foreach ($company_code as $company_code_ticked) 

          {if($company_code_ticked == $row['company_code']){ echo "checked";}}}?>>

        <?php echo $row['company_code']; ?><br>

<?php  } ?>

 

Is there any other way to solve my first and second problems? Anyway, appreciate solutions provided.  :)

Link to comment
Share on other sites

I've got the solution for the first problem...

 

function validateCB(theName){

          var counter=0;

          var cb=document.getElementsByName(theName)

          for (i=0; i<cb.length; i++) {

          if((cb.tagName=='INPUT')&&(cb.type=='checkbox')){

          if (cb.checked)

            counter++;

          }

          }

          if (counter==0) { 

        return false;

        }

        return true;

        }

 

        if (!validateCB('company_code[]')) {

  alert("Please select company code");

  return false;

}

 

If anyone is having the solution for checking and unchecking all checkboxes, help is much appreciated!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.