在php中使用复选框来存储值

时间:2012-11-23 14:30:54

标签: php html

我想根据顺序选中的复选框存储值。但我有一个错误,如未定义的索引:productadd等等。

设计部分:

<table  width="600" style=" border-color:#799647; border-bottom-color:#799647; border-left-color:#799647; border-right-color:#799647; border-top-color:#799647;" border="2" >
    <tr style="border-color:#799647; background-color:#CAF9C5;">

        <td style="border-color:#799647;"><h4  style="color:Green;">Page Name</h4></td>
        <td style="border-color:#799647;"><h4  style="color:Green;">Add</h4></td>
        <td style="border-color:#799647;"><h4  style="color:Green;">Edit</h4></td>
        <td style="border-color:#799647;"><h4  style="color:Green;">View</h4></td>
        <td style="border-color:#799647;"><h4  style="color:Green;">Delete</h4></td>

        </tr>
        <tr style="border-color:#799647; background-color:#F5F2BF;">
        <td align="center"><label>Products</label><input type="checkbox" name="screen[]" value="Products Master" align="absmiddle" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="Productsadd" name="Productsadd" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="Productsedit" name="Productsedit" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="Productsview" name="Productsview" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="Productsdelete" name="Productsdelete" value="Yes" /></td> </tr>

        <tr style="border-color:#799647; background-color:#F5F2BF;">
        <td><label>SerialNO</label><input type="checkbox" name="screen[]" value="SerialNO" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="SerialNOadd" name="SerialNOadd" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="SerialNOedit" name="SerialNOedit" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="SerialNOview" name="SerialNOview" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" id="SerialNOdelete" name="SerialNOdelete" value="Yes" /></td> </tr>

        <tr style="border-color:#799647; background-color:#F5F2BF;">
        <td><label>Vechile Make</label><input type="checkbox" name="screen[]" value="Vechile Make" /></td>
        <td style="border-color:#799647;"><input type="checkbox" name="Vechileadd" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" name="Vechileedit" value="Yes" /></td>
         <td style="border-color:#799647;"><input type="checkbox" name="Vechileview" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" name="Vechiledelete" value="Yes" /></td> </tr>

        <tr style="border-color:#799647; background-color:#F5F2BF;">
        <td><label>Retailer</label><input type="checkbox" name="screen[]" value="Retailer" /></td>
        <td style="border-color:#799647;"><input type="checkbox" name="Retaileradd" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" name="Retaileredit" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" name="Retailerview" value="Yes" /></td>
        <td style="border-color:#799647;"><input type="checkbox" name="Retailerdelete" value="Yes" /></td> </tr>
 </table>

当我习惯点击保存按钮后,代码将被执行。但我在开关案例功能中有错误。 Php代码:

if(isset($_POST['Save']))
{
    $post['userid'] =($_POST['userid']);

    if(!empty($_POST['userid']))
     {  
    // if(is_array($_POST['Products'])) 
    // {
        foreach($_POST['screen'] as $post['Pagename'])
        {
        //  myfun($post['Pagename']);
switch($post['Pagename'])
    {
        case "Products Master": 
                                $post['addition'] =($_POST['Productsadd']);
                                $post['edit'] =($_POST['Productsedit']);
                                $post['vieww'] =($_POST['Productsview']);
                                $post['del'] =($_POST['Productsdelete']);
                                break;  

        case "SerialNO": 
                                $post['addition'] =($_POST['SerialNOadd']);
                                $post['edit'] =($_POST['SerialNOedit']);
                                $post['vieww'] =($_POST['SerialNOview']);
                                $post['del'] =($_POST['SerialNOdelete']);
                                break;  
        case "Vechile Make": 
                                $post['addition'] =($_POST['Vechileadd']);
                                $post['edit'] =($_POST['Vechileedit']);
                                $post['vieww'] =($_POST['Vechileview']);
                                $post['del'] =($_POST['Vechiledelete']);
                                break;  
        case "Retailer": 
                                $post['addition'] =($_POST['RetailerOadd']);
                                $post['edit'] =($_POST['Retaileredit']);
                                $post['vieww'] =($_POST['Retailerview']);
                                $post['del'] =($_POST['Retailerdelete']);
                                break;  

        default : 
                            ?>
                            <script type="text/javascript">
                            alert("Nothing selected.!!");document.location='userrights.php';
                            </script>
                                <?  

}
                    //echo $post['Pagename'];
        $news->addNews($post);
    //   }  
     }

        ?>
            <script type="text/javascript">
            alert("Created Sucessfully..!!");document.location='userrights.php';
            </script>
            <?
}
}
  ![this is my visual screen][1]

1 个答案:

答案 0 :(得分:0)

这将通过在访问之前检查索引是否已设置来修复它。

但是,我建议您考虑使用数组代替复选框将它们分组到集合中以便于解析:

if(isset($_POST['Save']))
{
    $post['userid'] = isset($_POST['userid']) ? $_POST['userid'] : null;

    if(!empty($_POST['userid']))
     {  
    // if(is_array($_POST['Products'])) 
    // {
        foreach($_POST['screen'] as $post['Pagename'])
        {
        //  myfun($post['Pagename']);
switch($post['Pagename'])
    {
        case "Products Master": 
                                $post['addition'] = isset($_POST['Productsadd']) ? $_POST['Productsadd'] : null;
                                $post['edit'] = isset($_POST['Productsedit']) ? $_POST['Productsedit'] : null;
                                $post['vieww'] = isset($_POST['Productsview']) ? $_POST['Productsview'] : null;
                                $post['del'] = isset($_POST['Productsdelete']) ? $_POST['Productsdelete'] : null;
                                break;  

        case "SerialNO": 
                                $post['addition'] = isset($_POST['SerialNOadd']) ? $_POST['SerialNOadd'] : null;
                                $post['edit'] = isset($_POST['SerialNOedit']) ? $_POST['SerialNOedit'] : null;
                                $post['vieww'] = isset($_POST['SerialNOview']) ? $_POST['SerialNOview'] : null;
                                $post['del'] = isset($_POST['SerialNOdelete']) ? $_POST['SerialNOdelete'] : null;
                                break;  
        case "Vechile Make": 
                                $post['addition'] = isset($_POST['Vechileadd']) ? $_POST['Vechileadd'] : null;
                                $post['edit'] = isset($_POST['Vechileedit']) ? $_POST['Vechileedit'] : null;
                                $post['vieww'] = isset($_POST['Vechileview']) ? $_POST['Vechileview'] : null;
                                $post['del'] = isset($_POST['Vechiledelete']) ? $_POST['Vechiledelete'] : null;
                                break;  
        case "Retailer": 
                                $post['addition'] = isset($_POST['RetailerOadd']) ? $_POST['RetailerOadd'] : null;
                                $post['edit'] = isset($_POST['Retaileredit']) ? $_POST['Retaileredit'] : null;
                                $post['vieww'] = isset($_POST['Retailerview']) ? $_POST['Retailerview'] : null;
                                $post['del'] = isset($_POST['Retailerdelete']) ? $_POST['Retailerdelete'] : null;
                                break;  

        default : 
                            ?>
                            <script type="text/javascript">
                            alert("Nothing selected.!!");document.location='userrights.php';
                            </script>
                                <?  

}
                    //echo $post['Pagename'];
        $news->addNews($post);
    //   }  
     }

        ?>
            <script type="text/javascript">
            alert("Created Sucessfully..!!");document.location='userrights.php';
            </script>
            <?
}
}
  ![this is my visual screen][1]