display records randomly one after another - php

I want to display 5 records from a database with in a td one after another (about 60sec). How will i do this, with out the need of page refreshing? I don't know any idea about ajax

So you want the contents of one cell be replaced continually? In that case, you have to use AJAX to retrieve your data and a timer to trigger this retrieval and contents change.

You don't have to use AJAX. You can put the content you want to refresh in an iframe, and the framed webpage will have a meta refresh header. This type of thing has been done for years before AJAX existed.

by using you can achieve
Algorithm :
Create the td with some id.
Send the ajax request( for require time of interval )
Get the random rows from by ajax , and put the values
into that specific row id using innner.HTML
That's all it will work.
If you want to know how to use ajax refer this below url
http://www.w3schools.com/php/default.asp

In this case, have to use AJAX

Related

Reload content page after delete a object via AJAX

I have a button and I want this to call a event via AJAX. This event is for delete a DB record so I want the content updated without the delete value/row. I think maybe I should put my code in the success of the .ajax call but my doubt goes to how to refresh the content? Do I need a second AJAX call to get the new content without the deleted value/row?
Thanks in advance
If you are using ajax then why are you refreshing page ? We use ajax to avoid page refresh. Still if u need it u can try add window.location.reload(); function in your ajax.success() function. here is a example in case its useful to you http://www.amitpatil.me/ajax-table-adding-removing-rows-dynamically-using-javascript-animation/
You can have it in the same call. In the DELETE script that the ajax call is pointing to just add on the query and send the data back using json_encode().
Then you simply update that specific area of the page.
Alternatively if you want to be lazy, after a success return from the ajax, simply do:
window.location.reload();
which will refresh the page.
If you use AJAX for delete records than you don't need reload all page content (it's a goal of AJAX). In this case you must change you content with JavaScript DOM element removing.
If you delete some record with AJAX with some id you performe something like:
$("#deleteBtn").click(function() {
// detect ID of the record (var id = ...)
// call AJAX with post(...) or ajax(...)
$("#id" + id).remove();
});
Or better if you perform remove() function on AJAX result for more security and remove it only if your server side script really have delete the record from DB.
In my example assumed that you use id attribute of the your rows (or other HTML elements) for storing id of the record for access in future via $("#id" + id). You need some entry point for find your DOM element by record id.
As I see it, there are who ways of doing this.
1: wrap your rows in a container with the unique ID from the database row, and then use that ID to remove/hide the row that you just deleted from your page.
2: as you mentioned, you can fetch the data again and print it, but this won't be very efficient performance-wise.
/regards

View live updates from MySQL database?

I want to create a page where people can insert some text, hit enter, and the text be stored in a MySQL database. I can do this, but I want to be able to load a page, enter a password, and see a list of all the info in said database, then whenever something is added to the database, it's added to the list on the page, without me needing to refresh the page or setup some javascript code to refresh the page every five seconds.
I believe Satya has it correct in suggesting that you use Ajax in order to refresh the data without refreshing the page. You can have it request updated information from a php script which queries your database for the data you wish to display, and then sets the elements on your page accordingly.
this is probably the best way for you to implement ajax calls using javascript
http://api.jquery.com/jQuery.ajax/
Or you an simly do it with the help of setInterval() function. You can call an html code in a div using
$('#id').html("<htmlcode></htmlcode>");
Example : http://jsfiddle.net/ipsjolly/995PJ/6/

how to limit the content to be shown on a page?

I'm making a page which shows data, which is quite enormous.Tried pagination on it but didn't work the way I wanted. I'm looking for, something like "See more results", which on clicking will increase size of the page. Further, is it possible to do it with some limit on content to be shown on every click, like on every click it should show, say 10 or 15 rows ?
The best way to do this, in my opinion, is to cut the text and load just part of it with PHP. In this way the initial page won't be heavy. Then with a ‘Read more...’ button you can send an ajax request (with jQuery, maybe using the function load() function) you can get additional text, which will be appended in a div after the current text. Additionally, if you want to retrieve just some lines each time you press the button, you can check with jQuery how many div are already added (just adding a class to your new divs, you can check this just with $('#NameOfTheparent div.classOfTheNewDivs').length) and pass the variable to PHP, during the ajax request (so it won't be $('newDiv').load('something.php') but $('newDiv').load('something.php?var=numberOfDivAlreadyLoaded').
Well, this is the concept. :P
in my opinion, you need something like this:
http://andylangton.co.uk/articles/javascript/jquery-show-hide-multiple-elements/

Random text from MySQL with button click and no refresh with PHP

I have a MySQL database with and id and a text string, I want to be able to display it, and with the click of a button display another random phrase without having to refresh the whole page.
I have look quite thoroughly and have no found no answer for this concrete question.
Is it possible to do it with PHP?
First try it WITH refresh.
You'll need to select a random text from your database (hint, use RAND() in your mysql request).
Once you know how to do that, learn how to make Javascript talk to your php page so you no longer need refresh. It's called AJAX, you can look at JQuery ( http://jquery.com/ ) for a library that will help you with it and specifically this page :
http://api.jquery.com/jQuery.ajax/
Your javascript will do a Ajax call to your php page, will get some data back and then will be able to display it in your page.
Look at the example, you should be able to do it from there.
But first do it with refresh, it's a first step.
If i were you i would use http://api.jquery.com/jQuery.get/
Create a page where you do the mysql query and then write a few lines of jquery to get the information from that specific page. You won't have to refresh the page and there are plenty of neat ways to change between the data you get from the database, with jquery
something like:
$.get("the_separate_page.php", function(data){
console.log('Your quote is : ' + data);
//check your log
});

jquery dynamic pagination for comments

I would like to create a pagination system for comments in my website.So far, I have been able to create pagination using php/mysql and html but the page has to refresh every time we click on the next button(for the next set of comments) or previous or a particular page....
As far as my knowledge of jquery is concerned I am thinking that, when the user clicks on the next button we post data for the page number to comments.php then echo all the comments in comments.php, then the jquery data variable recieves all the data echo'd in the file and appends it to the #comments box...
Is my solution a valid one??? or anyone has a better solution.....thanks
Your question doesn't make much sense and is very jumbled.
You can either load the entire list when the page first loads, and use jquery to paginate it by hiding the extra entries, which will work fine for lists with a few pages worth of content.
The other option is to use AJAX to fetch the next or previous page when the appropriate link is clicked.
There are plenty of pagination add ons for jquery. Maybe check them out.
Don't use a POST request to get the next page as it looks like you might be, unless you are just using the wrong terminology.
Yes, when you click 'next', you send ajax request to comments.php and replace current comments with new ones.
You can do it with a get()/getJSON() call in jQuery.
Something like
$('#next').click(function(){
$.getJSON('url?withnextpage=number',
function(data){
//update variables or the DOM
});
});
Returning it in JSON may be quicker. I hope that helps

Categories