Reputation: 31
I have written a form that allows for checkbox inputs in my SQL tables I have made the checkboxes as Variables.
What i want is when i display the forms that it should show the checkbox with the value as inputted by the initial input yes or no again.
<input type="checkbox" name="basic_inter" id="basic_inter" value="<? echo
$rows['basic_inter']; ?>">
If I use it this way it doesn't show the value of the checkbox. What is best way to display the checkbox is it due to the tables being Var or is it the method I echo it back to the screen?
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE company_name='$query'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<form name="form" method="post" action="control_adminupdateresellered.php">
<input type="hidden" name="company_name" value="<?=$query?>" />
Then this is some of the echo code I use others not fully included as they just are text
<input type="checkbox" name="basic_inter" id="basic_inter" value="<? echo
$rows['basic_inter']; ?>">
</strong></div> </td>
<td width="81"><div align="right"><strong>LBS Add-on:<br>
R500-00 P/M
</strong></div></td>
<td width="66"><div align="left"><strong>
<input type="checkbox" name="lbs_inter" id="lbs_inter" value="<? echo
$rows['lbs_inter']; ?>">
Upvotes: 0
Views: 2646
Reputation: 255
I would just put in mysql default_value to "No" and when inserting the value would change to "Yes" and on ouput i would write like this -> <input type="checkbox" name="lbs_inter" id="lbs_inter" value="<? echo $rows['lbs_inter']; ?>" <?= (isset($rows['lbs_inter']) && $rows['lbs_inter']=='yes'? 'checked="checked"' : '' )?> />
Upvotes: 0
Reputation: 2129
Please try this
<input type="checkbox" name="lbs_inter" id="lbs_inter" value="<? echo
$rows['lbs_inter']; ?>" <?php if($rows['lbs_inter']=='yes'){ echo "checked";}?>>
Upvotes: 4
Reputation: 863
http://www.w3schools.com/tags/att_input_checked.asp Make a if statement to echo checked if basic_inter is 1?
Upvotes: 1