Page 1 of 1

checkbox problem (checked/unchecked)

Posted: Wed Feb 03, 2010 3:07 pm
by Tassadduq
hi i have a web form for user to sell their used cars online. in the form a section contains the "Features" of the car. which i collect through check boxes like the give code below

Code: Select all

                           <table border="0" cellpadding="0" cellspacing="0" width="450">
  <tbody><tr>
    <td width="150">
    <input name="Features[]" id="AirBags" value="Air Bags" type="checkbox"> Air Bags</td> 
    <td width="150"><input name="Features[]" id="AirConditioning" value="Air Conditioning" type="checkbox"> Air Conditioning</td>
    <td width="150"><input name="Features[]" id="PowerWindows" value="Power Windows" type="checkbox"> Power Windows</td>
  </tr>
  <tr>
    <td width="150"><input name="Features[]" id="PowerSteering" value="Power Steering" type="checkbox"> Power Steering</td> 
    <td width="150"><input name="Features[]" id="PowerLocks" value="Power Locks" type="checkbox"> Power Locks</td>
    <td width="150"><input name="Features[]" id="PowerMirror" value="Power Mirror" type="checkbox"> Power Mirror</td> 
  </tr>
  <tr>
   <td width="150"><input name="Features[]" id="KeyLessEntry" value="Keyless Entry" type="checkbox"> Keyless Entry</td> 
    <td width="150"><input name="Features[]" id="CruiseControl" value="Cruise Control" type="checkbox"> Cruise Control</td>
    <td width="150"><input name="Features[]" id="NavigationSystem" value="Navigation System" type="checkbox"> Navigation System</td>
  </tr> 
</tbody></table>
user will check those check boxes which features his car have.
i save all these features in a single field separated with (,). example (Air Bags, Power Windows, Cruise Control, Power Mirror)
the features save query is given below

Code: Select all

    $features = $_REQUEST['Features'];
    $xCount = count($features);
    $catStr = "";
    for($i = 0; $i < $xCount; $i++){
        $catStr = $catStr.$features[$i].", ";
    }
    $catLen = strlen($catStr);
    $catStr = str_split($catStr, $catLen-2);
    $catStr = $catStr[0];
all these works fine. now the problem is that i used this form also for edit/update entry.
mean if user want to edit his existing entry , he will again use this form.
i passed a query to check if the url contains an id with form it will display the existing values otherwise it will displays blank value.
to get the record of the existing entry i used this command

Code: Select all

"SELECT * FROM usedcars WHERE carid = 6"

this query collects records from database fine.
i want that it check in the "features" field for values,
if it got some values like (Air Bags, Power Windows, Cruise Control, Power Mirror).
the check boxes of these values should be checked.
mean the check box of Air Bags should be checked,
mean the check box of Power Windows should be checked ...so on
:cry: is there anyway to control it?
Thanks in advance for help

Re: checkbox problem (checked/unchecked)

Posted: Wed Feb 03, 2010 3:35 pm
by AbraCadaver
Well, you need to get a row of data, explode the features into an array and then when you display your checkbox, check in the array. Something along these lines:

Code: Select all

$features = explode(',', $row['features']);
//..........
 
$airbags = '';
if(in_array('Air Bags', $features)) {
    $airbags = ' checked';
}
//..........etc..........
 
//..........
echo '<input name="Features[]" id="AirBags" value="Air Bags" type="checkbox"' . $airbags . '> Air Bags</td>';

Re: checkbox problem (checked/unchecked)

Posted: Wed Feb 03, 2010 4:20 pm
by Tassadduq
:) :) :) it works :o
Bundle of Thanks :D