Home >Backend Development >PHP Problem >How to modify checkbox in php

How to modify checkbox in php

藏色散人
藏色散人Original
2020-11-17 09:23:232050browse

How to modify checkbox in php: First create a PHP sample file; then modify the checkbox through the explode and in_array functions in PHP.

How to modify checkbox in php

Recommended: "PHP Video Tutorial"

Modify the checkbox value in the html form in PHP

In the construction of corporate websites, we often encounter customized requirements from some customers, so some existing programs cannot meet them and must be developed by ourselves. OK. A few days ago, a customer made LED flexible light strips and requested that the backend of the website be able to select multiple product categories. That means multiple selections can be made for the first-level and multi-level product categories when publishing. Multiple selection for product release is easy, but from the database It is not so easy to read out the data and display the selected ones. Maybe I am too stupid and it took me a day or two to complete it.

To summarize, the main ones used are the explode() and in_array() functions in PHP. explode is used to split multiple selection IDs before publishing and generate new arrays. When modifying, just use in_array The () function compares whether the corresponding value in the existing checkedbox form exists in the new array after segmentation. If it exists, checked will be displayed. If it does not exist, checked will not be displayed.

There is not much code, but it is very practical. I hope it can be given to other friends who need it, so I don’t want you to watch it. The following is a small piece of code that I intercepted. I believe it is not difficult to understand.

The following code part:

  <?php
            /**
            *
            *代码名称:checked修改
            *代码作者:刘康永
            *修改日期:2012-12-3
            *修改原因:LED软灯条产品多项选择分类修改
            *完成进度:已经完成
            *作用简介:用于html中对checked多项表单修改时使用,主要运用到php中的函数如explode和in_array这两个参数
            *
            */
            $sqlone="SELECT * FROM two_menu order by id asc";
            $resultone=mysql_query($sqlone);
            while($rowTwo=mysql_fetch_assoc($resultone)){
            //获取原二级分类值
            $allId = $row[&#39;type2&#39;];
            //分开原二级分类的值(数据写入后成为了数组)
            $nowAllId=explode(&#39;,&#39;,$allId);
            if(in_array($rowTwo[&#39;id&#39;],$nowAllId)){
            ?>
            <input name="ID_Dele[]" id="ID_Dele[]" type="checkbox" name="type2" value="<?php echo $rowTwo[&#39;id&#39;] ?>" checked="checked" />
            <?php echo $rowTwo[&#39;name_menu&#39;];?>
            <?php
            }else{
            ?>
            <input name="ID_Dele[]" id="ID_Dele[]" type="checkbox" name="type2" value="<?php echo $rowTwo[&#39;id&#39;] ?>" />
            <?php echo $rowTwo[&#39;name_menu&#39;]; ?>
            <?php
            }
            }
            ?>

The above is the detailed content of How to modify checkbox in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn