如何在php中突出显示复选框列表中的AJAX搜索结果而不丢失复选框选择?
在第一个php页面中,我有一个学生的复选框列表和一个搜索框。在ajax页面中,我将获得搜索结果 但问题是,如果用户选择了第一个搜索结果并再次搜索,则第一次搜索的选择将丢失 如何在第一次搜索时保存选择?我已尝试在会话数组中进行选择,但它不起作用如何在php中突出显示复选框列表中的AJAX搜索结果而不丢失复选框选择?,php,ajax,checkboxlist,Php,Ajax,Checkboxlist,在第一个php页面中,我有一个学生的复选框列表和一个搜索框。在ajax页面中,我将获得搜索结果 但问题是,如果用户选择了第一个搜索结果并再次搜索,则第一次搜索的选择将丢失 如何在第一次搜索时保存选择?我已尝试在会话数组中进行选择,但它不起作用 //check list $qry="select cand_id,name from candidate where inst_id=".$_SESSION['inst_id'].""; $res=$ob->select($qry,
//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
//检查列表
$qry=“选择cand_id,从inst_id=“.$_SESSION['inst_id']”处的候选人中选择姓名”;
$res=$ob->select($qry,$connect);
而($rw=pg_fetch_row($res))
{
echo“
”echo$rw[1];echo“
”;
}
//Ajax页面
如果($ajaxData!=“”)
{
if($\u会话['usertype\u id']==1)
{
$qry=“选择cand_id,从候选名称中选择名称,如“$dataup%”;
}
else if($\u会话['usertype\u id']==2)
{
$qry=“选择cand_id,从inst_id=“.$_SESSION['inst_id']处的候选名称。”
名称为“$dataup%”;
}
$res=$ob->select($qry,$connect);
$words=array();
$count=pg_num_行($res);
如果($count>0)
{
$i=0;
//回声“;
而($rw=pg_fetch_row($res))
{
$words[$i]=$rw[0];$i++;
}
$\u SESSION['checkAjax']=$words;//can\u id数组
$\u SESSION['checkAjax']
突出显示我假设您对复选框有这样的代码:
<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
>老师
谢谢您的帮助。但是在本例中没有表单提交,而是使用AJAX。这很好。您可以使用.serialize()
jQuery的函数对吗?只需包含jQuery库并使用$.ajax
。非常简单。请检查。是否在代码开头调用该函数来启动/恢复会话?会话启动在模板中,我已将其包含在上面的页面中。