0

我在第一个 php 页面中有一个学生复选框列表和一个搜索框。在 ajax 页面中,我将获得搜索结果。

但问题是,如果用户选择了第一个搜索结果并再次搜索,第一次搜索的选择会丢失

如何在第一次搜索时保存选择?我尝试在会话数组中进行选择,但它不起作用

//check list
    $qry="select cand_id,name from candidate where inst_id=".$_SESSION['inst_id']."";
    $res=$ob->select($qry,$connect);
    while($rw=pg_fetch_row($res))
    {

        echo"<br><input type=\"checkbox\" name=\"check[]\" value=\"$rw[0]\">";echo$rw[1];echo"<br>" ;
    }
//Ajax page
if($ajaxData!="")
{
                                if($_SESSION['usertype_id']==1)
                                {
                                $qry="select cand_id,name from candidate where name like'$dataup%'  ";

                                }
                                else if($_SESSION['usertype_id']==2)
                                {
                                $qry="select cand_id,name from candidate where inst_id=".$_SESSION['inst_id']."
                                and  name like'$dataup%' ";
                                }
                                $res=$ob->select($qry,$connect);
                                $words=array();
                                $count = pg_num_rows($res);
                                if($count>0)
                                {
                                    $i=0;
                                    //echo"<div  style=\"width: 200px; height: 200px;overflow-y: auto;padding-top: 10px;padding-right: 0px;padding-bottom: 0.25in;\">";
                                    while($rw=pg_fetch_row($res))
                                    {
                                        $words[$i]=$rw[0];$i++;


                                    }

                                    $_SESSION['checkAjax']=$words;//can_id array
  1. 如何使用数组$_SESSION['checkAjax']突出显示?
  2. 为什么$_SESSION['checkAjax']在每个 ajax 调用上都取消设置?
  3. 只需要更改所选学生姓名的颜色
4

1 回答 1

1

假设您有这样的复选框代码:

<input type="checkbox" name="options[]" value="student"> Student
<input type="checkbox" name="options[]" value="teacher"> Teacher
<input type="checkbox" name="options[]" value="professor"> Professor

如果是这种情况,您可以通过这种方式在服务器端检查它,我再次假设它是通过POST

<input type="checkbox" name="options[]" value="student"<?php echo (in_array("student", $_POST["options"])) ? ' checked="checked"' : ''; ?>> Student
<input type="checkbox" name="options[]" value="teacher"<?php echo (in_array("teacher", $_POST["options"])) ? ' checked="checked"' : ''; ?>> Teacher
<input type="checkbox" name="options[]" value="professor"<?php echo (in_array("professor", $_POST["options"])) ? ' checked="checked"' : ''; ?>> Professor

希望这可以帮助!:)

于 2012-09-07T02:23:38.933 回答