0

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']; ?>">
2
  • Could you show an example of how you're pulling the data from the database? Commented Mar 25, 2013 at 9:10
  • Basically on my input form my staff click the checkboxes, on the display screen I just want the checkbox to display what they clicked. So it should just show the box with a yes or no in it. No action is required from the checkbox at all other than displayed clicked or not Commented Mar 25, 2013 at 9:21

3 Answers 3

4

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";}?>>
Sign up to request clarification or add additional context in comments.

1 Comment

Both helped a lot Praveen just made life easier for me thanks guys
1

http://www.w3schools.com/tags/att_input_checked.asp Make a if statement to echo checked if basic_inter is 1?

Comments

0

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"' : '' )?> />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.