简体   繁体   中英

how to send id from view to controller in anchor tag in codeigniter in ajax success function?

hey guys i am trying to send a id from view to controller using anchor tag.but the value that it shows on the controller is just the name.let me show you my code

<script type="text/javascript">
jQuery('#search_form').submit(function(e){    
e.preventDefault();
var formData = new FormData(this);
jQuery.ajax({
type:'POST',
url:'<?php echo base_url("user/search_friends"); ?>',    
data:formData,
dataType:'json',
cache: false,
contentType: false,
processData: false,
success:function(data)
{      
console.log(data);
var ParsedObject = JSON.stringify(data);            
var json = $.parseJSON(ParsedObject);    
$id=json.id;
$.each(json, function (key, data) {       
var uname=data.uname;
var id=data.id;
alert(id);    
$('#search_result').append('<li class="list_style"><a href="<?php echo base_url('user/profile_friend/data.id') ?>">'+uname+'</a></li>');    
});
}
});
});
 </script>

Now what i am doing is searching a username and showing the results, the results shown are in anchor tag thus the data has a link on it i want to send the user to its profile page using the id but the id does not get passed to the controller can you tell me what i am doing wrong?

change this

$('#search_result').append('<li class="list_style"><a href="<?php echo base_url('user/profile_friend/data.id') ?>">'+uname+'</a></li>');

to

$('#search_result').append('<li class="list_style"><a href="<?php echo base_url('user/profile_friend/') ?>' + data.id + '">'+uname+'</a></li>');

Found the error hope it help someone else who wants to send data like this.

Just add slash like below

$('#search_result').append('<li class="list_style"><a href="<?php echo base_url('user/profile_friend/') ?>' +'/' +data.id + '">'+uname+'</a></li>');

That will send the value to the controller method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM