dqbn76906 2014-07-29 18:32
浏览 39

使用jQuery从Session数组中删除一个项目

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).

  • 写回答

1条回答 默认 最新

  • douxuan3095 2014-07-29 18:53
    关注

    I see some questionable code, but let's start with the "C" file. It appears you are assigning the "cid" variable to what appears to be a php variable "$cid". This won't work unless you change $cid to if it's numeric. If "$cid" is a string, then change that to to have php wrap it in quotes. Of course, make sure $cid is assigned something! Start there and let me know how you make out.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c