I have a check box list of students and a search box in first php page .In ajax page I will get the search results .

But the problem is ,if user selected first search result and searched again selection on the first search is lost

How can I save selections on the first search ?I have tried to make selections in an session array but it is not working

//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. How do I use array $_SESSION['checkAjax'] for highlighting?
  2. Why $_SESSION['checkAjax'] unset on each ajax call?
  3. Just need change of color on selected student names

Dani AI

Generated

As hinted, the first things to check are session usage and how you update the session array. Two common causes of "lost" selections are: (1) forgetting to call session_start() at the top of every PHP file that reads/writes $_SESSION (both the page that renders checkboxes and the AJAX endpoints), and (2) replacing the session key on each search instead of merging/toggling selections. Also note: if you use the Fetch API you must include credentials (cookies) so PHP sees the same session.

Server-side pattern (safe, simple)

  • Keep a single session key that maps selected candidate IDs to true (associative array for fast lookups).
  • Have a small AJAX endpoint that toggles one ID on/off in that session array.
  • When returning search results, mark checkboxes checked and add a CSS class if the ID exists in the session array.

Example (conceptual — adapt column names/security checks):

// save_selection.php (must be the first line)
session_start();
$id = (int)($_POST['id'] ?? 0);
$checked = ($_POST['checked'] ?? '') === '1';
if ($id > 0) {
    if (!isset($_SESSION['selected'])) $_SESSION['selected'] = [];
    if ($checked) $_SESSION['selected'][$id] = true;
    else unset($_SESSION['selected'][$id]);
    session_write_close(); // release session lock quickly
    echo json_encode(array_keys($_SESSION['selected']));
}

When rendering AJAX search HTML, check the session array and emit checked + a selected class:

// while rendering each row
$is = isset($_SESSION['selected'][$rowId]);
echo "<label class=\"candidate" . ($is ? " selected" : "") . "\">";
echo "<input type=\"checkbox\" class=\"candidate\" value=\"$rowId\"" . ($is ? " checked" : "") . ">";
echo htmlspecialchars($name) . "</label>\n";

Client-side notes

  • Use fetch/jQuery with cookies: fetch(..., { credentials: 'same-origin', method:'POST', body: formData }).
  • On checkbox change, POST the id+checked flag to the toggle endpoint and immediately add/remove the .selected class so the UI updates without waiting.

Quick troubleshooting checklist

  • session_start() is called at top of every PHP file that uses $_SESSION.
  • Check the Network tab to verify the Cookie header is sent with AJAX.
  • Don’t overwrite the session selection array wholesale on each search; merge/toggle instead.
  • Use session_write_close() in short AJAX handlers to avoid PHP session locking when you have many simultaneous requests.

CSS highlight example:

.candidate.selected { background:#fffae6; }

This keeps selections across searches, highlights chosen names, and avoids losing prior choices.

Hi. To use the $_SESSION array you must start a session first. The very first thing on your file should fire the function session_start();

This needs to be right at the top of the file (before any output to the browser).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.