I have 2 files: compare.php
(further mentioned as C) and compare-ajax.php
(C-A).
The part that adds item id to session and display them on a table works fine,
but I don't know why it doesn't erase the id (mentioned in the anchor element) from the Session array.
C:
1 <?php $session_start(); ?>
2 ....(some php,html code)
3 <script>
4 $( "#options" ).change(function() {
5 $cid= $("#options").val();
6 $("#teste").html($cid);
7 });
8 $(document).ready(function(){
9 $("#submitSearch").click(function(){
10 event.preventDefault(); // f important
11 $.post("compare-ajax.php",
12 { cid:$cid },
13 function(data){
14 alert ("test");
15 //var tds = $(originalTable).children('tr').children('td').length;
16 $("table tr").html("<td>"+data+"</td>");
17 });
18 });
19 });
20 </script>
...
C-A:(whole file)
<?php
1 session_start();
2 require "config.php";
3 if (!isset($_SESSION['cid'])) {
4 $comp_id=array();
5 $_SESSION['cid']=array();
6 array_push($_SESSION['cid'],$_REQUEST['cid']);
7 $comp_id = $_SESSION['cid'];
8 }else {
9 $comp_id=$_SESSION['cid'];
10 if (isset($_REQUEST['cid'])){
11 if (!is_numeric(array_search($_REQUEST['cid'],$_SESSION['cid']))){
12 if (count($comp_id)<=3) { // max number of items (ok)
13 array_push($_SESSION['cid'],$_REQUEST['cid']);
14 $comp_id = $_SESSION['cid'];
}
}
}
}
15 if (isset($_REQUEST['remove'])){
16 alert ($rem);
17 $rem=array_search($_REQUEST['remove'],$_SESSION['cid']);
18 if ($rem>=0) {
19 echo "Erasing!";
20 array_splice($_SESSION['cid'], $rem, 1);
21 $comp_id=$_SESSION['cid'];
}
}
22 echo "No of elements: ".count($_SESSION['cid']);
23 foreach ($comp_id as $item){
24 echo "<b>".$item."</b> || ";
25 $qu="SELECT DISTINCT * FROM car_tb WHERE c_id=".$item;
26 foreach ($dbo->query($qu) as $r) {
27 $t=1;
28 echo '<a id="test" href="'.$r[0].'">(('.$r[0].'))</a> ';
29 while ($t<=69): // max no. of elements for each item
30 echo "((".$r[$t].")) ";
31 $t=$t+1;
32 endwhile;
}
}
?>
33 <script>
34 $( "a" ).click(function( event ) {
35 event.preventDefault();
36 var r_id = $(this).attr("href");
37 $.post("compare-ajax.php",
38 { remove: r_id },
39 function(data){
40 alert ("test "+r_id);
41 alert ("This "+r_id+ " should be removed!");
42 $("table tr").append("<td>"+data+"</td>");
});
});
43 </script>
Needed: when I click the link (created on line 28 in C-A), the value that is in its href I want to be erased/removed from session. My problem is here: when I click the link(see line 28 in C-A) which is supposed to erase the corresponding id, I get the alert with the correct value, but nothing is erased from the session. I don't even get the alert (line 19 in C-A).