0

hi i have a jquery table populate from datebase:

<?php 
$attributes = array('name' => 'users');

echo form_open('users/action',$attributes);?>
<table class="data display datatable" id="example">
    <thead>
        <tr>
          <th>Username</th>
          <th>Name</th>
          <th>Nickname</th>
          <th>Birthday</th>
          <th>Sex</th>
          <th>Address</th>
          <th><input type="checkbox" id="select all"></th>
        </tr>
    </thead>
    <tbody> 
<?php foreach($query->result_array() as $row): ?>
        <tr class="even gradeC">
            <td><?php echo anchor('users/details/'.$row['usrID'],$row['usrName']);?></td>
            <td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
            <td><?php echo $row['usrpNick'];?></td>
            <td><?php echo $row['usrpBday'];?></td>
            <td><?php echo $row['usrpSex'];?></td>
            <td><?php echo $row['usrpAddress'];?></td>
            <td><input type="checkbox" name="checkID[]" id="checkID" value="<?php echo $row['usrID'];?>" />

              <label for="checkID"></label></td>
        </tr>
<? endforeach; ?>
    </tbody>
</table>
  <input type="submit" name="Delete" id="Delete" value="Delete" class="btn btn-orange" />
  <input type="submit" name="Edit" id="Edit" value="Edit" class="btn btn-orange" onclick="return validateForm()" />
  <input type="submit" name="AddUser" id="Add User" value="Add User" class="btn btn-orange" />
 </form>

as you can see below,, i use checkID[] so that i can get every id of each user. i did make a onlick on edit button so that it will check if the id is empty or not. heres my js function

<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["users"]["checkID[]"].value
if (x==null || x=="")
  {
  alert("Please select user to edit.");
  return false;
  }
}
</script>
</head>

the problem now is that it will still alert even i check one user in the table..

even i use this:

<?php 
$attributes = array('name' => 'users' , 'onsubmit' =>"return validateForm()");

echo form_open('users/action',$attributes);?>

1 Answer 1

3

document.forms["users"]["checkID[]"] will give you an array of all the checkboxes with that name. You're asking for the value of the array which doesn't make sense. You'll need to loop through them to find out if any are checked with something like this:

function validateForm() {
   var checkboxes = document.forms["users"]["checkID[]"];
   for (var i = 0; i < checkboxes.length; i++) {
      if (checkboxes[i].checked) {
         return true;
      }
   }

   alert("Please select a user to edit.");
   return false;
}

I haven't tested it but that code should give you the general idea.

Sign up to request clarification or add additional context in comments.

1 Comment

it works if i have two users in a table, but if the table has only 1 user, it will still alert that i have not check any checkbox,,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.