[英]i am trying to pass 3 variables in javascript [codeigniter post method]
It's a like dislike button, connected with a database.这是一个喜欢不喜欢的按钮,与数据库相连。 But it only takes the first variable "storyid".但它只需要第一个变量“storyid”。
<script type="text/javascript">
function savelike(storyid, username, autid)
{
$.ajax({
type: "POST",
url: "<?php echo site_url('site/homecontroler/savelikes');?>",
Data: "Storyid="+storyid,
success: function (response) {
$("#like_"+storyid).html(response+" Likes");
}
});
}
</script>
<p style=" width: 10%;float: left;"><a onclick="javascript:savelike(<?php echo $Data['postid'];?>, <?php echo $Data['firstname'];?>, <?php echo $this->session->userdata('userproid'); ?>);">
<i class="fa fa-thumbs-up" style="font-size: 25px; color: cornflowerblue;"></i>
<span id="like_<?php echo $Data['postid'];?>">
<?php if($Data['likes']>0){echo $Data['likes'].' Likes';}else{echo 'Like';} ?>
</span></a>
</p>
You miss the username and autid in Data您错过了 Data 中的 username 和 autid
<script type="text/javascript">
function savelike(storyid, username, autid)
{
$.ajax({
type: "POST",
url: "<?php echo site_url('site/homecontroler/savelikes');?>",
Data: "Storyid="+storyid+"&username="+username+"&autid="+autid,
success: function (response) {
$("#like_"+storyid).html(response+" Likes");
}
});
}
</script>
In Html pass the values as string missing '' in firstname and autid在 Html 中,将值作为字符串在 firstname 和 autid 中缺少 ''
<p style=" width: 10%;float: left;"><a onclick="javascript:savelike(<?php echo $Data['postid'];?>, '<?php echo $Data['firstname'];?>', '<?php echo $this->session->userdata('userproid'); ?>');">
<i class="fa fa-thumbs-up" style="font-size: 25px; color: cornflowerblue;"></i>
<span id="like_<?php echo $Data['postid'];?>">
<?php if($Data['likes']>0){echo $Data['likes'].' Likes';}else{echo 'Like';} ?>
</span></a>
</p>
Apologies for the formatting as I'm on mobile.为我在移动设备上的格式道歉。 Try defining the data to be sent like so:尝试定义要发送的数据,如下所示:
data: {
storyid: storyid,
username: username,
...
}
If that doesn't work please do a console log to see if you are getting the variables.如果这不起作用,请执行控制台日志以查看您是否正在获取变量。 If you aren't, then the issue is obvious.如果你不是,那么问题就很明显了。
PS you also haven't properly quoted your first paramter (postid)! PS你也没有正确引用你的第一个参数(postid)!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.