如何检索存储在数据库中的Checkbox检查事件

时间:2014-07-08 13:33:08

标签: php arrays checkbox state

我在项目中缺少几行代码。我有复选框并提交按钮。用户点击复选框,点击后他会过滤选项。我将复选框检查结果存储到数据库中作为序列化数组并将其作为反序列化数组进行检索,因此每当刷新页面时,我都会正确获取过滤器复选框结果。唯一的问题是当我刷新页面时,我的复选框没有显示。它正确显示结果并存储到数据库中,但我无法维护checkbox事件。我想我在html输入类型中缺少一行代码。我的unserialize数组存储在$ config变量中。 这是我的代码: HTML代码:

    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代码:

    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();
}

3 个答案:

答案 0 :(得分:1)

你想改变这个

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

这样的事情,不确定当你把它们拿出数据库时你的复选框值是什么样的

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

基本上告诉它,如果你的复选框被选中,请应用checked属性,否则不要,我想他们现在总是被检查。

当然,在创建html输出之前,您需要拉出数据库内容。

答案 1 :(得分:0)

如果要检查页面加载时的所有复选框, 你可以使用javascript $(document).ready()     document.getElementById(&#34; myCheck&#34;)。checked = true;

答案 2 :(得分:0)

如果选中了复选框,则需要输出“已选中”,如果未选中则不需要输出。

已从数据库中检索“已检查”值(在这种情况下无需使用会话)。

这是一种方法:

<?php

$is_checked = true;

if ($is_checked)
{
    // checked

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

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


?>

这是一种更紧凑的方式来做同样的事情:

<?php

$is_checked = true;

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


?>

编辑:以下是如何从数据库中检索$ is_checked值的示例。

创建一个测试表并使用此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')
;

使用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/>';