sending data from controller to view not working - codeigniter

I want to send data from controller to view but it gives error that variable is not defined. can any one tell me what is the problem?
here is my code in controller.
$data['num']=1;
$data['message']="Invalid email address or password.";
$this->load->view('admin/Login',$data);
this is view
<?php echo $message;?>

Related

Using route() and with() in html view in Laravel 5.6

For redirection, in controllers, we can write like this:-
Redirect::Route('front_index')->with('RegError', 'An error occured while adding the user.');
How can I set up the href of an anchor tag so that I can send the 'RegError' too? Say something like this?
<a href="{{route('front_index')->with('RegError', '')}}">
actually you can't because with function is used to pass parameters from controllers to view and route helper doesn't have this method.
You need to make request and in controller pass your RegError

Data lost on refresh in Laravel

I am redirecting a page like this:
return redirect('vendor/vendors')->with('vendor', $allvendors);
and getting it from Blade template like this:
#foreach($vendor as $vendors)
but it's not working or at times when it loads, and I refresh the page, it will give error like the data has been deleted:
Undefined variable: vendor (View: /opt/lampp/htdocs/easyhire-web/resources/views/vendor/vendors.blade.php)
The with method on the redirect response is meant for passing flashed session data. That data does not persist, so if you code your view to expect that data to be present on every request it will fail on normal page loads.
Any data that you need should be retrieved in the controller action that corresponds to the URL you are redirecting to.

when i call a function in a controller, it show me 2 time the controller which stop my task

Hello every body I have a problem when i call to a certain function in controller to update a record, it duplicate the my controller in URL which not let me work fine.Help please thanks in advance..
it show URL like this:
localhost/codetest/practice_controller/practice_controller/practice_update/1
In above practice_controller is coming two time.
Two possibilities.. either you have a redirect code in your controller which is redirecting in a false way or in the form action you have not written complete URL starting with base_url().
I would suggest to write complete url in form action like
<?php echo base_url()?>practice_controller/practice_update/<?php echo $update_id?>

post data via redirect function in codeigniter

Is there ever a way or some tricks to post an array of data or a single variable string data using redirect() function in codeiginter?
when using redirect you go from one controller or another by this process all post data are destroyed unless you stored them on a session, here is how i do it
$data = array('firstname'=>'fname','lastname'=>'lastname');
// i store data to flashdata
$this->session->set_flashdata('lolwut',$data);
// after storing i redirect it to the controller
redirect('controller/method')
so on your redirected controller you can access it via $this->session->flashdata('lolwut')
note that i am using flashdata not userdata, flashdata destroys itself on the next process.
read more flashdata here SESSION CLASS
In the first place why you need post data while redirect :
you can have post function that handle all your code and then redirect after success or failure depends on your usage
function method()
{
//do something
redirect('path/to/method');
}
if you want to have variables passed through other pages you can do this by :
Save data into session, $this->session->set_data($data); or $this->set_flashdata($data); depends on your usage
Pass in URL as parameter instead of form submission
hope that helped you someway

Use CodeIgniter form validation in a view

I have footer view that's included on all my pages which contains a form. I would like to be able to make use of CI's form validation library to validate the form. Is that possible?
Currently the form posts back to the current page using the PHP_SELF environment variable. I don't want to get it to post to a controller because when validation fails it loads the controller name in the address bar, which is not the desired behaviour.
Any suggestions gratefully received.
Thanks,
Gaz
One way, whilst far from ideal, would be to create a "contact" function in every controller. This could be in the form of a library/helper.
CI doesn't natively let you call one controller from another, although I believe there are extensions that enable this.
Another option would be an AJAX call instead, which would allow you to post to a generic controller, validate etc whilst remaining on the current page.
In this use case, I would definitely go for an AJAX call to a generic controller. This allows you to show errors even before submitting in the origin page.
Another way (slightly more complex), involves posting your form data to a generic controller method, passing it a hidden input containing the current URL.
The generic controller method handling your form can then redirect to the page on which the user submitted the form, passing it the validation errors or a success message using flash session variables: $this->session->set_flashdata('errors',validation_errors()) might do the trick (untested)
The good thing about this is that you can use the generic form-handling method for both the ajax case (suppressing the redirect) and the non-ajax case
AJAX would be best, just like everyone else says.
I would redirect the form to one function in one controller, you could make a controller just for the form itself. Then have a hidden value with the return URL. As far as errors go you could send them back with flashdata.
Just remember to never copy paste code, it a bad practice and guarantees bugs.
//make sure you load the proper model
if ($this->form_validation->run() == FALSE){
// invalid
$redirect = $this->input->post('url');
$this->session->set_flashdata('errors',validation_errors());
redirect($redirect);
} else {
/*
success, do what you want here
*/
redirect('send them where ever');
}

Resources