简体   繁体   中英

How to pass variables in url using codeigniter?

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 ie $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

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

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;
}

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. eg

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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