0

Here we get all treatments from db and onclick I want to save the clicked treatment id.


      <?php
          global $treatments;
          foreach($treatments as $treatment){
            echo ' <li class='.$treatment['name'].' onclick=getTreatmentId('.$treatment['id'].')>'.$treatment['name'].'</li>';
          };
       ?>

I made a js function

      <script>
          function getTreatmentId(id){
            
            return id; 
          }
      </script>

And I want to use that id to get all items with the same treatment ID. Like this:

   <?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 method :

  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;
    }
Rina5
  • 37
  • 1
  • 5

1 Answers1

1

You can pass this id to an action(function) of a controller or any php page where your id needed to perform query operations.

    <script>
      function getTreatmentId(id){
       
                    $.ajax({
                    type: "GET",
                    url: "host/controller/action",
                    dataType: 'json',
                    data: {
                        id: id,
                    },
                    success: function(response) {
                        console.log(response);
                        //here you can get response if you send from php-end
                    },
                    error: function(xhr) {
                        //do something on error
                    },
                    complete: function(){
                        //do something on complete
                    }
                });
            }
      </script>

And in php-end you can get id in this way

  <?php 
    global $iController;
    $id = isset($_GET['id']) ? $_GET['id'] : null;
    $items = $iController->getItemByTreatmentId($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>
      ';
    }
   ?>
Omar
  • 901
  • 11
  • 14