Passing parameter to same page with CodeIgniter - codeigniter

I have a controller that loads a page view. The page is actually a template of sorts; when a user clicks a button, content is loaded into a div based on the parameters passed through the button link. All button URLs go to the same page, it just passes a parameter. So for example, I have a page controller and a page_view file. When I click on the login button I want to go to the same page, but just pass action="login". If I clicked on "see stats", then I would want to pass action="stats".
Without Codeigniter I know I can just set the URLs on the buttons to "?action=stats" or "thisurl?action=stats", and within the page itself I have code that checks to see what the value of action is, and then generated content based on value.
How can I do this with the CodeIgniter framework?

You can call methods on a controller. So if you call a page like /your_controller/login, then it will call the login method in the controller and you can change the view to match that:
class Main extends CI_Controller {
function Main(){
parent::__construct();
// called when /main is accessed
// load your main view here
}
function login(){
// called when /main/login is accessed
// tweak view to match login view
}
}
More info here: http://codeigniter.com/user_guide/general/urls.html

Related

laravel-livewire return view on button click

I have one button in livewire view
<x-jet-button wire:click.prevent="confirmProcess" class="bg-blue-500 hover:bg-blue-700">
Next
</x-jet-button>
and here in controller my method
public function confirmProcess()
{
return view('livewire.confirm');
}
I also have view in livewire folder with name confirm.blade.php
I want to load different view on button click but when I click on button nothing happens.
how can I load other view also I want to use data from same controller also on other view.
Thanks
You can't return view from your confirmProcess method. What you can do is redirect from confirmProcess method to a named route like:
return redirect()->route('confirm');
Then from this route you can call the component like:
Route::get('/confirm-process', \App\Http\Livewire\Confirm::class)->name('confirm');

CodeIgniter Dry Navigation

My boss told me make dry navigation dont use repetitive code, for navigation i am trying to extend CI_Controler and with construct load header nav, body, footer files.
My question is when i create new controller and when i try to load different view files, how to achive that???
my extended controler
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_home');
$this->load->view('view_footer');
}
}
and later when i am creating new controler how to load diffrent view files
class Pages extends MY_Controller
{
public function __construct() {
$this->load->view('includes/nav_new_view');
$
}
}
You can create a template library yourself. For example :
function template($view_name,$view_data){
//below will return html string from view name
$data['content'] = $this->load->view($view_name,$view_data,true)
//load main template view and pass the html string to main template
$this->load->view('main_template',$data);
}
In main template, just echo $content
If I understand your question, you're trying to achieve a template situation. For this, the best way is to actually call your templates view files within a primary page view. What I mean is your controller function (not the constructor, an actual class function representing a page) should call a primary view such as
$this->load->view('page1', $this->data);
and within that file, you call
$this->load->view('nav', $this->data);
then your content and then
$this->load->view('footer', $this->data);
You would then repeat the process for page 2 where in your controller's page2 function, you would call
$this->load->view('page2', $this->data);
and your page2 view file is almost identical to page1 except you use your page 2 content in that area.
You could even use a single template view file and pass it a $content variable (which obviously changes per page) and call
$this->load->view('template', $this->data);

How do I auto fill field values in a section of a form that is loaded via ajax in Laravel 4?

I have a section of a form that dynamically loads different sets of fields based on the user's selection in a control. I'm using a javascript event handler to detect when the selection changes, and using AJAX (with HTML payload) to pull in the proper set of fields.
I would like to be able to use Laravel's Form::getValueAttribute() method to automatically fill in the form fields' values in both the static and dynamic form parts. However, the partial view that is loaded by my AJAX call does not have the same instance of the Form class as the view with my main Form, so I can't simply call getValueAttribute() in the partial.
My thought is to make the AJAX call a POST, and serialize the necessary data (a subset of Input::old() or the model data depending whether the page is loaded as the result of validation errors, or an UPDATE request) to send along with the POST so that the HTML fragment I get back has the values set properly.
Is this the best way to get what I want? If so, does Laravel have any tools to help with the serialization of form data? If not, what might be a better approach?
I've found an approach I like better. When the view is loaded normally I use AJAX as usual to load the partial. But when the view is loaded for a validation post-back or for editing, I use Laravel's Views' nest method to nest the partial view containing the proper fields directly into the response. The partial then has access to all the Input and error data I need. The user is still able to change the field set as usual but I put up a confirm prompt for them if they have already set some values in a field set they previously selected. If they decide to proceed anyway, the field set is cleared and a new field set is brought in via AJAX as usual.
My code looks something like this:
Controller:
public function newThing() {
if ( Request::session()->has('errors') ) {
// this is a validation post-back
return View::make('thing')
->nest('fields', 'fields_partial');
} else {
// just a normal unfilled form
return View::make('thing');
}
}
public function editThing() {
return View::make('thing')
->nest('fields', 'fields_partial');
}
View: thing.blade.php (just a snip of it)
...
<form>
...
<select id="picker">...</select>
<div class="sub-fields">
{{ isset($fields) ? $fields : '' }}
</div>
...
</form>
...
<script>
$('#picker').change(function() {
// if any .sub-fields inputs have been changed, get confirmation from the user
// if user confirms, do ajax stuff to replace .sub-fields contents with new field set
// otherwise cancel the change
});
</script>

how to use last insert id when redirect same form page in codeigniter

I just try to prevent re submission problem when refresh form page. so I use session flashdata() method and redirect same form page. but I want also display recent inputed data on form VIEW page. then I am try $this->db->insert_id() on my form VIEW page . but it always show 0. how can I solve it?
when you redirect the user after the successful form submission then add the parameter in the url just a exapmle with dummy code to make an idea for you
$insertedid=$this->my_model->add_info();
//your add_info() should return the inserted id
redirect("/myformcontroller/myformpage/".$this->db->insert_id());// add id in redirect url
on your myformcontroller controller's myformpage function
function myformpage() {
$insertedid=$this->url->segment(3);
if(!empty($insertedid)){
//get new added information and pass it to view
}
// your other code
}
hope it makes sense

Redirect the entire page from MVC3 Razor iFrame page to a different URL

I have an razor page https://myDomain1.com/myFrame.cshtml with a "continue" button on it
this razor page is an iframe inside another parent.cshtml
When I click on the "Continue" button I want to redirect my entire page(including parent page) to https://myDomain2.com/default .
Given below is the ActionResult in my Controller
[HttpPost]
public ActionResult myFrame(TestViewModel model)
{
Response.Redirect("https://myDomain2.com/default");
return View(model);
}
with my above code only the iframe part of the page is redirecting but my requirement is to redirect the entire page to https://myDomain2.com/default
My problem here is that ,I want to redirect my entire page(including parent page) to https://myDomain2.com/default .
Please help me how to redirect the entire page to a different domain URL
In the case that you must use this solution (sticking with frames), simply include the javascript content result - something along these lines:
[HttpPost]
public ActionResult myFrame(TestViewModel model)
{
return Content("<html><script>window.top.location.href = "http://www.whatever.com"; </script></html>");
}
Your frames must be in the same domain though for this to work.
HTTP does not allow you to specify the target frame in the response.
Instead, set target="_top" in the original <form>, or put a Javascript frame-buster in the target page.

Resources