I got a page that displays a value from database that i got by fetch.
Lets say clicking a button triggers a function that changes the database value (increases it by 1).
The value is changed in the database, but the page still shows the old number.
Is there any way to update this number without refreshing the page?
Yes, there is a way of updating using JavaScript.
You can use AJAX to connect to the server without reloading the page. For an introduction in AJAX, see the MDN for the JavaScript part and the official PHP documentation for the PHP part. You can also have a look at W3Schools if you want, but don't use it as your only source of learning.
Note that you have to write JavaScript and PHP code to get it working.
Related
I am tying to put some user data from the database using php.
The database contains so many record and i am displaying it using mysql_fetch_row() and this gives me the fields of first row.
Now the problem is a have two buttons namely next and previous. And when a click next the fields in the next row has to be obtained the they should be inserted into page without loading the page once again. I am facing problem with this.
I am using php for server side programming and html, css for client side programming.
I think that no one will give you the answer. Too many to write. You need to read about ajax (this is the technology that will allow you to do some action without page reloading).
You need to write javascript(you can use some library like jquery) function which will open some php page with the data that you want and then update your site.
Here you have some nice tutorial:
http://www.w3schools.com/jquery/jquery_ajax_intro.asp
I have an AJAX POST which runs a php script which updates MySQL. The page is part of a Joomla site. Its part of a series of POSTS which return HTML to div's in the page. It all works well but I currently use a simple echo statement to say "Record Updated !" etc. But I have little control over where this message goes - and I don't know how to remove it when the user moves to another record - I'd like the message to disappear. I've just realised that HTML created by Ajax is "on the fly" - i.e you don't see it when you view source. So it's not possible to use Javascript and the DOM to change it.
What I think I want to do is run a Javascript function within the php - but this doesn't work either !
Any help here appreciated - or an alternative approach to give a database update confirmation message which can then be removed when the user moves to handle another record.
echo statements in php are not shown to the user when you use ajax unless you use the php output in your ajax success function yourself.
So you just need to remove the part of the javascript that adds the ajax return data to the dom / screen (in case of an alert).
As to an alternative solution, you could use a statically fixed status bar that shows up on an update and slides away after a few seconds. I don't know if you are using a javascript library, but using jQuery that would be very easy to implement.
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/
I am retrieving Google's weather API XML and using PHP. I'm retrieving the weather for any searched city.
Now, this "app" is under a tab and whenever I submit the form it refreshes and I want to prevent this.
Is it possible? This will be implemented in a dashboard - thats the reason I want to prevent the refresh.
This is what I mean: http://www.screenr.com/30As
The entire code is here: http://jsfiddle.net/ZshpF/
Simply, copy paste the code in a php file and it will work.
You'll want to use AJAX. Since you mention jQuery in the tags, it has a very handy function for making the call to the server. But making the call from the JavaScript is only half the story, you'll also need something on the server listening for that call. It would essentially be another PHP script which acts as a page in and of itself, but would return data in the form of (most likely) JSON instead of HTML. It's not meant to be human-readable, but rather to be a sort of web service for your JavaScript code to use.
You can find a simple example here.
I think you can use jQuery ajaxForm
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
});