问题描述:

I am just missing few line of code in my project. I have checkboxes and submit button. User clicked in the checkbox and on click he will filter the the options. I am storing checkboxes checked result into database as a serialize array and the retreving it as a unserialize array so i am getting the filter checkbox result properly whenever i refresh the page. The only problem is my checkboxes are not showing checked when i refresh the page. It showing the result properly and stored into database but i am not able to maintain the checkbox event. I think i am missing 1 line of code in html input type.My unserialize array are stored in $config variable.

Here is my code:

html code:

 if($param['aktion'] == 'get-widget-vehicle-edit')

{

$page['register-fahrzeuge'] = array(

0 => array( 'FAHRZEUGE','enabled',$page['script'],''),

1 => array( 'EDIT-FAHRZEUGE','aktiv',$page['script'],'',''),

);

$html = '<table width="538" cellspacing="0" cellpadding="0" border="0">

<tr>

<td>

<div>'.CreateRegister($page['register-fahrzeuge']).'</div>

'.CreateMessage().'

<div class="cont-liste-verlauf register">

<table id="FAHRZEUGE" >

<tr>

<td>';

<table id="FAHRZEUGE" >

<tr>

<td>

<input type="checkbox" name="Anzahl_Fahrzeuge_ohne_Bilder" id="Anzahl_Fahrzeuge_ohne_Bilder" checked />

<label for="Anzahl_Fahrzeuge_ohne_Bilder">Anzahl_Fahrzeuge_ohne_Bilder</label><br>

<input type="checkbox" name="Fahrzeuge_ohne_Preis" id="Fahrzeuge_ohne_Preis" value="Fahrzeuge_ohne_Preis" checked />

<label for="Fahrzeuge_ohne_Preis">Fahrzeuge_ohne_Preis</label><br>

<input type="checkbox" name="Fahrzeuge_mit_Fehlern" id="Fahrzeuge_mit_Fehlern" value="Fahrzeuge_mit_Fehlern" checked />

<label for="Fahrzeuge_mit_Fehlern">Fahrzeuge_mit_Fehlern</label><br>

<input type="checkbox" name="Herausforderungen" id="Herausforderungen" value="Herausforderungen" checked />

<label for="Herausforderungen">Herausforderungen</label><br><br>

</td>

</tr>

</table>

$html .= '</td>

</tr>

</table>

'.CreateButton($page['button']).'

</div>

</td>

</tr>

</table>';

$return = array(

'status' => 1,

'html' => $html

);

echo json_encode($return);

die();

}

php code :

 if($param['aktion'] == 'get-widget-vehicle')

{

$page['register-fahrzeuge'] = array(

1 => array( 'FAHRZEUGE','aktiv',$page['script'],''),

0 => array( 'EDIT-FAHRZEUGE','enabled',$page['script'],'',''),

);

/*when empty default config...

$query= "SELECT dashboard_widget.configuration FROM dashboard_widget WHERE Id =1";

*/

$query ="SELECT dashboard_widget_users.configuration

FROM dashboard_widget_users

INNER JOIN yw_user ON dashboard_widget_users.dsnr_yw_user = yw_user.intern

INNER JOIN dashboard_widget ON dashboard_widget_users.dsnr_dashboard_widget = dashboard_widget.id

WHERE dashboard_widget_users.dsnr_yw_user=".$user_id." AND dashboard_widget.id=1 ";

$result = mysql_query($query, $myConnection);

$html = '<table width="538" cellspacing="0" cellpadding="0" border="0" >

<tr>

<td>

<div>'.CreateRegister($page['register-fahrzeuge']).'</div>

'.CreateMessage().'

<div class="cont-liste-verlauf register">';

if($row = mysql_fetch_assoc($result))

{

$empty_config=$row['configuration'];

if(empty($empty_config )) {

$sql="SELECT dashboard_widget.configuration FROM dashboard_widget WHERE Id =1";

$sql_result = mysql_query($sql, $myConnection);

$results = mysql_fetch_assoc($sql_result);

$config= unserialize($results['configuration']);

}

else{

$config= unserialize($row['configuration']);

}

foreach($config as $val)

{

if($val == 'Anzahl_Fahrzeuge_ohne_Bilder')

{

$sql='SELECT count( * ) as bilder FROM autodo.amo_bilder_live l LEFT JOIN autodo.afzdaten a ON l.dsnr_afzdaten = a.dsnr WHERE l.dsnr_afzdaten IS NULL';

$sql_result=mysql_query($sql);

$results=mysql_fetch_assoc($sql_result);

$configVehicleWidget[$val]['count'] = $results['bilder'];

}

else if($val == 'Fahrzeuge_ohne_Preis')

{

$sql='SELECT count(*) as Preis FROM autodo.afzdaten WHERE Preis IS NULL';

$sql_result=mysql_query($sql);

$results=mysql_fetch_assoc($sql_result);

$configVehicleWidget[$val]['count'] = $results['Preis'];

}

else if($val == 'Fahrzeuge_mit_Fehlern')

{

$sql='SELECT count(*) as fehler FROM autodo.afzdaten WHERE fehler IS NULL';

$sql_result=mysql_query($sql);

$results=mysql_fetch_assoc($sql_result);

$configVehicleWidget[$val]['count'] = $results['fehler'];

}

else if($val == 'Herausforderungen')

{

$sql='SELECT count(*) as herausforderung FROM autodo.afzdaten WHERE herausforderung IS NULL';

$sql_result=mysql_query($sql);

$results=mysql_fetch_assoc($sql_result);

$configVehicleWidget[$val]['count'] = $results['herausforderung'];

}

if($configVehicleWidget[$val]['count'] > 0)

{

$html .= '<a href="'.$configVehicleWidget[$val]['link'].'" style="color:red;">'.'<image src="'.$configVehicleWidget[$val]['image'].'" width="30" height="25" align="middle">.'.$configVehicleWidget[$val]['count'].' '.$configVehicleWidget[$val]['linkName'].'<br>'.'<br>'.'</a>';

}

else

{

$html .='<span style="color:green;">'.'<image src="'.$configVehicleWidget[$val]['image'].'" width="30" height="22" align="middle">.'.$configVehicleWidget[$val]['textAllOK'].'<br>'.'<br>'.'</span>';

}

}

}

$html .= '

</div>

</td>

</tr>

</table>';

$return = array(

'status' => 1,

'html' => $html

);

echo json_encode($return);

die();

}

网友答案:

You need to output "checked" if your checkbox is checked, and not , if it is not checked.

The "checked" value is already retrieved from the database (there is no need to use session in this case).

Here is one way to do it:

<?php

$is_checked = true;

if ($is_checked)
{
    // checked

    ?>
    <input type="checkbox" name="a" checked />
    <?php
}
else
{
    // not checked

    ?>
    <input type="checkbox" name="a" />
    <?php
}


?>

Here is a more compact way to do the same thing:

<?php

$is_checked = true;

// this will output either "checked" or nothing ""
echo '<input type="checkbox" name="a" ' . ($is_checked ? 'checked' : '') . ' />';


?>

Edit: Here is an example of how to retrieve the $is_checked value from a DB.

Create a test table and fill it with sample data, with this SQL:

CREATE TABLE test
(
`user_id` int(11),
`checkbox_name` varchar(256),
`is_checked` int(11)
);


INSERT INTO test (`user_id`, `checkbox_name`, `is_checked`)
VALUES
('1', 'a', '0'),
('1', 'b', '1'),
('1', 'c', '0'),
('2', 'a', '0'),
('2', 'b', '1'),
('2', 'c', '1')
;

Retrieve the data with PHP:

<?php

function is_cb_checked ($user_id, $checkbox_name)
{

    $q = mysql_query ('select * from test where user_id = ' . (int)$user_id . " and checkbox_name = '" . mysql_real_escape_string ($checkbox_name) . "'" );

    $row = mysql_fetch_assoc ($q);

    $is_checked = ($row['is_checked'] > 0 ? true : false);

    return $is_checked;
}


$user_id = 2;


$a_is_checked = is_cb_checked ($user_id, 'a');
$b_is_checked = is_cb_checked ($user_id, 'b');
$c_is_checked = is_cb_checked ($user_id, 'c');


echo 'a = <input type="checkbox" name="a" ' . ($a_is_checked ? 'checked' : '') . ' /><br/>';
echo 'b = <input type="checkbox" name="b" ' . ($b_is_checked ? 'checked' : '') . ' /><br/>';
echo 'c = <input type="checkbox" name="c" ' . ($c_is_checked ? 'checked' : '') . ' /><br/>';
网友答案:

you'll want to change this

<input type="checkbox" name="Fahrzeuge_mit_Fehlern" id="Fahrzeuge_mit_Fehlern" value="Fahrzeuge_mit_Fehlern" checked />

to something like this, not sure what you checkbox values look like when you take them out the DB

<input type="checkbox" name="Fahrzeuge_mit_Fehlern" id="Fahrzeuge_mit_Fehlern" value="Fahrzeuge_mit_Fehlern" <?php echo (isset($config['Fahrzeuge_mit_Fehlern'])) ? 'checked="true"' : ''; ?> />

Basically tell it if your checkbox was checked, apply the checked attribute otherwise don't, I imagine they are always checked right now.

Of course you will need to pull the database stuff out before creating the html output.

网友答案:

If you want to check all the checkboxes on page load, you may use javascript in $( document ).ready() document.getElementById("myCheck").checked = true;

相关阅读:
Top