Forum Moderators: coopster
<html>
<?php
include 'connect.php';
$sqlquery = "SELECT * FROM $db_table "; // query on table
$sqlresult = mysql_query($sqlquery, $db);
$count = mysql_num_rows($sqlresult); // count query result
?>
<table width="80%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form" method="post" action="delete2.php">
<table width="80%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td> </td>
<td colspan="4" align="left"><strong><font face="verdana" color="blue">Table 1</font></strong></td>
<td> </td>
</tr>
<tr>
<td align="center">#</td>
<td align="center"><strong>Variable 0</strong></td>
<td align="center"><strong>Variable 1</strong></td>
<td align="center"><strong>Variable 2</strong></td>
<td align="center"><strong>Variable 3</strong></td>
<td align="center"><strong>Variable 4</strong></td>
</tr>
<?php
while($rows = mysql_fetch_array($sqlresult)){
?>
<tr>
<td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><? echo $rows['val0']; ?></td>
<td><? echo $rows['val1']; ?></td>
<td><? echo $rows['val2']; ?></td>
<td><? echo $rows['val3']; ?></td>
<td><? echo $rows['val4']; ?></td>
</tr>
<?php
}
?>
<tr>
<td bgcolor="red"><input id='delete' type='submit' name='delete' value='Delete'></td>
<input type="button" value="Refresh Email List" onClick="window.location.reload()">
</tr>
</form>
</body>
</html>
Something like....
<form action="yourscript.php" method="post" onSubmit="confirmDeletes(this);">
....
<input type="checkbox" name="all_on" id="all_on" onClick="toggleChecks(this.form,1);"> All On <br>
<input type="checkbox" name="all_off" id="all_off" onClick="toggleChecks(this.form,0);"> All Off
.....
<input type="submit" value="Delete Selected">
</form>
<script type="text/javascript">
// this confirms deletes. Note that by returning false to the form,
// it allows Javascript to manage the delete and will not submit
// twice. But if JS is not emabled, the submit button will allow it to work.
function confirmDeletes (form) {
var msg = ''; var goForIt = false;
for (i=0;i<form.elements.length;i++) {
// Yes eval is evil, this should use document.getElementById
// but it will work
var type = eval('form.elements[' + i + '].type');
if (type == 'checkbox') {
var chked = eval('form.elements[' + i + '].checked');
if (chked == true) {
msg = 'You have elements in this form \\n' +
'selected for deletion. Is this what you \\n' +
'really want?';
break;
}
}
}
if (msg != '') {
goForIt = confirm(msg);
if (goForIt == true) { form.submit(); }
}
else { form.submit(); }
return false;
}
// This is your checkbox toggle. It will toggle all checkboxes
// on or off EXCEPT all_on and all_off
function toggleChecks(form,onoff) {
var sw = (onoff==1)?true:false;
for (i=0;i<form.elements.length;i++) {
var obj = form.elements[i];
if ((obj.type=='checkbox') && (obj.name != 'all_on') && (obj.name != 'all_off')) {
obj.checked = sw;
}
}
if (onoff == 1) { form.all_off.checked = false; }
else { form.all_on.checked = false; }
}
</script>
You can also just pass the checked state of a single box; if the checked state is true check all, if it's not, uncheck all.
About return false: Mot people avoid submit and use button because it would submit twice. By returning false form the checkDeletes function, it tells the browser to NOT do it's normal behavior - submit the form. This allows JS to manage it with form.submit().
If JS is disabled, the form will still work because submit submits; button does nothing without Javascript.