im new to php . plz help. i have a page where people fill in a form and check on checkboxes. i also have a page where i edit. i want to know if its possible that on my edit page i could see the checked box checked ? example if "checkbox male" was checked and i want to edit it. on my edit page , can it appear checked already? how if possible?

Some code hints would be nice: like, where do you get the data from, in what form? Considering that the users can only be male or female, you need radio buttons (only use checkboxes for design reasons). Theoretically you can do something like this:

echo '<input type="radio" name="sex"'.($sex=='male') ? ' checked' : ''.' />';
echo '<input type="radio" name="sex"'.($sex=='female') ? ' checked' : ''.' />';

here is my code. if you check on male then my input in database will be "1" else "0" same as if you check on female. what i want to do is . if i open my edit page then both my checkboxes are visible, but i want if example you checked previously checked male then the check box "male " on my edit page should appear checked already.

<?php


            $gmale = 1;
        } else {
            $gmale = 0;
        }

        if ($_REQUEST['female']!='') {
            $fmale= 1;
        } else {
            $fmale = 0;
        }
   ?>

   <form>
        <input type="checkbox" name="male" id="male">Male<br>
    <input type="checkbox" name="fmale" id="fmale">Female
                </form>

You could use a code that looks like this:

<?php
echo "<form>";

// not sure what gmale is your supposed to be or if it is supposed to be an if statment.
            $gmale = 1;
        } else {
            $gmale = 0;
        }
        if ($_REQUEST['fmale']!='') {
           echo "<input type='checkbox' name='fmale' id='fmale' checked>Female";

        }else{
            echo "<input type='checkbox' name='fmale' id='fmale'>Female";
        }
        echo "</form>";
   ?>

I assume your $_REQUEST was supposed to be fmale instead of female because the name of the input was fmale rather than female. Female would return nothing. if $gmale is supposed to be male then you could just make it look like the female statement which would require little work on your part.

Member Avatar for diafol
<?php $chk = (isset($sex) && $sex == 1) ? array('','checked ') : array('checked ', ''); ?>

<input type='radio' value='0' name='sex' id='female' <?=$chk[0]?>/>Female
<input type='radio' value='1' name='sex' id='male' <?=$chk[1]?>/>Male

$sex holds the value of gender retrieved from DB.

Your example seems to be using checkboxes - this isn't right for an either/or selection. You have the choice of using a select field or radiobuttons.

thanx alot guys . It worked. Will this work the same with dropdownlists?

for example if my list consists of "blue","green" and "yellow" . and i previously selected green. and now im on my edit page, will green be automatically selected?

heres my code

<?php
                    $sql = "Select * from Colors";
                    $rs = mysqli_query($conn,$sql);
                 ?>
                <select name="color" id="color">
<option  value=""<?php echo $color; ?>"" disabled selected style="color:grey;">Choose color</option>
                    <?php 

                        while ($row = mysqli_fetch_assoc($rs)) {
                            echo "<option value='" . $row['Color_id'] . "'>" . $row['Color_name'] . "</option>"; 
                        }
                    ?>

i have a table named "Colors". this table has 2 coloumns "color_id" and "color_name". each color_id has a unique color_name. at the moment all it outputs is all the options. what i want to output in my edit page is the previously selected color . please help

Member Avatar for diafol

Here's a quite and dirty function just knocked up with a few examples. Should work for select or radio. To get ti to work for select, change 'checked ' to 'selected ':

function getCheckedArray($options, $chosen, $compareValues=true)
{
    $fills = array_fill(0,count($options),'');

    if(!$compareValues)
    {
        $tmp = array_search($chosen, array_values(array_flip($options)));
    }else{
        $tmp = array_search($chosen, $options);
    }
    if(!is_null($tmp) && $tmp !== false){
        $fills[$tmp] = 'checked ';
    }   

    return $fills;
}

//EXAMPLE FOR COMPARING VALUES (third param = true [default])

$options = array('green','red','blue'); //can be taken from DB
$chosen = 'red'; //can be taken from DB 
$chk = getCheckedArray($options,$chosen);

for($x=0;$x<count($options);$x++)
{
    echo "<input type='radio' name='color' value='{$options[$x]}' {$chk[$x]}/> {$options[$x]}<br />";
}

//EXAMPLE FOR COMPARING KEYS #1 (third param = false)

$options = array(3=>'green',5=>'red',6=>'blue');
$chosen = 6;
$chk = getCheckedArray($options,$chosen,false);
$x=0;
foreach($options as $key=>$label)
{
    echo "<input type='radio' name='color1' value='$key' {$chk[$x]}/> {$label}<br />";
    $x++;
}

//EXAMPLE FOR COMPARING KEYS #2 (third param = false)

$options = array('green'=>'Verde','red'=>'Rosso','blue'=>'Blu');
$chosen = 'red';
$chk = getCheckedArray($options,$chosen,false);
$x=0;
foreach($options as $key=>$label)
{
    echo "<input type='radio' name='color2' value='$key' {$chk[$x]}/> {$label}<br />";
    $x++;
}

BTW I do not advise echoing on every iteration of the loop - it's best to build up a string (concatenation) and then spit out the one string at the end.

Member Avatar for diafol

IN fact, you could do something like this...

function getOptions($chosenText, $options, $chosen, $compareValues=true)
{
    $fills = array_fill(0,count($options),'');

    if(!$compareValues)
    {
        $tmp = array_search($chosen, array_values(array_flip($options)));
    }else{
        $tmp = array_search($chosen, $options);
    }
    if(!is_null($tmp) && $tmp !== false){
        $fills[$tmp] = $chosenText;
    }   

    return $fills;
}

function getSelectedArray($options, $chosen, $compareValues=true)
{
    return getOptions('selected ', $options, $chosen, $compareValues);
}

function getCheckedArray($options, $chosen, $compareValues=true)
{
    return getOptions('checked ', $options, $chosen, $compareValues);
}

You never call getOptions() from your own code, just getCheckedArray() or getSelectedArray()

Examples with select box:

//EXAMPLE FOR COMPARING KEYS SELECT BOX

$options = array('green'=>'Verde','red'=>'Rosso','blue'=>'Blu');
$chosen = 'red';
$chk = getSelectedArray($options,$chosen,false);
$x=0;
echo "<select id='colors10'>";
foreach($options as $key=>$label)
{
    echo "<option value='$key' {$chk[$x]}>{$label}</option>";
    $x++;
}
echo "</select>";

//EXAMPLE FOR COMPARING KEYS SELECT BOX

$options = array(3=>'green',5=>'red',6=>'blue');
$chosen = 6;
$chk = getSelectedArray($options,$chosen,false);
$x=0;
echo "<select id='colors11'>";
foreach($options as $key=>$label)
{
    echo "<option value='$key' {$chk[$x]}>{$label}</option>";
    $x++;
}
echo "</select>";
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.