Sequential Order of Checkboxes Selected PHP

三世轮回 提交于 2019-12-25 03:44:46

问题


At the moment, my html code is:

<form id = "query" method = "post" action = "search.php">
     <input type = "checkbox" name = "col_list[]" value = "host">host</input>
     <input type = "checkbox" name = "col_list[]" value = "atom_name>atom_name</input>
     …
     <input type = "submit">Submit</input>
</form>

And my php code:

 $columns = $_POST["col_list"];

Is there any way is which I can get the sequence in which the checkboxes where checked?


回答1:


you'd need to add a some javascript to get that info.

<input type="hidden" name="order"/> <!-- goes inside the form -->

Here is an examply using jQuery:

$('[name*="col_list"]').change(function(){
   if ($(this).prop('checked')){
       $('[name="order"]').val($('[name="order"]').val()+','+$(this).val())
    }
});

This should work fairly well with one caveat:

If the user un-checks then re-ckecks a checkbox it will be added to order twice.

order should result in something like:

,host,atom_name

Edit

Fixed some typos and here is a fiddle




回答2:


If the order is important you will have to implement some form of JavaScript or AJAX and call it when the check boxes are checked. Forms do not track the order in which fields are filled. JavaScript is well suited to this because it can trigger at the time of the click.

An alternative to AJAX could be adding a hidden field element and using JS to store the order clicked. Then it would be passed when the form is clicked.



来源:https://stackoverflow.com/questions/24660680/sequential-order-of-checkboxes-selected-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!