Hi all, I hav a code that takes my table rows and displays as checkboxes in a registration form, When the form submits user checked values will be saved separated by comma's(,) in one column . In future the user need to edit his details means how to show that user checked check boxes as checked="true" , The editing form also same as registration from .
Any help much appreciated,,

If you have a multiple-steps form, you could consider temporarily saving form information in a session. If you don't know which information your post data contains, you can print the post data on your screen as follows:

<?php
print_r($_POST);
?>

This will print all the data the user has submitted through a post on your screen. It will also contain the values of your checkboxes, in case you need to check what those values are.

Hope that's what you meant? :)

Hi minitauros, Sry for my english, My need is, When we edit some form means first we select the data and display that to text boxes and edit process will be done, Now how to represent that by using check boxes, i am trying to figure out the check boxes as checked if the user checked that in the registration form

Hm I'm still not sure what you mean, but if you for example have this checkbox

<input type="checkbox" name="imacheckbox" value="hello"/>

then $_POST will have the value "hello". Is that then what you mean?

To create a radio button list (which allows only one option to be selected at a time, like in a <select>), you can create a list of radio buttons.

Instead of adding <option>s to the <select> you should just add <input type="radio">'s to your page.

<input type="radio" name="city" value="city1"/>
<input type="radio" name="city" value="city2"/>
<input type="radio" name="city" value="city3"/>

Now when the form is submitted, $_POST will have the value of the radio input that was selected. In case you're using checkboxes, which allows multiple items to be selected, the code process should be the same. For example:

<input type="checkbox" name="city[]" value="city1"/>
<input type="checkbox" name="city[]" value="city2"/>
<input type="checkbox" name="city[]" value="city3"/>

When the user submits this form, there will be an array called $_POST in PHP. If for example boxes 1 and 2 were checked, it will contain $_POST[0] = city1, and $_POST[1] = city2. Was this then what you meant? :)

Yes thats the thing, now its all saved in a table in one column by separator comma(,), Now how to retrieve it as checked

You could explode the comma separated string.

$array = explode(',', $comma_separated_string);

This would return an array. Then you could check for each checkbox if it's value is in the array, and if it is, check it? E.g.:

while(...)
{
 if(in_array($value, $array)
 {
  // Make checkbox checked.
 }
}

Could u pls expand this ,
while(...)
{
if(in_array($value, $array)
{
// Make checkbox checked.
}
}
Its helping me,Now i got clear idea..

$query = 'SELECT city_name, other_stuff FROM table';
$result = mysql_query($query);

while($fetch = mysql_fetch_assoc($result)
{
  $city_name = $fetch['city_name'];

  if(in_array($city_name, $array)
  {
    echo '<input type="checkbox" name="city[]" value="' . $city_name . '" checked="checked"/>';
  }
  else
  {
    echo '<input type="checkbox" name="city[]" value="' . $city_name . '"/>';
  }
}

This would print a checkbox for each city that is retrieved from the database, but only prints those with a value that occurs in $array as checked.

Yeah its working , but its checking only one check box, pls visit the same link, i updated it..

I think it should be working. Did you check if your $array really does contain all the values of the checkboxes you want to be checked?

Member Avatar for diafol

Could I suggest that you take a different route to this?
The comma separated list is a bit clunky.
Going bitwise may be useful:

<input type="checkbox" name="choice[]" value="1" id="choice1" /> <label for="choice1">Choice 1</label>
<input type="checkbox" name="choice[]" value="2" id="choice2" /> <label for="choice2">Choice 2</label>
<input type="checkbox" name="choice[]" value="4" id="choice3" /> <label for="choice3">Choice 3</label>
<input type="checkbox" name="choice[]" value="8" id="choice4" /> <label for="choice4">Choice 4</label>
<input type="checkbox" name="choice[]" value="16" id="choice5" /> <label for="choice5">Choice 5</label>
(etc)

You then get the total number like this:

$choiceArray = (array) $_POST['choice'];
$choiceTotal = array_sum($choiceArray);

//put this into the DB as a single value
//e.g. choiceTotal = 14 means choice2,3,4, choiceTotal = 25 means choice 1,4,5

So for complete code:

//GET data from DB (choices field) - into $row['choices']...

$choiceTotal = $row['choices'];

$checkboxes = array(
               array('label'=>'Choice 1','id'='choice1','value'='1'),
               array('label'=>'Choice 2','id'='choice2','value'='2'),
               array('label'=>'Choice 2','id'='choice2','value'='4'),
               (etc)...
              );
$output="";
foreach($checkboxes as $chk){
  $sel = ($choiceTotal & $chk['value']) ? ' checked="checked"' : ''; 
  $output .= "\n<input type="checkbox" name="choice[]" value="{$chk['value']}" id="{$chk['id']}"$sel /> <label for="{$chk['id']}">{$chk['label']}</label>"
}

...
echo $output;

Thank u minitauros, Its worked for me.. Lot of thanks

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.