I was wondering how can I add two different pages together in php. For example, I would like to have the time variable on about.php and contact.php page and if I change the time from one page, it will affect the other page at the same time. Is it possible to do this from the mysql database?
Thanks
You may use a session for making your pages stateful if that is what you are asking. In my opinion using database call for this purpose would be an overkill. Try setting a session variable for the domain and you may call that variable from any page of that domain.
Please visit these links for further reference for session variables:
PHP Sessions across subpages of same domain
If using javascript/jquery, you may also have a look at this:
One Cookie - Multiple Pages
i think you can do that use require('filename') in PHP, that function/built in make yout get variabel, class etc from another file like you want to do .
that the reference : http://php.net/manual/en/function.require.php
Hope can help
Here is my response.
The qusetion is, Do you want the time update to be real-time event or not.
Real-Time Event:
If you want the update to be real-time(Ie The Page will not refresh or reloads upon the event updates) then you better do it with nodejs and websocket. The most common websocket
is Socket.Io.
Not-Realtime Event
If its not gonna be real-time. You can then use php. You can write an update code in any of the page. You will then also write a code to query the time from the database. Embed the query codes on both pages so that each time the page reloads or refreshes, the time will be fetched from the database and it will work fine. Your best bet will be to combine php with ajax. Thanks You
Related
I'm currently only using PHP to take user submissions, put them in a database, and echo them out on a page using SQL to select from a table, such as comments. I need a system that will automatically update comments without refreshing the page like on YouTube. The less the user has to manually update, the better.
I want it to work pretty much exactly how YouTube and Twitter function, where it'll say "x NEW COMMENT(s)" and clicking that updates everything.
My teacher recommended a JQuery function, but I don't have any background in that language so I don't know where to begin looking.
I'm at a complete impasse. I will update this if you guys need additional information to aid in my search.
You are looking for AJAX
You will need a HTML page with jQuery/AJAX that calls another PHP page. In that PHP page you do the DB request and then ideally return the data as JSON so that your frontend part can display it to the user.
As every one says, AJAX is the way. You can find a simple blog I did on it here.
I want to update my PHP GUI view when my data source is updated(data source can be a database). I'm having a hard time understanding how I can do this without reloading the whole view for the user.
What I try to achieve is:
Users view are shown, from an handheld device call a script on the site and the script updates a data source, when data source is updated, update the users view with this updated data without reloading.
Is this possible with PHP/JS and how would I achieve this? I do not need any code but more step by step explanation and perhaps what technology to use.
Appreciate any suggestion that leads me forward with this problem.
Best regards,
Gabriel Paulsson
Use ajax for refreshing data when any event happens. The page will not reload by using ajax. It is very simple to use ajax. Googel, gmail and many other use such ajax. Learn in ten minutes form w3school http://www.w3schools.com/ajax/default.asp
It should be possible to do it just with php, telling the page to update itself at a certain interval.
The way I would do it, is to use javascript with ajax and a timed event. The event fires of an ajax call to the server, the server returns any new information.
If you are new to ajax, you probably want to use a library to make things a bit easier to handle. My personal favourite is prototype.js, although at the moment that seems to be a bit "on the way out" and eg jquery may be a better alternative.
I'm working on a travel website. I'm looking for a function that allows my users to click a hotel and save their selection to a "list" on the sidebar.
I want it to function like a cart so that the list never erases and so that you can see the selection until you are ready to print everything out.
Any help/suggestions are appreciated.
Thanks!
If your users are going to be clicking around to a lot of pages, it will be a real challenge to keep sending this growing list around from page to page on the client side. I would recommend that you consider a different approach. One is cookies, and another is session state.
If you are using server-side code, it would help us to know what language you are using so that we can give you some more ideas.
I would suggesting using AJAX to pass the selection to the backend. If you want this to persist across multiple sessions you're going to want to save it to a database so they can come back later.
http://api.jquery.com/jQuery.ajax/
Use cookies if you don't want to use php.
http://www.w3schools.com/js/js_cookies.asp
has a good example of how to use cookies in javascript.
I recommend using "My favourite posts" wordpress plugin for this kind of job.
Let's say we have a page written in PHP. This page loads by it self a template with header, body and footer and print this out. Now let's say that in the body of this page we would like to start a loop and load some posts (messages taken from a database).
We also need the page to load new posts every 10 seconds, if any, without refreshing the page (classic AJAX). This ajax call will use JSON and AJAX and micro templates.
Now i'm just wondering:
Do we really need PHP to load posts the first time the page is loaded? Can't we just start that Ajax call and load posts with Ajax instead? (Notice that the existing ajax call would be kept as it is, since it loads posts starting from the latest loaded (in case of no posts, that would mean all posts).
If you did not understand my question don't hesitate to let me know.
In this situation I think the simpler approach is the let AJAX handle it, if you do let php load the initial messages, you'll have two places in code, that you'll need to maintain to perform identical jobs.
I think you are asking how you should load the posts the first time the page is accessed. If so: When the page firsts loads, have some PHP that prints out the existing posts. Then, add some JavaScript to update the page with new posts every 10 seconds. This is a matter of preference. You might want there to be no posts when the page first loads, and then use Ajax to get the existing posts once your page has loaded.
Edit:
I agree with jondavidjohn that you might be better off using pure Ajax. However, you could always isolate the code that fetches the pages into a separate function. That way, the script that generates the page calls the same function as the script that is called via Ajax.
The drawback with that technique is that it doesn't downgrade gracefully. So people with javascript disabled will not see any posts.
I'd recommend outputting some data with php - AJAX requires JavaScript which many people don't have activated.
Why not, instead of having the browser poll the server for new posts, have the browser push new content to the browser when it is available using the likes of node.js?
I designed my site with AJAX exclusively, and it works perfectly except for one rather major issue: Using AJAX requires JS to be enabled. Of course, if users trust your site, this is not a problem, but if they don't, then an AJAX solution won't work unless you put the entire page in a noscript tag.
I would like to ask for how to redirect to previous page (ajax paging).
Example,
Let's say currently I am in page 5, then I click on one record, after I edit it, I would like to go back to that page 5 not page 1.
The problem is my paging is using ajax,
http://domain/photo/#5
I try to use $_SERVER[http_referer], but I only get http://domain/photo , I not able to get # and the value.
What is the best way to redirect back to the previous paging. My last choice is using $_SESSION. Hope I can get better answer here.
Thx.
Like Lauri says, you need to manage this at the client-side using Javascript. Basically you need to implement some sort of client-side browser history. Just look at how Facebook loads in photos, but still lets you use the back button.
There are various libraries available to help you:
JS History Frameworks