dongyanjing5975 2014-02-06 18:18
浏览 53
已采纳

重新填充页面上的复选框的方法?

I have a bit of an odd/unique situation, where I am currently looking for a way to 're-populate' checkboxes on my page.

First some background on how the page/app works.

1.) state1: You get simple search form. Enter in first or last or state, etc., hit submit. It queries a DB and posts back to the SAME Page (?mode=results).

Inside of a MAIN container DIV, I loop through the returned records, and create (echo) a TABLE with a certain tabular layout to parse the values from each record/row (name, date, ID#, etc).

If I have 5 records returned from DB, I loop through and create 5 tables right after each other, populating each with the unique data from the record/row.

In each one of these tables I create, along with the data from the DB record/row. I also create/add in several check boxes.

Using some jQuery and on $(document).ready, I listend for the onClick event for all these checkboxes. So when clicked, I grab some data from the checkbox, as well as some parent info, and do the following:

a.) I create/add another element to the stage
b.) Make an Ajax call to an external .php script, so that I can update the SESSION with my array I have build (collecting all the user checks/input)

I build the array in JS/jQuery and use the Ajax call to get this data into my PHP session (anyone knows a way to NOT use this external .php script to update the SESSION var, let me know please!)

So to re-cap so far: There is no "FORM" (tags), just check boxes in multiple tables. There really is no 1-button (submit) event that POSTS all the checkbox data anywhere at one time. Each time a checkbox is clicked (or un-clicked), I update my SESSION array. That if/when a user IS ready to 'check out' (for lack of better term) (leave the page and go to another page), the SESSION data/array is 'current' and the data is ready to be used on any following pages.

so far everything posted above works just fine.

The question I have is: If the user wants to go BACK to this MAIN PAGE with all the checkboxes, what nice, simple/elegant solutions do I have to re-populate all these checkboxes?

I'm thinking the only approach I have is the check and see if this SESSION array exists when main page loads (meaning it's a re-visit, not the first time), and try to loop through this array and somehow target elements on my stage to find the correct table, then the checkbox ID?

Usually use jQuery to walk the DOM - not PHP? And what about toggling the checkbox with PHP?

Is there a nice way to do this?

  • 写回答

2条回答 默认 最新

  • doushanlv5184 2014-02-06 21:09
    关注

    thanks for the suggestions.. this is what worked for me in the end (so far)

    function sessionCheck($recordID, $checkBoxID){
        for($r = 0; $r< count($_SESSION['userPicks']); $r++){
            if($_SESSION['userPicks'][$r]['recordid'] == $recordID){
                for($z=0; $z < count($_SESSION['userPicks'][$r]['itemsordered']); $z++){
                    if($_SESSION['userPicks'][$r]['itemsordered'][$z]['name'] == $checkBoxID){
                        return 'checked="checked"';
                    }else{
                        return "";
                    }
                }
            }else{
                return "";
            }
        }
    }
    

    usage for MY project is like this:

     <?=sessionCheck($row["id"] ."-A","Testimonial History") ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?