dplht39359 2015-08-08 10:50
浏览 34
已采纳

AJAX没有将数据传递给CodeIgniter Controller

Greeting StackOverflow! I am trying to pass data to controller using AJAX, but for some reasons it doesn't.

Controller:

public function updateSocial($key){
        if($key != null){
            list($name, $address) = explode("&&", $key);
            $this->db->query("UPDATE social SET address = '" . $address . "' WHERE name = '" . $name . "'");
            echo "Affected rows: " . $this->db->affected_rows();
        }
    }

Ajax:

function updateSocial(name){
                var address = $("#" + name).val();
                var key = name +"&&"+ address;
                //alert(key);
                $.ajax({
                    type: "GET",
                    url: "<?php echo base_url(); ?>controller/updatesocial", 
                    data: key,
                    dataType: "text",  
                    cache:false,
                    success: 
                        function(data){
                            alert(data);  //as a debugging message.
                        }

                });
            }

and HTML:

                <div class="row">
                    <div class="col-lg-10 col-md-10 col-sm-12 col-xs-12">
                        <label for="{name}">{name}</label>
                        <input id="{name}" class="form-control" name="{name}"value="{address}"/>
                    </div>
                    <div class="col-lg-2 col-md-2 col-sm-12 col-xs-12">
                        <div class="clearfix">&nbsp;</div>
                        <input type="submit" class="btn btn-material-green pull-right" onclick="updateSocial('{name}')"/>
                    </div>
                </div>

I've searched for solutions here on StackOverflow, tried everything and couldn't solve it.

If I manually enter values in url as following:

www.domain.com/controller/updatesocial/something&&another_something

it works, so the problem is not PHP-side.

What I'm trying to do: I have multiple input fields that needs to be modified individually, and I want to use AJAX to do so.

I'm sorry if same problem has been posted, but I couldn't find it. I've tried everything I could find and it doesn't work.

  • 写回答

5条回答 默认 最新

  • dsarttv037029 2015-08-08 11:44
    关注
    function updateSocial(name){
                var address = $("#" + name).val();
                var key = name +"&&"+ address;
                //alert(key);
                $.ajax({
                    type: "GET",
                    url: "<?php echo base_url(); ?>controller/updatesocial/"+key, 
                    dataType: "text",  
                    cache:false,
                    success: 
                        function(data){
                            alert(data);  //as a debugging message.
                        }
    
                });
            }
    

    You are sending data using GET method so you have to send it by appending url and your controller will be

    public function updateSocial(){
        $key = $this->uri_segment(3);
        if($key != null){
            list($name, $address) = explode("&&", $key);
            $this->db->query("UPDATE social SET address = '" . $address . "' WHERE name = '" . $name . "'");
            echo "Affected rows: " . $this->db->affected_rows();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)