I am loading data from an API through JS send the data to the Laravel Controller and save them into the Database. After loading all the Ajax I want to display the data in a subview/section of the master.blade - Is it possible to render a View dynamically after the page finish loading, - also for later I want to update database rows and display the new data in the view dynamically.
//afater Ajax loading - update / display the data in the view
public function loadcomments() {
$comments = Comment::all();
$childcomment = Childcomment::all();
return View::make('partial')
->with(compact('comments'))
->with(compact('childcomments'));
}
in user.blade.php (main site) I am defining
#section('comments')
#include('partial')
#stop
and in the master.blade.php I am defining the yields:
#yield('content')
#yield('comments')
any idea how to render the site with the updated content?
Once the page has finished loading, with out making further AJAX calls to the Laravel app its self it will have no further part in the request.
You could just update the front end markup with JS or you can make a call back to your Laravel application using AJAX/jQuery to pull the data back out after it has added it to the database.
Use a resource controller or similar implementation to allow insertion and reading of the comments (CRUD) so you can pull data when you need using AJAX.
Edit
There are different ways to make a page dynamic on the front end all of these will usually include Javascript or another front end scripting language.
In the past I have used jQuery to handle updates of the page content using either JSON or XML/HTML but lately I have started to use AngularJS.
Another library is EmberJS which I am currently learning to use but I feel front end languages are out of scope for the question.
There are many tutorials on updating the HTML after a page has loaded by making a call back to a controller or other resourceful route.
Say the posts have been saved to the database, if this is done AFTER the view has been returned to the browser you WILL have to use javascript to pull out the data and most likely have a piece of js code to tick over that "polls" your resource controller for new comments.
If a new comment has been detected a second request is made to pull the comments out OR the comments are returned from the polling requests using AJAX.
On the laravel side, a partial view could be returned or JSON, for simplicity we'll replace the view. You would make a jQuery selector for the current existing partial on the browser and then replace it with the one pulled from Laravel with AJAX.
$('#some-page .my-view-to-update').html(somedata);
My jQuery is VERY rusty so read up on the relevant documentation to update the HTML correctly.
You can also use Pusher
take a look at this
https://pusher-community.github.io/real-time-laravel/
Related
The situation is like this:
when I first load the page, it displays the data which is powered by a yii widget using e.g
$this->widget(blahblabhablha)
Now, inside that widget, there's clickable dropdown menu, whereby when I click any of the options, I fire an ajax call. Then the backend php script will query the data using the module/controller/action url that I used in the url parameter of $.ajax(). Then I echo json_encode() the data that I fetch from db so that the ajax will get a response.
How to update the widget I just mentioned, using the data response of ajax?, because It's a widget that displays rows and also has pagination, how am I suppose to update that widget to display the data from the ajax response?
When refreshing grid view for example, Yii actually renders the whole page behind the scenes and then extracts part with gridview div and replaces it. If you are using CgridView, it would be enough for you to set 'ajaxUrl' option when configuring the widget. If not you will have to create html from your ajax response and then place it in the appropriate container via js. Another way, and probably better way is for you to create partial view with only your widget content and then to use renderPartial() in your action to refresh it.
I have an application where I use ajax to update the comments & Like count.
I am using Codeigniter page caching http://www.codeigniter.com/user_guide/general/caching.html
and set following code to recreate caching every 60 minutes
$this->output->cache(60);
Problem is when somebody enter a new comment, DB operation is happening (Because of Ajax call), But the Newly inserted comment disappears after the page refresh, because of the cached HTML page. How to handle caching and also dynamic content to change?
The main purpose of caching is to save server resources on page load (i.e. so the server doesn't need to fetch all the dynamic data every time its loaded).
It sounds like you are using the AJAX function to both submit the data but also modify the webpage on the client side simultaneously. When the user then refreshes the page, this will fetch the content back from the server, in this case the original cached content that was generated before the comment was made and is therefore operating exactly as it should.
If you have a true requirement for caching this page, you could consider deleting the cache, which would force it to be rebuilt on the next page load whenever a comment is made. Placing the following line of code in your controller (in the function that received the AJAX data) should do it:
$this->output->delete_cache('foo/bar');
I found the solution.
To delete a cache file you need to use the following function
$this->output->delete_cache('CONTROLLER/FUNCTION');
Note: No Slash before or after.
In my Case i was using custom routes in codeigniter. When user type example.com it is routed to example.com/CONTROLLER/FUNCTIONNAME and that too default landing page.
So i created a new function. Please refer here
I'm working on a Yii application in which in some part of it I need to use a CGridView to show some database records.My aim is to update it's result by an ajax call(based on some filters on table columns) but the problem is in each call CGridView establishes a new get request for getting it's related jquery files and this causes increasing my website loading time.
Is there any way to tell CgridView to get these files only one time in the first ajax request?
how can I stop it from getting these files several times?
Use scriptMap to prevent loading of jquery files in your ajax call.
Yii::app()->clientScript->scriptMap=array(
'jquery.js'=>false,
'jquery.ba-bbq.js'=>false,
'jquery.yiigridview.js'=>false
);
You can get or load js file externally,
you can found your answer here.
Well, I've read this tutorial if I could say: http://www.symfony-project.org/book/1_1/02-Exploring-Symfony-s-Code
And, actually, I write my code very similiary. But my question, where should I insert my jQuery code? I am sure it should be in the part of the View, but are there any good examples on how should I combine it to make "live" websites?
Edit: By saying live, I mean, for example, send POST request through Ajax and get information and similar. So, no refreshes for people.
Thank you.
jQuery as a part of javascript resources should be included in html.head or in-place, depending on what should jquery do and if the code is reusable for more views.
So it has to be part of View and you're choice is to set it up in layout or action view itself
If you need the javascript on every page then add it to your master view if not then just include it to the particular view files.
In context to codeigniter:
I extend the controller with MY_Controller and initialize a property as array which would hold the scripts that are added dynamically to the view.
eg.
var $templateData['scripts'] = array();
The controllers then pass $this->templateData to the views
And the views load the extra scripts( available as $scripts) as directed by the controllers in the head tag
This way you can even avoid loading the main jquery file when not needed.
Loading jquery.js only for the controller that need it.
Context
I'm working on a project that I'd like to make more dynamic with PHP + AJAX.
I'm using jQuery for the AJAX part because I totally suck in Javascript, and anyway it seems to be worth something.
I reached a point where my application needs to use a form to send a post, and I want to use an ajax call to perform this action. In the page I'd like to send the post, there is also a list of the most recent 15 posts submitted.
First question: Should I just forget about it and use just PHP?
The action
The user writes something in the <textarea></textarea> and clicks on a <a id="myPostSubmit">Submit</a> that is the handler that I'll manage on the jQuery script with something like $("#myPostSubmit").live('click', function() { /* here i make the ajax call */ });. If the post is successfully submitted we are going to do something (I'll talk about it in the next section), either we will alert the user using my showAlert(text) function, that shows a black box for 4 seconds with the text in it.
Second question: Should I manage the click event in any other ways? Should I create a function, such as sendpost(post) and attach it into the HTML onclick="" event?
If the post is successfully sent
I'd open a discussion about 2 options:
We refresh the page [not actually
loading the entire page but making
another ajax call that retrieves the
posts lists and makes disappear the
old one, load the PHP file to
retrieve the new posts (including
the one we just sent), and then make
the post list appear]. Pro: 1) We are sure that what the user is reading after the post list is loaded is the real post sent. So it actually double checks the action. 2) We load also some possible posts sent in the mean while. Cons: 1) We have to create a PHP file that gets the post list template, slicing the template of that page in 2 files. 2) It doesn't really seems that smooth to me.
We just use Javascript to get the post template, add it to the list. Pro: 1) We make it really smooth, without reloading the entire page. 2) We don't need of any PHP file to reload the page. We just use Javascript (jQuery). Cons: 1) How do we get the post html template to add it to the list? 2) How do we get the user (logged) informations without PHP?
Third question: Is it better the 1st or the 2nd solution? Would you provide a even better 3rd solution?
The PHP page
The PHP page that will receive this AJAX call is : ?p=action&a=sendpost. The page require $_POST['myMessage'] to be set and not empty and nothing else. The page itself will get all the user infos from the cookies and will manage to perform the needed query.
The application
It is divided in 3 parts: App, Template, Library. Basically each page of the application has its own .app.php and .tpl.php file.
The .app.php file manages the building
of the basis of the page, using classes
and other stuff from the library. In
our case it retrieves datas from the
database and put them into
variable.
The Template is called at the end of the .app.php file. The .app.php file send to the template the retrieved data and the .tpl.php file outputs them.
The library is used to contain the classes and functions we need in the application file.
Fourth question: Is this a good way of manage my web application?
Edit: What about returning the alert message to the user?
I read about an option, inside $.ajax() that will manage the response on success or in error. The documentation about it is very simple and I didn't get it.
Fifth question: How should I return (from the PHP file) the error
or the success?
First question: Should i just forget about it and use just PHP?
Well, you application will relay on JavaScript if you use ajax, this days i think it just fine ;)
Second question: Should i manage the click event in any other ways? Should i create a function, such as sendpost(post) and attach it into the HTML onclick="" event?
Create a function and bind onclick. Code will be more readable ;)
Third question: Is it better the 1st or the 2nd solution? Would you provide a even better 3rd solution?
My solution: ajax submit the form and on callback insert new comment in to the list or display error message if user can't comment.
Check jQuery serilize() for submitting forms data with ajax.
Fourth question: Is this a good way of manage my web application?
It's just fine ;) When you application get's bigger you will have to redesign it, but don't do it know, do it when current solution becomes to hard to work with.
Read some good book on building MVC framework. And on programming patterns in general.
You seem to be on the right track with everything. There are lot of opinions called "best practices" about how to exactly attach event handlers, how to reload the data on the page and how to organize your application, etc, but I personally would rather build more software instead of worrying about details like that. The details will come to you eventually.
I personally find that updating whole chunks of server-side-rendered HTML on the page is more robust solution, but I have seen people getting excellent results with templates.