简体   繁体   English

我正在尝试在 javascript [codeigniter post 方法] 中传递 3 个变量

[英]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.

相关问题 我正在尝试将一些 PHP 变量和 Javascript 变量传递到 Javascript window.location.href 中的 url - I am trying to pass some PHP variables and Javascript variable into url in Javascript window.location.href 我正在尝试将 SASS 变量导入到我的 javascript 文件中 - I am trying to import SASS variables into my javascript file 如何将变量从JavaScript传递到CodeIgniter - How to pass variables from JavaScript to CodeIgniter 我正在尝试将一个值从 servlet 文件传递​​到 javascript 文件,但我一直为 null 我做错了什么? - I am trying to pass a value from servlet file to javascript file and i am keep getting null where am i doing wrong? 我试图传递 HTML 代码和一些变量时不需要的逗号 - Unwanted commas while I am trying to pass HTML code and some variables 我试图使用post方法将值从ajax传递到codeigniter控制器,但它返回void - I tried to pass value from ajax to codeigniter controller using post method but it is returning void 如何在 POST 方法中传递多个变量? - How to pass multiple variables in a POST method? JavaScript新手。 我试图将单选按钮的值作为javascript函数中对象的名称传递 - new to javascript. I am trying to pass the value of a radio button as the name of an object in a javascript function 尝试在JavaScript弹出窗口中传递两个变量 - Trying to pass two variables in JavaScript popup 我正在尝试创建一个圆形进度条,但我无法访问 javascript 变量,为什么? - I am trying to create a circle progress bar, but I can not access the javascript variables, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM