Sama seperti isu sebelum ini. Ini validate di mana user perlu pilih sekurang-kurangnya satu checkbox.
Masalah yang sama timbul. Jika satu sahaja data, array tidak berjalan. Jadi, check dahulu samada array cuma satu data sahaja atau lebih.
<html>
<head>
<title>Mi-Scramble</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function validatePwd() {
if(document.getElementsByName('userassign[]').length == 1)
{
if(document.myForm.userassign.checked)
{
return true;
} else {
alert ("You must select minimum one user! ");
return false;
}
} else {
var flag = 0;
for (var i = 0; i< document.getElementsByName('userassign[]').length; i++) {
if(document.myForm["userassign[]"][i].checked){
flag ++;
}
}
if (flag < 1) {
alert ("You must select minimum one user!");
return false;
}
return true;
}
}
</script>
</head>
<body>
<form action="assignuserupd.php" method="post" name=myForm onSubmit="return validatePwd()">
<table class="responsive table table-striped">
<thead>
<tr>
<th style="">User Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$sqlrtbl="SQL";
$resulrtbl=pg_query($sqlrtbl);
while($rowr = pg_fetch_assoc($resulrtbl))
{
?>
<tr>
<td><?php echo $rowr['username']; ?></td>
<td>
<input type="checkbox" name="userassign[]" value="<?php echo $rowr['username']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Submit" class="btn btn-primary">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
Sourcecode for blogspot http://codeformatter.blogspot.com/
0 comments:
Post a Comment