dongshao4207
2019-04-27 05:02
浏览 47

如何使用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 2019-04-27 05:25
    已采纳

    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');

    已采纳该答案
    打赏 评论
  • dsqa6272 2019-04-27 05:29

    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;
    }
    
    打赏 评论
  • douzhuang2570 2019-04-27 09:12

    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;
    
    打赏 评论
  • dswm97353 2019-05-06 12:14

    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');
        }
    
    打赏 评论

相关推荐 更多相似问题