如何获取单击按钮的 id 并在 php 函数中使用它

这里我们从 db 和 onclick 获取所有治疗,我想保存点击的治疗 ID。



      <?php

          global $treatments;

          foreach($treatments as $treatment){

            echo ' <li class='.$treatment['name'].' onclick=getTreatmentId('.$treatment['id'].')>'.$treatment['name'].'</li>';

          };

       ?>


我做了一个js函数


      <script>

          function getTreatmentId(id){

            

            return id; 

          }

      </script>

我想使用该 id 来获取具有相同处理 ID 的所有项目。像这样:


   <?php 

        global $iController;

        $items = $iController->getItemByTreatmentId(Here i want to send clicked id); 

        foreach($items as $item){

          echo '

          <div class="group">

          <div class="classic_manicure">

            <img src="'.$item['img_url'].'" alt="'.$item['name'].'" />

            <h3>'.$item['name'].'</h3>

            <p>'.$item['time'].' min</p>

            <p>'.$item['price'].'$</p>

            <button>BOOK IT</button>

          </div>

          ';

        }

        ?>

getItemByTreatmentId 方法:


  public function getItemByTreatmentId($id){

        $sql = "SELECT * FROM treatments_item WHERE treatment=$id";

        $treatments = mysqli_query($this->connection,$sql);

        $returnArray = array();

        while($row = mysqli_fetch_assoc($treatments)){

            array_push($returnArray, $row);

        }

        return $returnArray;

    }


12345678_0001
浏览 85回答 1
1回答

FFIVE

您可以将此 id 传递给控制器的操作(函数)或您的 id 需要执行查询操作的任何 php 页面。&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; function getTreatmentId(id){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "GET",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: "host/controller/action",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //here you can get response if you send from php-end&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function(xhr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do something on error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; complete: function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //do something on complete&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; </script>在php-end中你可以通过这种方式获取id&nbsp; <?php&nbsp;&nbsp; &nbsp; global $iController;&nbsp; &nbsp; $id = isset($_GET['id']) ? $_GET['id'] : null;&nbsp; &nbsp; $items = $iController->getItemByTreatmentId($id);&nbsp;&nbsp; &nbsp; foreach($items as $item){&nbsp; &nbsp; &nbsp; echo '&nbsp; &nbsp; &nbsp; <div class="group">&nbsp; &nbsp; &nbsp; <div class="classic_manicure">&nbsp; &nbsp; &nbsp; &nbsp; <img src="'.$item['img_url'].'" alt="'.$item['name'].'" />&nbsp; &nbsp; &nbsp; &nbsp; <h3>'.$item['name'].'</h3>&nbsp; &nbsp; &nbsp; &nbsp; <p>'.$item['time'].' min</p>&nbsp; &nbsp; &nbsp; &nbsp; <p>'.$item['price'].'$</p>&nbsp; &nbsp; &nbsp; &nbsp; <button>BOOK IT</button>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; ';&nbsp; &nbsp; }&nbsp; &nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP