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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?