Hi All,

I need to change the default values of checkboxes to 1 for checked and 0 for unchecked instead of 0 for checked and NULL for unchecked.

If this is possible and how is it done.

Here is the script that I am using:


THE UPDATE FORM

<?php
function displayMenu(){
$id = "1";


	

$query=" SELECT * FROM menu_1 WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);

while($row = mysql_fetch_array($result))
  {

$item = $row['item'];
$level1 = $row['level1'];
$level2 =  $row['level2'];
$level3 =  $row['level3'];
$level4 =  $row['level4'];
$level5 =  $row['level5'];
$level6 =  $row['level6'];
$level7 =  $row['level7'];
$level8 =  $row['level8'];
$level9 =  $row['level9'];	

  
echo"<table style='font-size:12px' align='center' border='1' cellspacing='0' cellpadding='5'>";
echo "<tr><td>ID</td><td>ITEM</td><td>Level 1</td><td>Level 2</td><td>Level 3</td><td>Level 4</td><td>Level 5</td><td>Level 6</td><td>Level 7</td><td>Level 8</td><td>Level 9</td></tr>";


echo"<tr align='center'>";	

	echo"<td><input size='10' type='text' name='u_id' value='$id'></td>";
	echo"<td><input size='10' type='text' name='u_item' value='$item'></td>";

	if($level1=="1") {
		echo "<td><input type='checkbox' name='u_level1' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level1' value='0' /></td>\n";    
	}
	
	if($level2=="1") {
		echo "<td><input type='checkbox' name='u_level2' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level2' value='0' /></td>\n";    
	}
	
	if($level3=="1") {
		echo "<td><input type='checkbox' name='u_level3' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level3' value='0' /></td>\n";    
	}
	
	if($level4=="1") {
		echo "<td><input type='checkbox' name='u_level4' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level4' value='0' /></td>\n";    
	}
	
	if($level5=="1") {
		echo "<td><input type='checkbox' name='u_level5' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level5' value='0' /></td>\n";    
	}
	
	if($level6=="1") {
		echo "<td><input type='checkbox' name='u_level6' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level6' value='0' /></td>\n";    
	}
	
	if($level7=="1") {
		echo "<td><input type='checkbox' name='u_level7' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level7' value='0' /></td>\n";    
	}
	
	if($level8=="1") {
		echo "<td><input type='checkbox' name='u_level8' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level8' value='0' /></td>\n";    
	}
	
	if($level9=="1") {
		echo "<td><input type='checkbox' name='u_level9' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level9' value='0' /></td>\n";    
	}
echo "</tr>";	
}

echo "</table>";


		

}




?>

EXECUTION SCRIPT

<?php

$p_id = $_POST['u_id'];
$p_item = $_POST['u_item'];
$p_level1 = $_POST['u_level1'];
$p_level2 = $_POST['u_level2'];
$p_level3 = $_POST['u_level3'];
$p_level4 = $_POST['u_level4'];
$p_level5 = $_POST['u_level5'];
$p_level6 = $_POST['u_level6'];
$p_level7 = $_POST['u_level7'];
$p_level8 = $_POST['u_level8'];
$p_level9 = $_POST['u_level9'];


echo $p_item;
echo $p_level1;
echo $p_level2;
echo $p_level3;
echo $p_level4;
echo $p_level5;
echo $p_level6;
echo $p_level7;
echo $p_level8;
echo $p_level9;

  
$username="root";
$password="";
$database="cmms";
mysql_connect(localhost,$username,$password);

$query="UPDATE `cmms`.`menu_1` SET `level9` = '$p_level9',
`level8` = '$p_level8',
`level7` = '$p_level7',
`level6` = '$p_level6',
`level5` = '$p_level5',
`level4` = '$p_level4',
`level3` = '$p_level3',
`level2` = '$p_level2',
`level1` = '$p_level1' WHERE item='$p_item';";


mysql_query($query);
echo "Record Updated";
mysql_close();


?>

Thank you

Max

I Solved it by changing the Value of the "else" echo'd checkbox from 0 to 1.

See below code:

if($level1=="1") {
		echo "<td><input type='checkbox' name='u_level1' value='1' checked='checked' /></td>\n";
	} else {
		echo "<td><input type='checkbox' name='u_level1' value='1' /></td>\n";    
	}

Regards

Max

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.