dongtuan1594 2018-01-31 12:10
浏览 43
已采纳

如何在没有复选框的php中发布空白数组(既不是NULL也不是“”)值

I have a list of checkboxes and if checked their values are posted to json file which is working fine. When user uncheck all of them I get NULL value in array and if I set its value="" its returns depends :[""]. what I want is if user did not check any checkbox it returns depends:[] only neither NULL nor "".

Here is my code:

<label for="depends">Linked With</label></br>

<input type="checkbox" name="depends[]" value="1"  id="ch2" > 1
<input type="checkbox" name="depends[]" value="2"  id="ch3" > 2
<input type="checkbox" name="depends[]" value="3"  id="ch5" > 3
<input type="checkbox" name="depends[]" value="4"  id="ch6" > 4
<input type="checkbox" name="depends[]" value="5"  id="ch7" > 5
<input type="checkbox" name="depends[]" value="6"  id="ch8" > 6
<input type="checkbox" name="depends[]" value="7"  id="ch9" > 7

in php code I am calling it as

'depends'=>$_POST['depends']
  • 写回答

1条回答 默认 最新

  • dongshungai4857 2018-01-31 12:20
    关注

    in your php code you can handle it like that :

    <?php
        /* if $_POST['depends'] is not empty then you store the value, else you create an empty array [] */
        $var = isset($_POST['depends']) ? $_POST['depends'] : array(); /* you can replace $var */
        print_r($var);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊