Auto save changed text in richtextbox to database [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have created one simple note taking application in jquery & php.
Current functionality is like when user clicks save button it sends a ajax request with complete data to update it in db(mysql).
All works well. But now I want to auto save only changed text while user typing.
I don't want to send entire text to server again and again in text change event.
It should send only text which has changed.
For Ex:
This is saved text.
When user continues typing.
This is saved text. Unsaved text..
It should be able to send only "Unsaved text.." to server to update in db.
How can i implement this in jquery & server side script.?
Any Idea..?

I do not think this is a good idea. First of all why do you want this functionality? Just for fun, I saw it somewhere and thought that it will be cool is not really valid. One of the valid reason I see is that the notes will be huge and you do not like to send them constantly to a server. But you have to understand that with a text note it is hard to get a huge note. You have to type a lot in order to surpass 1KB.
If you really need it, you have to handle finding changes on a client and on the server. On a client you have to find what has changed (and taking into the account how much time have you spent thinking about a question, I highly doubt you can do this). It is not just adding new text in the end. What if person will:
add the text in the middle
remove some text
substitute a string with another string
make multiple above-mentioned things
Even if you will do this, when you receive this info on the server, you have to write a logic to change your field in the database. And it will be much harder than just simple update.
So my thoughts about this: do not over complicate things.
If you afraid that the person will lose his changes and do not want to send new draft to the server every time a user added/deleted few chars, than save it locally with the help of localstorage (html5 localshorage) or cookies and build your client side logic upon this.

An option would be to make the richtextbox a Content Editable div containing a simplified HTML markup so that when you add something it appears in a span like this:
<span class="changed added" data-pos"23">new text</span>
where data-pos is the location of the inserted text in the textbox and when you delete something it appears in a span like this:
<span class="changed deleted">deleted text</span>
That way when you click save you can send only the changed text $('#richtextbox span.changed') back to the server for processing. Then the server can do a string replace to add or remove the text as needed from the original text in the database.

Related

Make permanent changes to a webpage, through the webpage

Right this has been asked before in similar context but the answer was not given.. I need to know how to change the contents of "homepage.php" (Example) PERMANENTLY by filling out a form on the webpage itself, I know I have to store data in MySQL database but how should I go about doing this (which way). I know how to store and retrieve data but just this particular problem has me baffled.
Do I save single css values into the database (e.g. blue, green, margin-left, margin-right) or can I store a whole css block of code as a variable then save it in the database
|| body { //Content of body } .navbar{ //navbar content here }? ||
End result I need is to edit a page without altering the code so that everyone can see it not just by using cookies. (Please do not tell me about needing a server ETC I know..)
I am using the Procedural method of PHP programming with no framework for those seeking to give an example.
Thanks in Advance =D!
I would post a comment but I don't have enough points to comment.
Would you be able to utilize jQuery for the webpage? I have had to do this exact same thing with php/mysql, and using jQuery .css() and .html() provides a working solution.

Form add new line on enter, set up for sql

There are a lot of little pieces of information on this subject, and maybe I'm just not looking in the right spot. But I'd like to put it all together in one place. Start to finish.
I'm trying to make a form pretty much like the one I'm using right now on stack overflow. There is a textarea, when someone hits enter, it does not submit the form, but adds a new line. But it doesn't display \n or <br \> in the actual text box.
Then the text with all structural integrity needs to be entered into a database and retrieved to an xhtml page.
I've hear of using javascript. I've heard of the nl2br function. They all seem to be pieces to the puzzle, but I can't find the big picture.
Does anyone have a sample script of start to finish how they would accomplish this?
Thank you very much for your help! I hope others can use this too who are having difficulty.
You need to create a regular <textarea>, and save its raw text to the database. (making sure to use parameters to prevent SQL injection)
Then, when you want to display the text, use <pre> or nl2br() to ensure that the newlines are displayed by the browser.

How do I permanently change the contents of a div with JavaScript & PHP?

Okay. So I don't have any example code to show, but after doing a bit of research and learning the basics of PHP, I think the answer to my question should be pretty simple.
Here is the scenario, as I would like it to be:
On the homepage there will be several team names, with scores next to them. Like "house-points" in Harry Potter.
Below the score is a small text-field. Below that is a submit button.
The user will put a number in the text-field, press submit, and that number will be added to the team's total score.
NOW. I know how to achieve all of that with JavaScript. Easy. What I want to know IS:
How do I make that new number (the new score total) STAY there. I need to permanently alter the HTML when that submit button is pressed.
All I know is that I need to use PHP. I know the basics of PHP, so whatever the answer is, just throw it at me and I'll figure it out.
Sounds like what you want to do is submitting forms. First drop the JavaScript, you won't need it. What you need is to put your text fields in a form and when you submit you can fetch your values with $_<GET|POST|REQUEST>['<name_of_field>'].
Then you will need to store it somehow. The best way to do it is to use a database like MySQL or MongoDB to store it, but it could be a bit tricky if you are just learning this, so maybe you would like to stick to files. You could do this with INI files and PHP's INI functions.
Lastly you will need to print out the correct values to the website. Now this is easy: Just edit your HTML file to do something like
<?php echo $score['team1']; ?>
for each team after retrieving the correct values at the top or something. (Don't forget to rename the HTML file to .php as well).
Now you should be all set to save your scores. =)
If you mean really permanent you'll have to send it to a database via Ajax (combination of PHP and Javascript). OR write it to a text-document, which is less good.

How to use php to store and display any visitor's input

I want to create a very simple site, but unfortunately my php skills are weak. Basically, when a user shows up, I want to have a page with text and a blinking cursor (I can probably figure the cursor part out myself, but feel free to suggest). When a user types, I want it to show the text as they type, and when they hit enter (or click something/whatever), the text just typed will be sent to a database and then the page will update with that new text, for anybody else to see. The cursor will then be blinking on the next line down. So basically it's like a really simple wiki, where anyone can add anything, but nobody can ever remove what has been typed before. No logging in or anything. Can someone suggest the best way to go about this? I assume it will require a php call to the database to display the initial page, then another php request to send data, then another php request to display the new page. I just don't know the details. Thanks so much!
Bonus question 1: How can the page be updated dynamically, so if A sends text while B is typing, B sees the text A sent on B's page immediately?
Bonus question 2: What sorts of issues might arise if this database grows extremely large (say, millions of words), and how might I address these up front? If necessary, I could show only a small chunk of the (text-only) database on any given page, then have pagination.
If you only have one page, you don't need a database. All you need to do is save a text file on the server (use fopen() and related functions) that only gets appended to. If you have multiple pages, then a simple id (INTEGER), filetext (LARGEBLOB). (Note largeblob has a limit of 2^32 bytes).
For the user's browser part, you'll need to use Javascript and AJAX to inform the server of any updates. Just get in touch with a PHP script that (1) accepts the input and (2) appends it to a file.
Bonus question 1: How can the page be updated dynamically, so if A sends text while B is typing, B sees the text A sent on B's page immediately?
Also use the AJAX call to fetch new content (e.g. if you assign line numbers, then the browser just tells the script the last line it read, and the script returns all new lines past that point).
I assume it will require a php call to the database to display the initial page, then another php request to send data, then another php request to display the new page.
Pretty much. But only send the last 50 lines or so of the file when the browser visits it. You don't want to crash the browser.
Bonus question 2: What sorts of issues might arise if this database grows extremely large (say, millions of words), and how might I address these up front? If necessary, I could show only a small chunk of the (text-only) database on any given page, then have pagination.
Think in terms of bytes, not words, and you'll likely run into performance issues. You could cap file sizes or split up the storage into multiple files at a certain size so you don't have to scan pass content that will rarely be fetched.

Adding front-end functionality to a PHP and MySQL driven Quiz application

I recently built a quiz web application using PHP and MySQL (www.ReadySetQuiz.ME) The format looks something like this:
When did we declare Independence from the British?
1775
1777
1776
1774
The Louisiana Purchase was an aquisition from which country?
England
France
Spain
Germany
The answers are a list of radio buttons. After you answer all the questions you hit the submit button and it shows how many you got correct and a breakdown of each question: whether you chose the correct answer (if not, which was the correct answer) and a distribution of previous users' choices.
One big problem I'd like to solve with this current quiz design is people can just open another tab in their browser and Google the answer. I'd like to only show 1 problem at a time and add a time limit to encourage quick responses, and consequently, eliminate the aforementioned problem.
So, I'm looking for some input/guidance - big picture (50,000 feet). How do I make it so one question is presented at a time and users only have a few seconds to answer the question? Do I use JavaScript? Are there any new improvements in HTML5 that can help me accomplish something like this? I'm really just looking for what I need to learn next. Thanks for your time!
Question1) How do I make it so one question is presented at a time and users only have a few seconds to answer the question?
You need to use a countdown/timer to present the user for a minute of less.
Question 2) Do I use JavaScript?
Yeah you can use javascript and use jQuery to make your life easier.
Also disabling right click menu and text selection in the browser window (using js) will make it difficult for the user to just copy the keyword and paste it in a new browser and search. So if they will have to type the question/keywords it will take time.

Categories