How to create a voting system with AJAX? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery voting system
I've been using this tutorial to create a voting system.
The problem is that on the tutorial the votes update because he can just echo out the votes into the same place if that makes sense. But what if I had an image triggering the vote and I wanted to then update the votes which were somewhere else on the page?
For example, his "button" to vote up holds the amount of votes up, but I want to hold that data somewhere else but don't know how to auto update it once the vote has been completed.
Also, my image always disappears when I click it, how can I stop that?
I would post the code, but it's basically the same as the tutorial only changed for my own needs.

Say you want to update the results in a new <div id="new-results">.
Using the same code inside the
$(".vote").click(function(){ /* his code here */ });
you'll want to replace
parent.html(html);
with
$("#new-results").html(html);
in both places where the parent is referred to.

Related

Save client-side changes to server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a very simple site (PHP) that allows user changes (via jQuery) that modify elements' classes, and add/delete rows from a table.
I want to write these changes to the actual PHP file in question on the server every time a user makes a change, so that when the page is refreshed or revisited, the changes remain intact.
What's the simplest / best way to accomplish this? Thank you!
For addition/deletion of rows, fire ajax call and update records associated with the user. This is in addition to client-side (jQuery/JavaScript) changes to page. Whenever they revisit/refresh the page, data will be fetched from db, and as such will always be latest.
When you say that users modify elements' classes, I assume you mean their page's looks and styling (like some sites have on-page options to change background color, font-sizes). If that is the case, I suggest you:
Create a list of all possible states and store them in a new table in db, say display_options. You could store details like option_type (e.g. background color, font-size etc) and option_back_value (e.g. #FFF, #000 etc).
Create a new table say user_display_options where you store things like user_id, option_type and their chosen option_id.
While loading page, do a join on these tables using user_id. Then while creating your page, conditionally add classes etc to the page.
When user edits page, fire ajax call sending required data like user_id, option_type, option_id. As mentioned for other task, these changes to db will be in addition to client-side changes.
Also read #David 's comment to your question - it clears an important concept.

Displaying questions in a questionnaire using PHP?

I am building an online questionnaire. In this questionnaire, the user should see only one question at a time per page. When he sees the question, he will provide an answer and after that he will press on next to go to the second question and so on until the end of the questionnaire. I have already built the questionnaire but I am stuck now on the part" how i can display only one question from the database?". Also, when the user press on next, should i save the answer immediately to the database or I should wait until he completes answering all the questions and save them at the end to the database?
If anyone can help in that, please provide me with the appropriate technique to do that in PHP.
Use a counter variable that matches all the question ids (hope you have unique ids for each of your question).
And then loop through that variable and for each counter value fetch the data from the database using where id = ? clause where ? will be replaced by the counter variable value.

Simplest way to update website content without editing the page [duplicate]

This question already has answers here:
How do I make a div element editable (like a textarea when I click it)?
(8 answers)
Closed 8 years ago.
I've built a website for a friend and because he is not very techy and will be updating it relatively often.
My question is is there a way I can update the site using say, a PHP form and that will write a new div into the page?
So in standard english it would be the following:
Enter title
Enter date
Enter poster
Enter content
Submit
Can I do that simply in PHP? I'm open to other suggestions as well.
Thanks a lot!
Liam
I highly suggest you "Inplace Editing"
http://www.appelsiini.net/projects/jeditable
http://code.google.com/p/jquery-in-place-editor/
https://stackoverflow.com/questions/708801/whats-the-best-edit-in-place-plugin-for-jquery
The user has only to point and click on elements which he want to change. Plain simple... It's on you to implement an basic authentication system so that only the Owner of the Page is allowed to edit and save Data.
All in all it's just the editing of a file. You can write a php script that takes that input from the user and basically rewrites the html page accordingly.

Re-ask: How do I use php so that when a user presses a specific button, they can write to a specific .txt file? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I use php so that when a user presses a specific button, they can write to a specific .txt file?
First of all, am I able to re-ask a question? I got two answers, yes, but I tried both and they didn't work. I'm worried that people will forget about my question and I might not ever get the answer to my question and patch up my code. Anyway, here it is. If, however, re-asking questions is against some rule I was not aware of, I will refrain from this in the future. Thanks!
Basically, as in other questions I've asked related to my php chat application, I am trying to get it so that there is a text field where $msg is displayed via msg.txt. Two users can communicate to another in this way. This would be easy if I wanted to use a simple include function. But I don't want to take all the trouble to make and upload all those pages to my server. So how can I have it where when the user, say named Aaron, clicks on a button titled Benjamin, and types to a file called aaronbenjamin.txt, and if Aaron wants to talk to another user, he can press on a button titled Chris, and type to a file called aaronchris.txt? And all from the same box and text field? Thanks, I appreciate it.
The most important thing about my question was that the user is able to press the name of another user and they will be able to chat with the user. This means that they will be switching to a different .txt file whenever they click on a user. Thank you to #Cyclone and #cronoklee for your answers, but sadly they didn't work.
Use this for the buttons:
<textarea name=msg></textarea>
<input type=submit name=file value=Aaron>
<input type=submit name=file value=Benjamin>
And this for the PHP:
$fn = preg_replace('/\W+/', '', $_REQUEST["file"]);
file_put_contents("$fn.txt", $_REQUEST["msg"]);

How is the Pop up alert which is displayed on stackoverflow when you are answering a question and someone submits another answer created?

Ok, so you know when you're answering a question and are in the middle of typing it, and someone else posts an answer to your question and you get a little popup that says there is a new answer to the question? My question is how do you do that? I think I have the basic concept down... A question is answered, added to the database. The page your on keeps checking the database for new answers, and if there is something new displays a popup. (I'm not sure if that's how it's done, but just and idea) Anyway, I'm trying to create an application with similar functionality to that popup using php, and jQuery / Ajax / something else? I have a page that will be on the screen and will display information from the database. What I need: to figure out how to get that popup to display only when there is new content added to the database.
Thanks for the help!
I should also add... if anyone has any tutorials, or code snippets to share about the ajax / jquery integration with sql that'd be great. I'm pretty decent at PHP but totally new to ajax and jquery :-/
when you first load the question page, also read the number of answers.
Now poll the server with ajax requests every few seconds/minutes that return the number of answers..
If the number of answers is greater than the number when you first loaded the question then show the message that additional answers have been posted..
This is a tutorial on how to do the actual notifications.
As for the querying, it's a fairly simple AJAX call to the database on a timed interval to check for new results. If count > 0, then fire the notification process.
The refresh method on this tutorial could easily be re purposed for such notifications.
Create a timer in Javascript and call an AJAX endpoint that provides you with messages to diplay or that delivers HTML to place in a placeholder at the top of each page. If you get data returned then insert that into the page DOM using jQuery.
You got the basic concept - I don't know for certain, but this does seem to be the obvious way to do this.
As for the specific question - the result from the ajax call should indicate if an answer has been added or not. Only display the popup when the results indicates that a result has been added.
You can use jQuery to do almost all of this.
Create a script that will return the needed JSON data, probably just the id of the current article and then the count of answers. In your page, create a timer that performs an ajax call to the script every ~30 seconds. In your success callback function, compare the number of answers that were returned against the number of answers currently on the page. If the answer is greater, then perform a notification using the show() or fadeIn() functions. (You can either have the message HTML loaded when the page loads or you can append/prepend all of the html for the message with the ajax call.
This is a vague answer, but the question is actually very broad so you could do this in a million different ways. If you need some help with it, you can PM me.

Categories