skip to Main Content

I did some research on this topic but i cant run this id is not send to page.

listdata.php

<td class="text-center" style="min-width:130px;">
    <button style="width:100%" class="btn btn-primary detail-customer" data-id="<?php echo $m->id; ?>">Bilgi</button>
</td>

ajax.php

$(document).on("click", ".detail-customer", function() {
    var id = $(this).attr("data-id");

    $.ajax({
        method: "POST",
        url: "<?php echo base_url('customers/info'); ?>",
        data: "id=" + id
    })
    .done(function(data) {
        window.location = "customers/info";
    })
})

controller:

public function info()
    {
        $data['page'] = "customer_information";
        $data['userdata'] = $this->userdata;
        $id= trim($_POST['id']);

        print $id;
        //$this->template->views('customers/info', $data);
    }

2

Answers


  1. Are you naming your ajax file as Ajax.php? is that a PHP file? It definitely should be a js file.

    From what I know from Jquery and Ajax, url key takes the location of the PHP file, and the data key should be passed as an object. Take these two files as an example:

    Ajax.js

    
    $(document).on("click", ".detail-customer", function() {
        var id = $(this).attr("data-id");
        $.ajax({
            type: "POST",
            url: "/absolute_path/to/php_file.php",
            data: {my_id:id, my_custom_key: "my_custom_value"},
            dataType: "JSON",
            success: function (response) {
                console.log(response);
            },
            error: function(response){
                console.log(response);
            }
        })
        .done(function(data) {
            window.location = "customers/info";
        })
    })
    

    Php_file.php

    <?php
    $id = $POST["my_id"];
    $customValue = $POST["my_custom_value"];
    $response = ["message"=> "We took your id which is $id and your custom value which is $customValue"];
    echo json_encode($response);
    ?>
    

    This is a very basic example of how to use ajax.

    Login or Signup to reply.
  2. Please try it

    $(document).on("click", ".detail-customer", function() {
        
            var id = $(this).data("id");
            $.ajax({
                method: "POST",
                url: "<?php echo base_url('customers/info'); ?>",
                data: {"id":id},
                Content-Type:"json"
            });
            .done(function(data) {
                window.location = "customers/info";
            })
        });
    

    "we do not read your complete controller code
    if your code is correct total then please
    first convert data in json then
    return it.
    "

    public function info()
        {
            $data['page'] = "customer_information";
            $data['userdata'] = $this->userdata;
            $id= trim($_POST['id']);
            echo json_encode($id);
            //$this->template->views('customers/info', $data);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search