CodeIgniter: Passing variables via URL - alternatives to using GET
- by John Durrant
I'm new to CodeIgniter and have just discovered the difficulties using the GET method of passing variables via the URL (e.g. domain.com/page.php?var1=1&var2=2).
I gather that one approach is to pass the variables in the URI segments but haven't quite figured out how to do that yet as it seems to create the expectation of having a function in the controller named as the specific URI segment????
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?
I've created the following class in application/libraries/
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_variables {
function variables_via_link($action, $link_text, $style, $link_data) {
$attributes = array('style' => 'margin:0; padding:0; display: inline;');
echo form_open($action, $attributes);
$attributes = array('class' => $style, 'name' => 'link');
echo form_submit($attributes, $link_text);
foreach ($link_data as $key => $value){
echo form_hidden($key, $value);
}
echo form_close();
}
}
?>
With the following CSS:
/*
SUBMIT BUTTON AS LINK
adapted from thread: http://forums.digitalpoint.com/showthread.php?t=403667
Cross browser support (apparently).
*/
.submit_as_link {
background: transparent;
border-top: 0;
border-right: 0;
border-bottom: 1px solid #00F;
border-left: 0;
color: #00F;
display: inline;
margin: 0;
padding: 0;
cursor: hand /* Added to show hand when hovering */
}
*:first-child+html .submit_as_link { /* hack needed for IE 7 */
border-bottom: 0;
text-decoration: underline;
}
* html .submit_as_link { /* hack needed for IE 5/6 */
border-bottom: 0;
text-decoration: underline;
}
Link then created using the following code in the VIEW:
<?php
$link = new C_variables;
$link_data=array('var1' => 1, 'var2' => 2);
$link ->variables_via_link('destination_page', 'here is a link!',
'submit_as_link', $link_data);
?>
Thanks for your help...