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

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

Passing multiple variables in URL using codeigniter, Yes, it seems correct to do <a href="<?php echo site_url("post/edit/".$row->id); ?> ">Edit</a>. Just make sure that your action method (edit in this� how to pass variable in url in codeigniter. Ask Question Asked 4 years, if passing the variable in url that is bad practice , still if u want this u can use

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

Codeigniter passing variable to URL, In this example we are going to show you how to Pass parameter from controller to view file in CodeIgniter framework PHP. Here we using 2 files for pass� Anyway Instead of using GET I've decided to use POST by adapting a submit button (disguised as a link) with the variables in hidden input fields. I've created the following solution which seems to work fine, but am wondering whether I'm on the right track here or whether there is an easier way of passing variables via a link within CodeIgniter?

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;

Pass parameter from controller to view file, Currently I am passing the $id using URL like: $this->product($id) will only have a value for $id if that method is called as part of servicing a� The PATH_INFO environment variable will work as Colin describes on your development server but it tends to fail on servers using a CGI wrapper for PHP Setting your own environment variable in .htaccess allows you to use segmented url's together with query_strings.

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

Pass Id getting from URL to many function in CodeIgniter, You must pass true as the first parameter, otherwise, it will return the string representation of the current URL. URI Strings�. Many times, all you really want is to get� There's no problem with that function. The problem is probably in defining the value of that variable.Show us the code which defines the variable before and when you're calling the mydata function. – Ofir Baruch Mar 6 '12 at 14:07

Working with URIs — CodeIgniter 4.0.4 documentation, A URL with “product” as the first segment, and anything in the second will be and the “product_lookup_by_id” method passing in the match as a variable to the � From CodeIgniter's user manual: Note: If you use an object, the class variables will be turned into array elements. View Regardless of how you pass the data, it can be displayed like this:

URI Routing — CodeIgniter 3.1.11 documentation, how to pass id in url in codeigniter how to pass multiple data from controller to view in codeigniter how to pass multiple parameters from view to controller in� In this tutorial I have discussed how to pass data from controller to view in CodeIgniter. The process is very simple and you could easily render views through the controller. If you need help, you could add a comment below describing your issue.

Passing multiple variables in URL using codeigniter, CodeIgniter Passing Parameter for beginners and professionals with examples on mvc, url, route url, models, file system, url, Model, View, Controller, database� When I first started working with CodeIgniter, not using GET really threw me off as well. But then I realized that you can simulate GET parameters by manipulating the URI using the built-in URI Class. It's fantastic and it makes your URLs look better. Or if you really need GETs working you can put this into your controller:

Comments
  • why don't you use the ajax method in submit click to pass variables?
  • maybe he wants it in the url for bookmark-ability
  • Is there any way to do this <?php echo base_url(); ?>search?result="+state1+"&c="+city1 @M.Hemant