CodeIgniter - sending variable from controller to a view which use template - codeigniter-2

I have the following problems.
I am using the following template for my application:
<?php $this->load->view('backOffice/bo_header_in');?>
<?php $this->load->view($bo_main_content);?>
<?php $this->load->view('backOffice/bo_footer_in');?>
When I send data from the controller, to edit a record, I have the following code:
$data['userid'] = $this->back_office_users->getOneUser($userid);
$data['bo_main_content'] = "backOffice/edituser_view";
$this->load->view('backOffice/bo_template_in', $data);
Now, in my view, where I want to edit the record, I am getting it all right, my form is filled with the user name and password but.... on the top of the page, I get displayed the userid, username, userpassword, which is not what I want. Can anyone help me how can I send the data from my controller to my view, so I can have only my form filled, and not have the userid,username and password diplayed on the top of the page?

What you have shown looks ok. Search your models/controllers for a var_dump and re-look at the code in your view for a print_r. I typically do this during development to see the variable contents.

Related

Laravel - Get HTML of current page or of a view according to a path

I have a view mypage.blade.php and a route.
The url is like : https://example.com/mypage/param1/param2. The route use param1 and param2 and generate the page.
Question 1
In that page, I try to get its HTML code. Is there a way to do it?. I tried render() but I don't get what I want.
Question 2
In the view, can I get the HTML code of an other view by specifying a path ?
You had the right idea. Not sure why it wouldn't work for you.
In the controller, set the view into a variable:
$view = view('myBaseView', compact('people', 'places', 'things'));
Now, if you dump the rendered view variable, you have the page's HTML:
dd($view->render());
To get the html of another view by specifying the path and using the internal controller, you would need to set up some kind of a wrapper or catch so that the view variable is not returned as a view, but rendered out to html as above. Your method would need to trap whatever the original controller was sending before it pushed out the view.
Of course, old school php can get the other page's rendered html too possibly if your server is set to allow this:
$html = file_get_contents('http://mypage.com/');
Something else you might find handy is the Laravel sections method. If you just want to render part of the page you can do so by calling whatever section you want from a partial view:
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
dd($sections['modalContent']); // this will only dump whats in the content section
I don't know what you want to do with this html, but if you wish to display it on a page, once you send it (you'd possibly want to return the view along with a compact of the variable $view... as a normal variable if so), remember to use this format:
{!! $view !!}
HTH

Laravel Redirect as POST

Currently my users must get the visit form given by Route::get then fill it in to get back a result view given by Route::post. I need to create a shareable link such as /account/search/vrm/{vrm} where {vrm} is the VRM that is usually filled in on the form page. This VRM then needs to redirected to Route::post as post data. This needs to be done by my controller. How can I do this in my controller?
Routes:
// Shows form view
Route::get('/account/search', 'User\AccountController#getSearch')->name('account.search');
// Shows result view
Route::post('/account/search', 'User\AccountController#runSearch');
// Redirect to /account/search as POST
Route::get('/account/search/vrm/{vrm}', function($vrm) { ???????? });
POSTs cannot be redirected.
Your best bet is to have them land on a page that contains a form with <input type="hidden"> fields and some JavaScript that immediately re-submits it to the desired destination.
You can redirect to a controller action or call the controller directly, see the answer here:
In summary, setting the request method in the controller, or calling a controller's action.
Ps: I don't want to repeat the same thing.
For those who comes later:
If you are using blade templating engine for the views, you can add '#csrf' blade directive after the form starting tag to prevent this. This is done by laravel to prevent cross site reference attacks. By adding this directive, you can get around this.
return redirect()->route('YOUR_ROUTE',['PARAM'=>'VARIABLE'])

Controller redirect back to a POST form

I have a table showing a list of names, with an "edit" button and hidden id value at the side.
Clicking the "edit" button will post the hidden id as a form value and display the edit page so the user can change details of that person. Pretty standard.
When editing the details and submitting I'm using a validator. If the validation fails it needs to go back to the edit page and display the errors. Problem is that the edit page required an Id value via POST method, but the redirect only seems to utilise the GET method which leads to a "Controller Method Not Found" error as there is no Get route set up.
Does anyone know how I can redirect back to the page via POST not GET. Currently my code looks like:
public function postEditsave(){
...
if ($validator->fails())
{
return Redirect::to('admin/baserate/edit')
->withErrors($validator)
->withInput();
}else{
...
thanks
You can use Redirect::back()->withInput();
You may wish to redirect the user to their previous location, for example, after a form submission. You can do so by using the back method
See: http://laravel.com/docs/5.0/responses
You don't need to use POST to go to the edit page. You can use GET and a parameter to the route, check this out: http://laravel.com/docs/routing#route-parameters
You'd have a GET route to show the edit page, and a POST route to handle the request when the user submits the form.
It'll look like this (notice the parameters):
public function getEdit($id)
{
return View::make(....);
}
public function postEdit($id)
{
...
return Redirect::back()->withErrors($validator)->withInput();
}
if "redirect with POST" is exist, then I don't know it. I recomend you to just use flash data
Redirect::to('user/login')->with('id', 'something');
You can use Redirect::to("dashboard/user/$id")->withErrors($validator)->withInput();. You should use double quote to pass parameter if there is any errors with validation.

Send back value from controller when submit form codeigniter

here is a question that I am not able to find a way to do it. At the home view, I have a php section that will echo out if users input is just signed in when they submit form. But when I am doing this, it will give out a undefined error, because that controller is not yet executed.
This is my home view.
<?php include('header.php'); ?>
<?php echo $sign_in_results;?>
<?php include('forms/forms.php'); ?>
<?php include('footer.php'); ?>
This is my sign in controller, will get execute when submit pressed, the modal will return true or false.
function form_sign_in_controller(){
$this->load->model("form_sign_in");
$database_insert_results = $this->form_sign_in->check_user_detail_with_db();
$data['sign_in_results']= $database_insert_results;
$template = $this->load->View('main_view', $data);
}
I tried to use if(isset()).. The error is gone, but that section does not echo out anything. Any idea? Thanks
validate the data that comes from your sign in form. Never pass data from a form directly into a database query.
validating the data will tell you if the form has correct values. if it does not, then show the form again.
form data is validated - query your database
there are 3 possible results - you got a match OR you did not get a match OR there was an error. Check for these 3 different conditions, and then show the appropriate view.

CodeIgniter Message System

I'm finding for a message passing system for codeIgniter. I found 3 ways.
1) set_flashdata(); from session class,
2) form_validaion from form validation class and
3) a variable set in controller and show it in view file.
1) is good, but we can only use for next server request. we can't use it for calling views.
2) can only use for form validation and it doesn't disappear on page refreshs.
3) is also doesn't disappear on page refreshs and we had to set it manually for form validation errors.
my want is like set_flashdata, show only one time and disappear on page refresh, doesn't need to set error message manually for form validations and able to use together with calling views.
It is not so bad if we use all three ways together, but I hope for better way.
Is there any ways or something for it?
Thanks!
Are you saying that you want to directly display flashdata in the view? Normally you just pass the flashdata to the view via the controller. I wouldn't recommend any other way.
Controller:
$data['var'] = $this->session->flashdata('var');
$this->load->view('view_file', $data);
View:
This is the flashdata: <?php echo $var; ?>

Resources