how to store and retrieve checkbox value in php?

how to store and retrieve checkbox values in mysql database with php

I was working on a project and i had landed in an awkward situation, where i have to save the tick box values in a database and once done, i had to retrieve those values and present it to the user for editing. i searched everywhere on the web on how to store and retrieve check box values and the answers are hard to find. So i have decided to document her on how it worked for me.

For example when there are checkboxes for 5 colors – Red, Green, Blue, Yellow, Brown and i have the php script doing insertion into database.

Storing checkbox values into a database

The logic is simple. when the user checks those color checkboxes, get the POST value array colors[] then using implode function, i store them in database (single field) separated using comma values.


<form name="form1" method="post" action="">
  <p>
    <input name="colors[]" type="checkbox" id="colors[]" value="Red">
    Red 
    <input name="colors[]" type="checkbox" id="colors[]" value="Blue">
    Blue 
    <input name="colors[]" type="checkbox" id="colors[]" value="Green">
    Green</p>
  <p> 
    <input name="colors[]" type="checkbox" id="colors[]" value="Yellow">
    Yellow 
    <input name="colors[]" type="checkbox" id="colors[]" value="Brown">
    Brown </p>
</form>

and the php code to join the selected values is…

//join the post values into comma separated 
$colors = mysql_real_escape_string(implode(',', $_POST['colors']));

and then store this value in the database in a single TEXT field in mysql.

Retrieving from Database and show checkboxes

For example if user selects Red, Blue, Brown checkboxes and we have those values stored in database. Now the user wants to edit those checkbox options in a form and now we have to show all check box colors with Red, Blue and Brown checked.

This is kind of tricky but there is a way to do it.

The logic goes this way..

1. Make an array variable containing all colors.
2. Retrieve those colors stored in database and make them another array using explode function separated by commas.
3. Loop through all colors and check whether each color is stored in database. If so write a SELECTED checkbox else write a unselected checkbox. To accomplish the array match, i will be using in_array() function in php.

The php code is below…

<?
// $row['color] is a database field.

$aColors = array("Red", "Blue", "Green", "Yellow", "Brown");

//converting comma separated into array using explode function
	$dbcolors= explode(',',$row['color']);
			
		
  foreach ($aColors as $color) {
			 
	 if(in_array($color,$dbcolors)) {
	  echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$color\" CHECKED> $facil ";
		  } else
		  {
	  echo "<input name=\"colors[]\" type=\"checkbox\" value=\"$color\"> $color";
				  }
			  }
?>

NOTE: The above code may not work since double quotes with echo must be escaped.

Upon running this code, you will see all check boxes with colors with values stored in database selected.

Hope this helps!

Similar Posts:

Tags:

Prabhu Balakrishnan

I am an entrepreneur and graduate in Computer science engineering, pretty cool chap, blogging since 2005. My interests are Machine learning, Parallel computing, Computer vision, PHP, Wordpress, Linux etc. I like to chill out with travels, video games and a cup of hot coffee. I am born in India, currently living in Budapest, Hungary. Feel free to contact me anytime!

Leave a Reply

Your email address will not be published. Required fields are marked *


one × = 4

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source codes with [code][/code] tags. Powered by
  1. Hamed

    thanks in advanced to answer my question?

    what should i do if the checkbox will be send without any tick and parameter;
    when the array is empety
    how do i keep a way from this message : Undefined index

    Reply
  2. Hamed

    thanks in advanced to answer my question?

    what should i do if the checkbox will be send without any tick and parameter;
    when the array is empty
    how do i keep a way from this message : Undefined index

    Reply
  3. Keith

    Why doesn’t this work?

    I include a database file and make two separate files including it and it yields errors with defining the variables.

    Similar questions have been posed regarding this. Is there a solution?

    Reply
  4. Jeff

    Hi, I have a smiliar code like this, retrieving stored data value from mysql but after displaying it on the page, can I use the results shown to update on the same page?

    Hope you guys understand. =x

    Reply
  5. Hugo

    I have a doubt concerning:

    1. Make an array variable containing all colors.

    I have a table in my database with all the possible color values, how do I get to something like this:

    $aColors = array(“Red”, “Blue”, “Green”, “Yellow”, “Brown”);

    Reply
  6. ehsan

    Prabhu Many thanks for sharing this useful tutorial.

    I am a little puzzled by one variable on your php code line number 13
    What does this variable $facil do over here on line 13?

    Reply
  7. v k

    i want to get database table fields with checkbox and if i select any check box either 1 or more files trough check boxes it will get the data what i have in the checked fields in database … using php … i’m using xampp..
    can you help me plz

    thank you

    Reply
1 2