$_REQUEST not working in codeigniter - codeigniter

i dont know what am i doing wrong but i am just messed a bit
i have a a href thats calling a controller
<img src="<?php echo base_url();?>assets/front_assets/images/button_joinclub.png" width="180" height="44" border="0">
now the function is this
class mailing extends CI_Controller{
private $pagesize = 20;
function __construct() {
parent::__construct();
#session_start();
}
function index()
{
echo $_REQUEST['club'];
}
}
but it gives me error
A PHP Error was encountered
Severity: Notice
Message: Undefined index: club
Filename: controllers/mailing.php
Line Number:12
EDIT
i need to call the mailing/index from different pages, and sometimes i need to pass parameter and sometimes not
if i use
function index($club)
{
//function body;
}
then i always need to send some parameter
sometimes the calling href can be like this also
<img src="<?php echo base_url();?>assets/front_assets/images/button_joinclub.png" width="180" height="44" border="0">
so it will call for an error since in function definition i have issued the presense of a parameter, and i am not passing any parameter through this link
so thats why i need
a href="<?php echo base_url();?>mailing/index/?club="<?php echo $club_info[0]['club_title'];?>"
so that i can use isset($_REQUEST['club'] to check if present or not.

First of all, there is no need to echo base_url(); Only /mailing/index is enough. To pass parameters you do as McGarnagle told you as third segment.
Then in your controller in index function:
$club_title = $this->uri->segment(3);
You have just set a new variable called club_title that holds its value. That's how you pass params and if you dont want to pass it from other pages, you don't need to. It only means that the variable will be null in that case.
The way URI helper works so you understand what happened:
Controller - segment 1
Method - segment 2
Paramater - segment 3
You can add as many parameters as you want and then call access them with URI. Make sure you load it in your autoload.php in config folder or in construct function of your each controller like this:
$this->load->helper('url');
PS: We never use $_REQUEST in codeigniter.

CodeIgniter disables all GLOBALS except $_GET, $_COOKIE and $_POST to ensure security.
Reference:
Register_globals
During system initialization all global variables are unset, except those found in the $_GET, $_POST, and $_COOKIE arrays. The unsetting routine is effectively the same as register_globals = off.
See Documentation here

CodeIgniter comes with helper methods that let you fetch POST, GET, COOKIE or SERVER items , but CodeIgniter disables all GLOBALS except $_GET, $_COOKIE and $_POST to ensure security.
You can use input methods are as follow :
$this->input->post()
$this->input->get()
$this->input->cookie()
$this->input->server()

CodeIgniter purges the $_REQUEST variable for security reasons. I assume it's related to the automatic input filtering described in the Codeigniter Manual here, but it's not specifically mentioned there either though. I am unsure whether setting
$config['global_xss_filtering'] = TRUE;
in config.php affects it or not.

Related

how i pass multiple variables from view to controller in codeigniter inside form_open

my view page
i have variables $month,$year,$id.in my viewpage.i want to pass this to controller function using echo form_open for updation
echo form_open('money_c/updatemanualdata/'.$id,$month,$year);?>
how will call this in my controller money_c
function updatemanualdata('')..?>here what will i put please help
If your form action will be
echo form_open('money_c/updatemanualdata/'.$id.'/'.$month.'/'.$year);
You can catch that in your controller using url segment like this.
be sure to load the url helper $this->load->helper('url')
function updatemanualdata($id, $month, $year){}

Add parameter to Codeigniter URL

I have a problem when i try to add other parameter to URL.
before i use Codeigniter i add those parameters using JavaScript like this
test
but when i tried to do it with Codeigniter i don't know how.
<?php echo anchor("home/index/param1","test"); ?>
as i said i want to add this parameter for example my URL looks like this
home/index/param2
so when i click on test i want the URL to be like this
home/index/param2/param1
Take a look at CodeIgniter's URL Helper Documentation
The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.
For your example, you could try:
<?php
$base_url = 'home/index/';
$param1 = 'param1';
$param2 = 'param2';
$segments = array($base_url, $param1, $param2);
echo anchor($segments,"test");
?>
You can't do that with the form helper, you have to use your js function again :
echo anchor("home/index/param2", "test", array("onClick" => "javascript:addParam(window.location.href, 'display', 'param1');"));
It will produce :
test
But I don't see the point of dynamically change the href on the click event. Why don't you set it directly at the beginning ?
echo anchor("home/index/param2/param1", "test");

JavaScript code in view issue in Laravel

I put JavaScript code in a view file name product/js.blade.php, and include it in another view like
{{ HTML::script('product.js') }}
I did it because I want to do something in JavaScript with Laravel function, for example
var $path = '{{ URL::action("CartController#postAjax") }}';
Actually everything is work, but browser throw a warning message, I want to ask how to fix it if possible.
Resource interpreted as Script but transferred with MIME type text/html
Firstly, putting your Javascript code in a Blade view is risky. Javascript might contain strings by accident that are also Blade syntax and you definitely don't want that to be interpreted.
Secondly, this is also the reason for the browser warning message you get:
Laravel thinks your Javascript is a normal webpage, because you've put it into a Blade view, and therefore it's sent with this header...
Content-Type: text/html
If you name your file product.js and instead of putting it in your view folder you drop it into your javascript asset folder, it will have the correct header:
Content-Type: application/javascript
.. and the warning message will be gone.
EDIT:
If you want to pass values to Javascript from Laravel, use this approach:
Insert this into your view:
<script type="text/javascript">
var myPath = '{{ URL::action("CartController#postAjax") }}';
</script>
And then use the variable in your external script.
Just make sure that CartController#postAjax returns the content type of javascript and you should be good to go. Something like this:
#CartController.php
protected function postAjax() {
....
$contents = a whole bunch of javascript code;
$response = Response::make($contents, '200');
$response->header('Content-Type', 'application/javascript');
....
}
I'm not sure if this is what you're asking for, but here is a way to map ajax requests to laravel controller methods pretty easily, without having to mix up your scripts, which is usually not the best way to do things.
I use these kinds of calls to load views via ajax into a dashboard app.The code looks something like this.
AJAX REQUEST (using jquery, but anything you use to send ajax will work)
$.ajax({
//send post ajax request to laravel
type:'post',
//no need for a full URL. Also note that /ajax/ can be /anything/.
url: '/ajax/get-contact-form',
//let's send some data over too.
data: ajaxdata,
//our laravel view is going to come in as html
dataType:'html'
}).done(function(data){
//clear out any html where the form is going to appear, then append the new view.
$('.dashboard-right').empty().append(data);
});
LARAVEL ROUTES.PHP
Route::post('/ajax/get-contact-form', 'YourController#method_you_want');
CONTROLLER
public function method_you_want(){
if (Request::ajax())
{
$data = Input::get('ajaxdata');
return View::make('forms.contact')->with('data', $data);
}
I hope this helps you... This controller method just calls a view, but you can use the same method to access any controller function you might need.
This method returns no errors, and is generally much less risky than putting JS in your views, which are really meant more for page layouts and not any heavy scripting / calculation.
public function getWebServices() {
$content = View::make("_javascript.webService", $data);
return (new Response($content, 200))->header('Content-Type', "text/javascript");
}
return the above in a method of your controller
and write your javascript code in your webService view inside _javascript folder.
Instead of loading get datas via ajax, I create js blade with that specific data and base64_encode it, then in my js code, I decode and use it.

ZF2 Session not lasting on redirect

My site uses multiple languages and my users can click on flags to set their desired language. When that flag is clicked, a Session should store that information and then i want my controller to redirect the user to another page. This i do with the following code:
<?php
public function setLangAction () {
$oLanguageCookie = new Container('language');
$oLanguageCookie->lang = $this->params ('langvar');
$this->redirect()->toRoute('loadpage', array('page' => 'home'));
}
?>
However, when i print_r($_SESSION) in the indexAction (the action where loadpage routes to), $_SESSION is empty.
Can somebody help me?
Depending where you param comes from you should execute
$this->params()->fromQuery('langvar');
$this->params()->fromPost('langvar');
unless it is a route parameter then you can use either:
$this->params()->fromRoute('langvar');
$this->params('langvar');

JToolbar::save() redirection

I'm going through the Joomla 2.5 tutorial to build a custom component. Now I'm facing an issue on the redirection after using JToolbar::save() or JToolBarHelper::cancel for that matter. By default Joomla wants to redirect to the default layout (from the edit layout). However I don't want it to do that. I want it to redirect back to another view. In Joomla 1.5 I would have done this through adding the function into the controller - something like
function cancel()
{
//redirects user back to blog homepage with Cancellation Message
$msg = JText::_( 'COM_BLOG_POST_CANCELLED' );
$this->setRedirect( 'index.php?option=com_jjblog&view=jjblog', $msg );
}
Now that works beautifully for the cancel function, however for save this is a much more complex thing. If I want to overwrite the url do I have to redirect the controller to the model and then write in all the code for the model interaction? Because that seems slightly excessive just for a url redirection like you would in Joomla 1.5?
Hope you have added the save toolbar code with the proper controller name like this
JToolBarHelper::save('controllerName.save');
Create a save function in appropriate controller.
Add the task in the form
Finnally make sure you have added form action withthe corresponding component name.
You can try this-
In the controller firstly you call the parent save function than redirect to url.
function save(){
parent::save();
$this->setredirect('index.php?option=com_mycomponent');
}
OK it didn't need to $this->setRedirect at all. Just needed me to change the value to
protected $view_list = 'jjBlog';
which then sets the redirects of everything back to that list view.
Source link for this is here.
Thanks for all the responses though!!
view.html.php
protected function addToolbar ()
{
JRequest::setVar ('hidemainmenu', false);
JToolBarHelper::title (JText::_ ('Configuration'), 'configuration.gif');
JToolBarHelper::save($task = 'save', $alt = 'JTOOLBAR_SAVE');
}
controller.php
public function save()
{
$mainframe = JFactory::getApplication();
$mainframe->enqueueMessage (JText::_ ('COM_SOCIALLOGIN_SETTING_SAVED'));
$this->setRedirect (JRoute::_ ('index.php', false));
}
I think you can use
global $mainframe;
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
If you are overriding joomla's default save function in your custom component like
function save( $task = 'CustomSave', $alt = 'Save' ) // or even same name Save
Inside your controller you can use the CustomSave as the task and use $mainframe for redirect.
or
$mainframe = &JFactory::getApplication();
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
Hope this may help you..

Resources