dtyqeoc70733 2010-11-25 08:38
浏览 24
已采纳

未选中的复选框和隐藏的输入问题


We have an edit form:we populate it with a variable number of checkboxes (it all depends,let's say, on how many pictures we have on the article we're about to edit).
All the checkboxes are per default "checked" and if nothing happens the data associated(our pictures in this case) remains untouched.
Altough if we uncheck one we actually want to not keep the picture in our edited post and delete it from our database.

I know unchecked checkboxes dont get Posted,and that I have one work-around:
place right before the checkbox an hidden field with the same name..in this way with the checkbox unchecked can still post the hidden input.
but i have this situation:

/*i'm querying the database to load all the article's data and I check if  */
/* there's any picture so I build, with a while loop, an associated checkbox*/
 <input type="checkbox" name="picture[]" value="$row['pic_id]"/>$row['pic_name'] 

When I submit our edit form I check if there's any data inside our array picture and if positive i want to delete it from database. Now if I simply add an hidden field at the top of each checkbox (so same name and same value..cause it's the value i need to use in the next query) I fall into the problem:
how to distinguish if the posted data comes from the actual checkbox (if eventually checked) or from the hidden input??

thanks again Luca

  • 写回答

2条回答 默认 最新

  • doujiexi1824 2010-11-25 09:43
    关注

    On the server side you can use the following approach:

    $array_of_ids_to_delete=array_diff($array_with_all_ids, $posted_ids);
    

    The $array_with_all_ids must be already available, because I guess you use it to output all the checkboxes. $posted_ids represents your picture[] array.

    After that you only have to run a foreach on $array_of_ids_to_delete and get rid of those images.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数