php - form validation using javascript in codeigniter - Stack Overflow

hi i have a jquery table populate from datebase:<?php $attributes = array('name' => &#

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);?>

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);?>
Share Improve this question edited May 26, 2011 at 5:27 kester martinez asked May 26, 2011 at 5:13 kester martinezkester martinez 4316 gold badges14 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744781228a4593352.html

相关推荐

  • php - form validation using javascript in codeigniter - Stack Overflow

    hi i have a jquery table populate from datebase:<?php $attributes = array('name' => &#

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信