Hi all,
I am very new to php and am slowly trying to get something together.

I have a MySQL database with a table that has (among others) a field which is TINYINT called "field1". I have managed to be able to store a 1 or 0 in it from a checkbox.

I now need to be able to display that choice as a checkbox after it has been stored in the database.

The code I have:

<?php
/* some php code */
/* Name */
echo "<p><b>Name: ".$req_user_info['name']."</b><br />";
/* Username */
echo "<p><b>Username: ".$req_user_info['username']."</b><br />";
/* Email */
echo "<b>Email:</b> ".$req_user_info['email']."</p>";
/* field1 */
echo "<b>field1:</b> ".$req_user_info['field1']."</p>";
/* some more php code */
?>

Displays this:

Name: Steven King
Username: stevenk
Email: stevenking@kingmail.com
field1: 1
(fictitious details)

This shows me that the value is being retrieved correctly, but instead of text of numeral 1 or 0, I want it to display a checkbox that is either checked(1) or not checked(0).

I have found so much help on how to get checkbox values into a MySQL database, but none on how to display retrieved values as a checkbox.

Any help that anyone could offer would be greatly appreciated.

Cheers Al

I have tried"

if($req_user_info['field1'] = 1){
	echo "<b>eNewsletter: </b><input type=\"checkbox\" name=\"field1\" value=".$req_user_info['field1']." CHECKED><br/>";
}
else{
	echo "<b>field1: </b><br/>";
}

But this displays a checked checkbox whether the value of field1 is 1 or 0

It seems that if I keep trying then eventually I get there:

if($req_user_info['field1'] == 1){
	echo "<b>field1: </b><input type=\"checkbox\" name=\"field1\" value=".$req_user_info['field1']." CHECKED><br/>";
}
else{
	echo "<b>field1: </b><input type=\"checkbox\" name=\"field1\" value=".$req_user_info['field1']." UNCHECKED><br/>";
}

I am no so much closer to my goal

<?php 
session_start();
$req_user_info['field1'] = 1; //switch to zero to see the change

if($req_user_info['field1'] == 1)
{
echo '<strong>field1: </strong><input type="checkbox" name="field1" value="'.$req_user_info['field1'].'" checked="checked" /><br/>';
}
else{
echo '<strong>field1: </strong><input type="checkbox" name="field1" value="'.$req_user_info['field1'].'" /><br/>';}

?>

always echo with single quotes, you can then use doubles for other things plus php reads different if single or double quotes are used, even the ticks on top left of keyboard ` .

look carefully at this

="'.$req_user_info.'"

equals double single dot (your-string) dot single double


Hope this helps a little, if you save it to a nameit.php file and upload it in ascii it should work

how to write the code for displaying the checkbox and if any value in the checkbox is clicked ,then the details regarding the selected option is displayed

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.