dongshao4207
dongshao4207
2019-04-27 05:02

如何使用codeigniter在url中传递变量?

已采纳

I am passing multiple variables like window.location.href="<?php echo base_url(); ?>search?result="+state1+"&c="+city1; instead of window.location.href="<?php echo base_url(); ?>search/"+state1+"/"+city1;

Now, the problem is when I define route i.e. $route['search?(:any)'] = "test/search?$1"; after a click on submit button then it shows an error on search page and print nothing. So, How can I resolve this issue? Please help me.

view:

<script>
    $(".submit").click(function(){
        state1 = $("#state1").val();
        city1 = $(".city1").val();
        window.location.href="<?php echo base_url(); ?>search?result="+state1+"&c="+city1;
    });
</script>

controller:

public function search($raw)
{
    echo $raw;
}

config/route.php

$route['search?(:any)'] = "test/search?$1";

Thank You

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • doucou19961205 doucou19961205 2年前

    Your routing is wrong. No need to route the url for access the $_GET values.

    Try below code.

    Change $route['search?(:any)'] = "test/search?$1"; to $route['search'] = "test/search";

    To get it values:

    $this->input->get('result');

    $this->input->get('c');

    点赞 评论 复制链接分享
  • dswm97353 dswm97353 2年前

    Your trying to use Get method values like Url parameter, Please try this code

    Jquery Code

         $(".submit").click(function(){
        state = $("#state").val();
        city = $(".city").val();
        window.location.href="<?php echo base_url(); ?>search?state="+encodeURIComponent(state)+"&city="+encodeURIComponent(city);
    });
    

    Route

    $route['search'] = "test/search";
    

    Controller

    public function search()
        {
            $state = $this->input->get('state');
            $city = $this->input->get('city');
        }
    
    点赞 评论 复制链接分享
  • douzhuang2570 douzhuang2570 2年前

    Currently what you are doing is sending $_GET Data to the controller, you will need to access the data by using

    $this->input->get();
    

    It is an array so you will automatically get all the variables you've sent.

    Alternatively you can send the data via segments as CI3 is designed to work with. In your controller you can pass the parameters as arguments to the function. e.g

    function search($param1,$param2,$param3){
    }
    

    Using this method this information can then be access by using your site URL plus the segment of data.

    www.test.com/index.php/controller/param1/param2
    

    You will also need to change your JS code to

    window.location.href="<?php echo base_url(); ?>search/" + state1 + "/" + city1;
    
    点赞 评论 复制链接分享
  • dsqa6272 dsqa6272 2年前

    Try this,
    POST:

    $(".submit").click(function(){
     var state1 = $("#state1").val();
     var city1 = $(".city1").val();
     $.ajax({
        beforeSend: function () {
        },
        complete: function () {
        },
        type: "POST",
        url: "<?php echo site_url('controller/cmethod'); ?>",
        data: ({state: state1 ,city: city1}),
        success: function (data) {
        }
     });
    });
    

    GET:

    $(".submit").click(function(){
     var state1 = $("#state1").val();
     var city1 = $(".city1").val();
     $.ajax({
        beforeSend: function () {
        },
        complete: function () {
        },
        type: "GET",
        url: "<?php echo site_url('controller/cmethod/'); ?>"+state1+"/"+city1 ,
        success: function (data) {
        }
     });
    });
    

    PHP:
    POST

    function search(){
        echo print_r($_POST);die;
    }
    

    GET

    function search($state,$city){
        echo $state;
        echo $city;
        die;
    }
    
    点赞 评论 复制链接分享

相关推荐