如何设置id并使用PHP在同一页面的下一部分中获取?

时间:2017-03-25 10:23:48

标签: php html

我需要当用户点击锚标记时,来自db的id将存储,并且在下一部分中它将被提取。我在下面解释我的代码。

NULL

此时,当用户点击锚标记时,相应的ID将存储并单击IS_ERR按钮,显示 <div class="fynd-space-itms"> <?php $classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc"); $sql="select * from category order by id desc"; $catdata=$dbobj->db_get_data($sql); $counter=0; foreach ($catdata as $v) { $id=$v['id']; ?> <div class="col-sm-3"> <a href="javascript:void(0)" class="<?php echo $classarr[$counter];?>"><?php echo $v['name'] ?></a><!-- activemap1 --> </div> <?php $counter++; if($counter==4){ $counter=0; } } ?> <button class="btn nextbtnbgdiv open2" type="button">Next <span class="fa fa-arrow-right"></span></button> <div class="fyndspacecategory fyndsheightsubc nano"> <div class="nano-content"> <?php //need id here ?> </div> </div> 的底部部分,我需要在此处获取ID。所有代码都在同一页面内。

1 个答案:

答案 0 :(得分:0)

好吧,从你想要在同一页面上加载数据的事实开始,你不想重新加载页面来完成它,你将不得不使用AJAX,但如果重新加载页面不是问题你没有它可以通过使用iframe来完成它(但这不是一个很好的方式来完成它)。

我将开始假设您将使用vanilla Javascript来解决此问题而不是使用JQuery。首先,我们需要看一下fetch js API

所以让我们开始根据你的例子编写一些代码:

    <div class="fynd-space-itms">
        <?php
           $classarr = array("item-exhibitation maploc", "item-parking maploc", "item-offices maploc", "item-storage maploc");
           $sql="select * from category order by id desc";
           //$catdata=$dbobj->db_get_data($sql);
           $catdata = [["id"=>1,"name"=>"foo"],["id"=>2,"name"=>"bar"],["id"=>3,"name"=>"oxo"]];
           $counter=0;
        ?>
        <?php
        //Using php loops shorthands to get an easier to read code
        foreach ($catdata as $key => $v):
        ?>
        <div class="col-sm-3">
        <button onclick="loadData(<?php echo $v["id"]; ?>);" class="<?php echo $classarr[$counter];?>"><?php echo $v['name'] ?></button><!-- activemap1 -->
        </div>
        <?php
          //Automating your class array index reset at any size
          $counter = ( $key%sizeof($classarr) ) ? $counter+1 : 0 ;
          endforeach;
        ?>

    <div class="fyndspacecategory fyndsheightsubc nano">
    <div class="nano-content" id="content-wrapper">
    </div>
    </div>
    <script>
      function loadData(id){
        var data = new FormData();
        data.append("data",JSON.stringify( {
          id: id
        }));
        var request = new Request('./loadData.php', {
        method: 'POST',
          body: data
        });

        fetch(request).then(function(r) {
          if(r.status != '200'){
            return false;
          }
          r.text().then(function(data){
            document.getElementById('content-wrapper').innerHTML = data;
          });
        });
      }
    </script>

当单击一个按钮时,它将使用fetch API发送一个包含类别id的formData,向一个名为loadData.php的php脚本发送请求。请记住,fetch返回一个promise,你必须等到它被解析,所以我们使用该方法在完成时执行一些函数。

获取承诺可以解析为text,json和其他格式,如here解释。

好吧,让我们来看看loadData.php:

    <?php
    //some db stuff here
    $data = json_decode($_POST["data"],true);
    $sql="select * from category order where id = ".$data['id'];
    //$data=$dbobj->db_get_data($sql);
    $data = "Some Fancy Data Here";
    print $data;
     ?>

这里就像调用数据库函数和格式一样简单,并将数据打印为字符串以供请求读取。如果你想返回一个数据数组,你应该想要json_encode php数组并打印它。

希望它可以帮到你!