Jump to content

checkbox help - need a simple example


faneotoss

Recommended Posts

hi all!

 

i have searched several forums including here but couldn't find a simple example about checkbox handling in php & mysql.

I managed to do something but still doesn't work.

 

I just wanted a form that only contains several checkboxes and users will choose some of them, click and submit. I managed to write these in a single column. These will be thier preferences so when ever they want to edit form should retrieve the checkbox's checked status from the DB.

 

Table products:

 

company_id
1
company_name
ACME
ch1
101,102

 

<?php
if (!$logged[company])
{
	echo ("<a href=\"login.php\">login</a>");
}
else
{
	if (!$_POST[update])
	{

	$select = mysql_query("SELECT ch1 FROM products WHERE company_usr='$logged[company_usr]'");
	$row = mysql_fetch_array($select);

	echo ("
		<form method=\"POST\">
		<input type=\"checkbox\" name=\"ch1[]\" value=\"101\"	/><br>
		<input type=\"checkbox\" name=\"ch1[]\" value=\"220\" /><br>	
		<input type=\"submit\" size=\"20\" name=\"update\" value=\"Update\">

		</form>"); 
} 
else 
{
$ch1 = $_POST[ch1];	
$update = mysql_query("UPDATE products SET ch1='" . mysql_escape_string(implode(", ",$ch1)) ."' WHERE company_usr='$logged[company_usr]'" );
} 
}

?>

 

Below code inserts into column ch1 the checkbox values.

 

What i want is how to retrieve these values and mark checkboxes as checked if its checked.

I will be glad if someone helps. Or even you can post a simple checkbox example that submit values into db and retrieve values according to.

 

thnx

Link to comment
https://forums.phpfreaks.com/topic/37925-checkbox-help-need-a-simple-example/
Share on other sites

Here's the code...

 

<form method="post" action="path to script">

<input type="checkbox" id="colors[]" value="red" /> Red

<input type="checkbox" id="colors[]" value="blue" /> Blue

<input type="checkbox" id="colors[]" value="green" /> Green

<input type="checkbox" id="colors[]" value="yellow" /> Yellow

</form>

 

Notice the [ and ] after "color." That will alert your PHP script that this data will be stored in an array. You won't have an array without the brackets.

 

Now, let's say someone fills out the form. Maybe she is ordering shirts and wants one in every color. She checks every box on the form.

 

You want to store the information in that array to your database. So in your PHP script, you try something like this:

 

$colors=$_POST['colors']; //takes the data from a post operation...

$query=INSERT INTO colors VALUES('$colors');

 

But that doesn't work. You'll get "Array" as your value in the database.

 

Instead you'll need to use PHP's serialize() function.

 

As the PHP documentation explains, the serialize function "[g]enerates a storable representation of a value."

 

In other words, it takes arrays (and other data types), and converts the contents into data that can be stored.

 

Let's re-do the above code using serialize().

 

$colors=serialize($_POST['colors']); //takes the data from a post operation...

$query=INSERT INTO colors VALUES('$colors');

 

Now, let's say you want to retrieve the array data from the database. But if you look at your database, you'll see something like this:

a:3:{i:0;s:8:"red";i:1;s:9:"blue";i:2;s:6:"green";i:3;s:4:"yellow";}

 

Funky, ain't it? But that's not a problem: unserialize() to the rescue.

 

As its name implies, unserialize() takes serialized array data and converts it back to a usable array.

 

To retrieve the array data from the database, then, you might do this:

 

$query=SELECT * FROM shirtColors;

$doQuery=mysql_query($query);

$numrows=mysql_num_rows($doQuery);

if($numrows>0)

{

while($colors=mysql_fetch_array($doQuery))

  {

  $colors=unserialize($colors['colors']);

 

  foreach($colors as $shirt)

  {

  print $shirt.', ';

  }

  }

}

else

{

print 'No colors in database.';

}

 

You should get "red, blue, green, yellow."

 

And there you have it.

I'm still a beginner myself, so this may not be the best solution, and someone please correct me if I'm wrong or if there's a better way.  My first thought is to retrieve ch1 from the database, explode it into an array ...

 

Then you would have to take your input tags and put a for statement and an if statement:

 

$array = explode(",", $result);
echo "<input type=\"checkbox\" name=\"ch1[]\" value=\"101\"";
for($i = 0; $i < count($array); $i++) {
  if($array[$i] == 101)
    echo "checked=\"checked\"";
}
echo " /><br />\n";

 

Kinda messy, but it would work ... I think.

 

The post above me is confusing to me, because currently, your script is successfully entering the values into your database, separated by commas, correct?  If that's the case, I think my script would work.  If you're having issues with entering them into the database from the array, you'll have to combine mine and the post above.

thanks for your reply.

Actually it didn't work. Is my query wrong?

 

here what i do

 


<?php
if (!$logged[company])
{
	echo ("<a href=\"login.php\">login</a>");
}
else
{
	if (!$_POST[update])
	{

	$select = mysql_query("SELECT ch1 FROM products WHERE company_usr='$logged[company_usr]'");
	$row = mysql_fetch_array($select);

	echo ("
		<form method=\"POST\">
		<input type=\"checkbox\" name=\"ch1[]\" value=\"101\"	/><br>
		<input type=\"checkbox\" name=\"ch1[]\" value=\"220\" /><br>	
		<input type=\"submit\" size=\"20\" name=\"update\" value=\"Update\">

		</form>"); 
} 
else 
{
$ch1 = $_POST[ch1];	
$update = mysql_query("UPDATE products SET ch1='" . mysql_escape_string(implode(", ",$ch1)) ."' WHERE company_usr='$logged[company_usr]'" );

             $result = mysql_query("SELECT ch1 FROM products WHERE company_usr='$logged[company_usr]'");

             $array = explode(",", $result);
             echo "<input type=\"checkbox\" name=\"ch1[]\" value=\"101\"";
             for($i = 0; $i < count($array); $i++) {
                 if($array[$i] == 101)
                   echo "checked=\"checked\"";
                   }
                  echo " /><br />\n";
} 
}

?>

 

I think i have to write mysql_fetch_array to somewhere or am i ?

I can successfully enter values of chkboxes, update them vice versa but still can't return them as checked for editing.

 

anyone could help  ???

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.