Good day,
I have a problem parsing data using href
get method to php retrieve by ajax to another page. Is it possible? btw I'm using CODEIGNITER.
Here's my html href code:
<a href="'.base_url().'homeadmin/view/page_user_profile/user_profile? user_id='.$key->user_id.'" class="btn btn-outline purple"> <i class="fa fa-pencil"></i> View</a>
Here's my PHP code parsing the id after button click:
function user_profile()
{
$user_id = $this->input->get('user_id');
$sql = $this->crud->getData("tbl_user as u,tbl_group as g, tbl_sub_group as sg, tbl_user_permission as up, tbl_user_profile as p", "u.user_id='$user_id' AND u.user_group_id=g.group_id AND u.user_sub_group_id = sg.sub_group_id AND up.user_user_perm ='$user_id' AND u.user_id = p.profile_id")['rows'];
echo json_encode($sql);
}
Then this is my ajax code:
function user_profile()
{
$.get(baseurl + 'homeadmin/user_profile', {}, function(data) {
var result = eval('(' + data + ')');
$('#user_name_profile').html(result[0].user_username);
})
}
Here is what should be the output:
<h1 class="font-green sbold uppercase" id="user_name_profile"></h1>
I hope someone will correct me. Thank you!
You need to bind the button to the function user_profile()
:
$( "a.btn" ).bind( "click", user_profile );
No need to define the href attribute. More information on binding here.
The empty object of your $.get()
invocation inside the function should be {user_id: $key->user_id}
. Make sure $key
is available in the scope of the function.