Zoha Ali Khan
Zoha Ali Khan

Reputation: 1699

GET url in Codeigniter

I am using codeigniter for my project. To get the uri segments I know I can use

$this->uri->segment();

but my case is a bit different

My url looks like

localhost/mediabox/home/box/21

but once I go to this url a popup form apears in which the user provide a key to access this page and I validate the key using ajax method which is inside my home controller validate_key function

when I echo the url it gives me localhost/home/validate_key

while calling valiate_key of the home controller how can I get the 21 from the url wrritten in the url bar?

Any ideas?

Thanks

Upvotes: 1

Views: 24608

Answers (3)

GautamD31
GautamD31

Reputation: 28753

Its better and suggetsed to use hidden field and post the value when it is needed.

Upvotes: 0

Abdulaziz
Abdulaziz

Reputation: 2211

The problem:

It's not a bug, it's a natural behavior.

Consider the following:

  1. you request the validate_key function from the server by typing the URL in your address bar. current_url() returns localhost/blabla/validate_key. No AJAX involved.

  2. requesting validate_key with AJAX. The same PHP code will be executed.
    the current_url() will change to localhost/blabla/validate_key
    even though your browser's address bar is showing localhost/blabla/box/21.

So, what does this means? It means the Codeigniter base_url() doesn't care about your address bar, it cares about the function it is in, whether it was called via ajax or normal request.
so as long this function is being executed, the URL is pointing to it.

The solution:

My favorite solution to such a case, is to simply create a hidden input.

Simply, when a user requests the box function. you're showing him a popup form. so add a hidden_input field, give it a name and a value of 21(depends).

For example(you should tailor this to your specific needs):

Add this to your form in the view that get displayed by the box function:

form_hidden("number", $this->uri->segment(3));;

Now these data will be sent to your validate_key function. How do we access it? It's simple!

function validate_key(){

$this->input->post("number");//returns 21 or whatever in the URL.
//OR if the form sends GET request
$this->input->get("number");//return 21 or whatever in the URL.
/*
 *Or , you can do the following it's considered much safer when you're ONLY
 *expecting numbers, since this function(intval) will get the integer value of
 *the uri segment which might be a destructive string, so if it's a string
 *this function will simply return 0.
 */
$number = intval($this->input->post("number"));//returns 21 or whatever in the URL.
//Or if it it GET request:
$number = intval($this->input->get("number"));//returns 21 or whatever in the URL.
}

Upvotes: 3

peacemaker
peacemaker

Reputation: 2591

It looks like you've used .htaccess to remove the index.php part of the url. So, when you navigate to localhost/mediabox/home/box/21 you're passing the value 21 to the function named box in the controller named home

If you want to keep that value within the validate_key function, just pass it through when calling it:

function box($param)
{
    //$param = 21
    $this->validate_key($param);
}    

Upvotes: 0

Related Questions