skip to Main Content

I am having the trouble to find a solution how I can get the values from the object which is requested from link from my web..

The thing is that I was created method in PHP to get the data from the database of the values of one object which I have to parse in my modal window so I don’t have to refresh page to get details about my product.

Here is PHP code to get the details which perfectly works and my URL returning the $data object with values.

Controller.php

     public function orderinfo($id){
              $orderInfo = $this->adminsModel->getOrderInfo($id);
              $data=['orderInfo'=>$orderInfo];
              $this->view('admin/orderinfo',$data);
          }

And the model function for php:

        public function getOrderInfo($id){
             $this->db->query("SELECT * FROM ORDERS WHERE id ='$id'");
            $row = $this->db->single();
            return $row;
        }

And the thing is that I learned easily how to get id in javascript of my object which has the id in database.
here is code to get id and i understand it how it works:

HTML:

<a class="fa fa-file-audio-o edu-back-restart" href="#"
 data-toggle="modal" data-target="#InformationproModalftblack"
 id="<?php echo $activeOrders->id; ?>"
 onclick="showDetails(this)">INFO</a>

NOTWORKING CODE:/mytry/
Javascript to get object id and to get object and its values (object and values is my problem):

    <script>
  function showDetails(a) {
   $("#"+a.id).click(function () {
         alert(a.id);
      });
    //NOT WORKING-How to get object from url?
    $.ajax({
    url: "localhost/test/orderinfo/280",
        method: "GET",
        datatype: Object,
        success: function(response){
    var customer =response;
           console.log(response);
    }
    });


}
</script>

I don’t know how to get whole object from that url with js or ajax and i don’t know how to get object values as : id, name, street..

Thank you so much for you help…
sorry if i have some mistakes in explanation the problem.

2

Answers


  1. When you are running the ajax call, the URL field should be referencing the PHP script that you are trying to run, e.g:

    url: "localhost/test/orderinfo/Controller.php",
    

    Next, make sure the PHP script is calling the orderinfo() function at some point. If you have this function in a larger script and don’t have any logic for invoking it, I would recommend putting the function in a smaller PHP file whose sole purpose is to return the output of that query. For example:

    //Whatever you need to import to make your query calls
    //Whatever variables you need to initialize for your query calls
    $temporary_id = "ABC123";
    public function orderinfo($id){
          $orderInfo = $this->adminsModel->getOrderInfo($id);
          $data=['orderInfo'=>$orderInfo];
          $this->view('admin/orderinfo',$data);
    }
    return orderinfo($temporary_id);
    

    If you could provide any information about the object you are returning as well as the output of that console log, that would be extremely helpful.

    Edit: Just noticed the comments, you could pass the id value in as data:

    url: "localhost/test/orderinfo/Controller.php",
    data: {'id':id_variable},
    

    And then in the PHP, get the id value using $_SESSION['id'];.
    Alternatively, you could pass it as a URL parameter:

    url: "localhost/test/orderinfo/Controller.php?id=ABC123",
    data: {'id':id_variable},
    

    And get in the PHP using:

    $id = $_GET['id'];
    

    The value of the ID should be stored in that PHP variable as ABC123.
    Hope this helps.

    Login or Signup to reply.
  2. The best way for sending data from PHP to JS is encoding them as JSON object with json_encode.

     public function orderinfo($id){
         $orderInfo = $this->adminsModel->getOrderInfo($id);
         $data=['orderInfo'=>$orderInfo];
         $this->view('admin/orderinfo',json_encode($data));
     }
    

    so in your JS you can decode it with parseJSON like

    <script>
      function showDetails(a) {
        $.ajax({
        url: "/test/orderinfo/280",
            method: "GET",
            datatype: JSON,
            success: function(response){
                var obj = jQuery.parseJSON(response);
                console.log(obj);
            }
        });
    }
    </script>
    

    Note that you don’t need to return whole $data in this object, and probably you even should not do it for performance and security reasons.

    Instead, just prepare the object with only the data required for JS and send them as shown.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search