Javascript Reload? |
[eluser]Corey Freeman[/eluser]
I'm trying to make a social-media like voting system. I dunno if this is the totally wrong place to ask, but how would I go about having the button auto-reload and display the new amount of votes + disable the voting functionality? I don't know if using a model to just check that the vote is in a pivot table and then reloading could be done in a frame, per say... Sorry if this is way off-topic and confusing.
[eluser]treeface[/eluser]
The question that pertains to CI here is how you should handle AJAX requests with CodeIgniter. It effectively works just like a regular request. Run an asynchronous request in javascript to your server to the controller that you wish with a JSON string, decode it in your controller, do what you need to do with your model, load whatever you need to load from your views as strings (see params for the load view function), and pass back whatever you need to pass back to javascript in a new encoded JSON string (or whatever communication method you want to use). Beyond this is outside the scope of CodeIgniter and might better be asked on a site like stackoverflow with tags for javascript, php, and codeigniter. It would also help if you were more descriptive about exactly what you want to do. For now I'm left wondering what you mean by "auto-reload".
[eluser]Corey Freeman[/eluser]
Sorry. Like when you like something on facebook? It changes from "like" to "xxx people like this". That's the effect I'm trying to achieve.
[eluser]treeface[/eluser]
No worries..the most difficult part of learning something new is finding the right questions to ask. The trouble here is that you're asking to solve a simple problem in a complex frame of reference. If you know Javascript, PHP, and HTML this is not hard. OK so basically here are the issues at hand: 1. You will need an html element to click (the "like" button in your example). 2. If you don't understand javascript, you need to. If you're learning javascript for the first time, learn some basics about the language (it's not too hard) and then *use jQuery*. jQuery, like CodeIgniter in PHP, is a sort of "framework" for Javascript that lets you easily do pretty much everything you will need to do. This will, for the most part, solve any cross-browser JS issues and it will keep your code clean, compact, and readable. 3. This button will need to have an event listener attached to it. An event listener is just a function that is run when an event fires. So in Javascript you would have something like this: Code: $(document).ready(function() { 4. So now you're sending it off to PHP...in CodeIgniter, inside the controller method that is run on the asynchronous request, get the id, send it off to your model where you will have a "likeItem" function, get info back from the model, die() back the results to javascript, and manipulate your myLikeButton how you please. So yeh...this is really a multi-topic question. If all of this is foreign to you, you need to look up tutorials first on how to use javascript, then on how to use jQuery, then on how to use CodeIgniter (if you don't know how), then on how to use AJAX with CodeIgniter (really these pieces are separate...you could take a javascript front-end and plug it into any backend just the same...only difference is in your URLs). Here's one I just found on Google: http://www.mrforbes.com/blog/2009/01/a-q...-tutorial/
[eluser]Corey Freeman[/eluser]
Thanks for the comprehensive tutorial. I guess I'd say I have beginner-moderate knowledge of Code Igniter at this point, moderate PHP knowledge, Expert HTML knowledge, and beginner jquery knowledge. I'll have to look into the steps you've suggested. Thanks ^_^ I may have to hire someone to enhance this for me later, haha.
[eluser]treeface[/eluser]
You're welcome, Corey. Just spend the next day or two diving into jQuery (browse their website...they have great docs and examples) and you'll get a hang of it in no time. Since you know HTML, I think you'll be amazed at what it can do w/regard to HTML manipulation. Then do the same for CodeIgniter. If you're a real beginner, have a look at my (totally awful) tutorial on CodeIgniter here (at the time I wasn't as good as I am now, but that might be an advantage for you): http://vimeo.com/8785711 Really what you're trying to do is just a simple asynchronous post request sent to PHP so CI can handle it, store the "like", and send info back to JS that it was successful (or any other info you wanna send back). Conceptually, it's not difficult at all, it's just a matter of understanding the moving parts. Good luck!
[eluser]Corey Freeman[/eluser]
I found this tutorial: http://ad1987.blogspot.com/2009/02/reddi...l-and.html and tried adapting it to codeigniter. Of course, I got nothing... VIEW Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" Votes Controller Code: <?php From what I understand, the ajax runs the PHP stuff in the browser, then the javascript makes it look all pretty. I assume my problem lies with getting the ajax to run the controller? I adapted it from the tutorial to have only a vote-up function...so I may have messed up there too... help? ![]() |
Welcome Guest, Not a member yet? Register Sign In |