简体   繁体   English

使用AJAX将输入值传递给控制器​​的困难

[英]Difficulties using AJAX to pass input value to controller

I have this PHP CodeIgniter code where in the view I am getting input from a text field. 我有此PHP CodeIgniter代码,在该视图中,我从文本字段获取输入。 Using AJAC I am trying to pass this value to the controller using GET request. 使用AJAC,我正在尝试使用GET请求将此值传递给控制器​​。 The controller will then call a function from my model to retrieve a database record matching the search criteria. 然后,控制器将从我的模型中调用一个函数来检索与搜索条件匹配的数据库记录。

For some reason it doesn't work. 由于某种原因,它不起作用。 I tried to do a var dump in the controller to see if the value is passed by AJAX, but I am not getting anything. 我试图在控制器中进行var转储,以查看该值是否由AJAX传递,但是我什么也没得到。 Any ideas what I am doing wrong and why I can't receive the form value in the controller? 有什么想法我做错了,为什么我不能在控制器中收到表单值?

View: 视图:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.13.3/jquery.min.js"</script>
<script language="Javascript">
    $(document).ready(function () {
        $('#submitbutton').click(function () {
            $.ajax({
                url: "../../index.php/testcontroller/getdatabasedata",
                data: {
                    'searchvalue' : $('#inputtext').val() 
                },
                method: 'GET'
            }).done(function (data) {
                var dataarray = data.split('##');
                $('#question').html(dataarray[ 1 ]);
                $('#answer1').html(dataarray[ 2 ]);
            });
            return false;
        });
    }); 
</script>  
</body>

Controller 调节器

public function getdatabasedata()
{
    $this->load->model('testmodel');
    $year = $this->input->get('searchvalue');
    //I TRIED TO DO A VARDUMP($YEAR) BUT I DON'T GET ANYTHING!

    $movie = $this->testmodel->findquestion($year);
    $moviesstring = implode(",", $movie);
    echo $moviesstring;
}

Model 模型

function findquestion($searchvalue)
{
    $this->db->where('answer1', $searchvalue);
    $res = $this->db->get('questions');
    var_dump($res)
    if ($res->num_rows() == 0)
    {
        return "...";
    }
    $moviearray = $res->row_array();
    return $moviearray;
}

Script: 脚本:

<script 
  src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script language="Javascript">
$(document).ready(function ()
{
  $("#submitbutton").bind("click",function() 
    { 
      var target_url = '<?php echo(base_url()."testcontroller/getdatabasedata") ; ?>';
      var data = {'searchvalue' : $('#inputtext').val() }; 
      $.ajax ({

              url : target_url, 
              type: 'GET', 
              data: data, 
              cache: false, 
              success: function(controller_data)
                { 
                  var dataarray = controller_data.split('#'); 
                  $('#question').html(dataarray[1]); 
                  $('#answer1').html(dataarray[3]); 
                }, 
            }); 
            return false; 
    }); 
});
</script>

.bind("click",function() - add quotes to click event. .bind(“ click”,function()-在click事件中添加引号。

var dataarray = controller_data.split('#'); var dataarray = controller_data.split('#'); - split data caracter must match character in implode function in controller. -拆分数据角色必须与控制器内爆功能中的字符匹配。

Controller: 控制器:

public function getdatabasedata(){

        $this->load->model('testmodel');
        $year = $this->input->get('searchvalue');
        $movie = $this->testmodel->findquestion($year); 
        $separated = implode("#", $movie);
        echo $separated;

    }

Hope this helped. 希望这会有所帮助。

I will share my usual ajax code that I use in my views , make sure your base url is correct 我将分享我在视图中使用的常用ajax代码,确保您的基本url正确

$("#submitbutton").bind("click",function()
        {

            var target_url = '<?php echo(base_url()."testcontroller/getdatabasedata") ; ?>';
            $.ajax
            (
            {
                url : target_url,
                type: "GET",
                // data: {'searchvalue' : $('#inputtext').val()},
                cache: false,
                success: function(data)
                {
                    alert(data);
                },
                error: function(jqXHR, textStatus, errorThrown)
                {
                    alert("error during loading ....");
                }
            });

        });// end loading via ajax

and in your controller just echo something 在你的控制器中只是回声

public function getdatabasedata()
{
    //$this->load->model('testmodel');
    //$year = $this->input->get('searchvalue');
    //I TRIED TO DO A VARDUMP($YEAR) BUT I DON'T GET ANYTHING!

    //$movie = $this->testmodel->findquestion($year);
    //$moviesstring = implode(",", $movie);
    //echo $moviesstring;
    echo "hello";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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