0

I want to get the id_user data based on the desired row table by doing onclick jquery, then the id_user data can be passed to the controller and redirect page to edit_user, how do I get the id_user data with onclick using the jquery function? I beg for your help, sorry if any of my questions are unclear, if unclear, you can ask me

This is my code

function edit(){
    var id_user = $(this).attr("id_user");
    var url = $(this).attr("href");

    alert(id_user);

    console.log(id_user);
    $.ajax({
        type:'post',
        url : url,
        data :{id_user:id_user},
        success:function(data){
            alert(data)
        },
        error:function(err){
            alert(err)
        }
    });
}

This is my code

 <a onclick="edit()" id_user="<?php echo $row ['idx'] ?>" href="<?php echo BASE_URL. 'app.php/usermanagement/edit_user' ?>" class="edit">Edit</a>

This is my controller

public function edit(){

        Template::setTitle('Edit User Management');

        $id_user =(int)Request::post('id_user');

        //$id_user = (int)$_POST['id_user'];

        echo $id_user; die;

        //$id_user = (int)Session::get('idx');

        $result = $this->getUserbyId($id_user);      

        $dataresult = json_decode($result, true);
        if($dataresult === NULL) {
            echo "<script language = \"javascript\">window.alert(\No Have Data\");";
            echo "javascript:history.back();</script>";
            return false;
        }
        $data = $dataresult;
        return $data;

This is my controller code for function getUserbyId

public function getUserbyId($id_user){

    //$id_user = Request::post('id_user');

    echo json_encode($id_user);

    if(!empty($id_user)){

        $url="http://localhost:8585/get-user/$id_user";

        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPGET, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result =  curl_exec($curl);


        if($result === FALSE){

            die('Curl failed: ' .curl_error($curl));
        }
        curl_close($curl);
        return $result;
    }
}

my problem is id_user undetified

2
  • please add the full controller code Commented Jan 2, 2020 at 5:24
  • I have show my complete controller @Amit Sharma Commented Jan 2, 2020 at 6:16

1 Answer 1

1

Try following code: Change html code: You need to pass reference this object in edit function.Also href should be void otherwise page will be redirected to url.

<a onclick="edit(this)" data-id_user="<?php echo $row ['idx'] ?>" href="javascript:void(0)" data-url="<?php echo BASE_URL. 'app.php/usermanagement/edit_user' ?>" class="edit">Edit</a>

Changes in javascript code

function edit(obj){

    var id_user = $(obj).data("id_user"); //GET data attribute id_user
    var url = $(obj).data("url"); //Get data attribute url

    alert(id_user);

    console.log(url);
    $.ajax({
        type:'post',
        url : url,
        data :{'id_user':id_user},
        success:function(data){
            alert(data)
        },
        error:function(err){
            alert(err)
        }
    });

}
Sign up to request clarification or add additional context in comments.

1 Comment

I get data but, the page no redirect, how can I redirect page with bring data id_user ? @Harpal Singh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.