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.
Related
When I add things to the Session with Session::put() in my controller action, then that data is not available in my view, with Session::get() when doing AJAX request.
The same problem goes for the Former package, which I use for nice form building. It relies on passing some info via the Session, which is used to mark fields as valid/invalid. This functionality is also not working when using AJAX.
I set a view like always, in my View:
$this->layout->content = View::make('account.login')
For AJAX requests, I do NOT render the normal way with layout, but instead get the specific "content" section of the template and return it:
$this->layout->content->renderSections()['content']
When I do a "normal" request, then Session data works fine.
When I do an AJAX request, then Session data set in the controller DURING the AJAX call is ignored. Any Session data set BEFORE the AJAX call is available.
I'm wondering if Laravel has some issue with Session under AJAX calls, or with the the "renderSection()" method above?
I have checked all the obvious problems:
AJAX request uses the same session ID as non-AJAX request.
GET/POST verbs are used correctly etc.
Replicate:
In CONTROLLER action: Session:put('foo','bar');
In VIEW file (in the content part): Session:put('foo2','bar2');
In VIEW file (in the content part): var_dump(Session::get('foo','bar')); // Returns 'bar' in non-AJAX calls, but returns nothing for AJAX calls (!!!)
In VIEW file (in the content part): var_dump(Session::get('foo2')); // Returns 'bar2' in both AJAX and non-AJAX calls as expected.
It seems like the Session values set in the controller action ARE LOST when it renders the view. Therefore my question if this is 1) an AJAX vs. SESSION issue in Laravel, or 2) an Session vs. renderElement() problem that I am not aware of?
I had the same problem and just found a potential solution:
I found a similar problem relating to laravel 3. For the session to persist in an ajax call you need to return the response correctly.
return json_encode($response);
This is causing the problem. It's not it appears a valid response to enable the session to persist. Change it to:
return Response::json($response);
This enables the session to persist!
For some reason a normal form submit or call to the method allows the first one but ajax does not.
I've seen references elsewhere about echo statements in the method affecting the session - the return I suppose must behaving similar to an echo
This is the post that triggered the solution:
http://forumsarchive.laravel.io/viewtopic.php?id=1304
I am trying to use php's use_trans_sid, so I will have phpsessid in all urls.
But, when I set use_trans_sid to 1, AJAX call did not get result properly.
Somehow the result truncated.
When I set use_trans_sid back to 0, AJAX call get result properly again.
What would be the problem?
I am using the Yii framework.
Check ajax url requests when use_trans_sid=1, if they haven't phpsessid var than you need manually add it to request url as GET parameter.
use_trans_sid=1 rewrites only page url, not js and ajax. When it using, PHP try to find session id in get parameter, if not found - created new session.
When you using use_trans_sid=0 ajax works, because PHP work over cookie session id
I have set php's use_trans_sid to 1.
With that, PHP will insert code to propagate session id.
In my case, PHP inserted a hidden variable in a form which has been encoded into json object.
As the result, ajax call get it as a request error (it did not get a json object).
I confirmed this by replacing the form with string like 'Hello'.
With that, PHP did not insert code to propagate session id. And ajax call get it as a good json object.
Now the problem has changed into 'how to make such interruption from PHP will not interfere ajax call?'
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
I have a controller where i have a method called index.
In this method i am retrieveing data from database and using a paging library setting variables for view for paging. When i add a new record i am hitting another function using ajax. There after insertion i call
$this->index();
Now in index i am checking a condition
if($this->input->is_ajax_request()){
//dont load header and footer
}
but the problem is that when i come to index from my save function it looses the ajax request and my condition in index function in not checked and header and footer is always loaded. I want the ajax request still be available even if i jump from one method of codeigniter to another? Any suggestion? Or alter native.
Because i dont want to create another function where create the paging again with header and footer ommited.
Something that might be useful is CodeIgniter's session class which has a flashdata method. Flashdata is a bit of session data that is only stored for the next server request, then it is deleted.
In your save function, you could have this at the end:
$this->session->set_flashdata('ajax', true);
and as part of the condition in your index function, you could have:
if($this->input->is_ajax_request() || $this->session->flashdata('item')){
//dont load header and footer
}
This would then check that the request was actually an ajax request OR that a session variable has been temporarily set to tell CodeIgniter that it should be treated like an ajax request.
I have a register page with the usual email,name,password ..which is validated in the server's submitted route/page. if it fails then I redirect back but I want to fill the values back in the register page..I can put the register form parameters in the session but it will stay there...is there a page memory(a smaller scope than session) just like session which will be just for the next page and then gone/ which is the best way to implement this.
Thanks
Why don't you just render the registration page from the POST route like this:
post '/register' do
#registration_data = params[:stuff] # store all your registration data
if info_validates # everything validates
redirect './user_home'
else # something fails validation
haml :register # or erb or whatever your template engine is
end
end
Then in your view, have it fill in #registration_data if it exists.
Also, you can clear session data with session.clear.
Ajax validation would be much easier. You just register an onclick event to your form submit button that makes a call to a page that returns a json status code with the error information or 200 for OK. If 200, then submit.