PHP: reload a php include function without reloading the page - php

I have a page that has a few php include functions, pulling in content from external php files to represent on the page. Like so:
<?php
include 'htmlblocks/catalog.php';
?>
What I am looking for is a way to reload one of those include functions when a button is clicked.
Is this possible and if so how would I go about doing this?
*The end goal is to have this specific file (the one that is included via php) take parameters from the url to use to select data in a SQL database *
Thanks in advance!

Related

How should be a dynamic website structured?

I have wrote my own CMS in PHP. It is quite simple, but it does not permit me to do some things, like the redirect with header() from dynamic page.
Here is its structure (very simplified):
<?php
$db = new PDO...
try {
//getting page info from database (by $_GET['id'])
//and put results into $pageInfo
$stmt->prepare
//.. catch etc...
?>
<!doctype html>
<html>
<head><title><?=$pageInfo['title'];?></title></head>
<body>
<?php
//this file below cannot contain a php redirect, because headers are already sent
include($pageInfo['content_path']);
?>
</body>
</html>
There is one page that is dynamic and can display other pages by changing the value of the get parameter id.
The included content often contains PHP scripts.
I have looked around the internet but I don't know how to resolve this issue.
May I create a header.html and a footer.html and include them into every page? But if I include the header file before including the content doesn't it send headers? How?
Really sorry if this question seems stupid, but I don't know how to do it. I haven't someone that teaches me, so I have to learn all by myself but for some things I don't know where to look.
Thanks in advance.
The preferred way is to have the application separated into several parts, one part that executes code (Controller or core) and another one that displays something (View or template).
For example in MVC (Model/View/Controller), your single page would be just a dispatcher that calls a controller, the controller executes PHP-code, and fetches some data by using the second part, the model, and finally calls a template to display the data.
In your example you could start by including two files for each call, one that contains only PHP-code and fetches all data, and after that one that contains a html-template with variables and only very simple code that is needed to display the data. You can then do redirects and exit in the php-part (controller).

Check which page called this php?

Here is what I'm dealing with:
Let's say I have some php page:
<?php
// do php stuff
?>
And I have two HTML pages where the action sends them to this php page. On this php page, I want it to do one thing if it comes from one of the html pages, and something else if it comes from the other. I cannot pass any explicit variables to the php page, I need to check which page called the php.
I have tried:
$_SERVER['PHP_SELF'] when I access this, it doesn't seem the send the referring page.
$_SERVER['HTTP_REFERER'] this gives the previous page to the one im looking for.
For example:
HTML PAGE1 form calls php page --> I want some variable in PHP that contains HTML PAGE1.
HTML PAGE2 form calls php page --> I want some variable in PHP that contains HTML PAGE2.
Thanks for the help. Please let me know if there's some way I can clarify the question.
If the domain differs you can use:
$_SERVER['SERVER_NAME'];
But in all scenarios this would help:
$_SERVER['REQUEST_URI'];
Giving you the complete URL that was called.
As Think Different said a hidden field in your form is probably your best bet because you don't want to modify the url. In this case you would have the form submit over a POST request and retrive your data with $page = $_POST['YOUR_HIDDEN_VARIABLE_HERE'].

Check text file every 20 seconds and get contents

I have a text file storing one string. I anticipate that the text file will be changing frequently, so in order to keep my page up to date, I would like to use PHP (preferably) to fetch data from the text file every 20 seconds so I can explode it into an array and use the contents accordingly. The variables would also need to update every 20s.
So: on page load, the contents are fetched and displayed. But the contents of the text file may be changed thus making the page outdated while a user may already have it open.
I tried META Refresh, but the whole page refreshes in the middle of browsing and interrupts the user.
Sorry for the confusing description, it's hard to explain. :)
I've searched the web for ages and not found an answer to my question. Please remember I am using a text file and not MySQL, since I'm only storing one string.
Thanks in advance.
If you want to stay with PHP, I'm afraid a refreshing HTML Meta is the solution :
<meta http-equiv="refresh" content="10; url=http://example.com/" />
Refresh the page every X seconds, so that the file gets reloaded.
Another way could be the use of frames, however I cannot seriously recommand it to you.
However, you can load a content without reloading the whole page, using Ajax. It allows you to perform a HTTP request to the server (using a Javascript code) and place its result on the current page, using Javascript as well. You could create a PHP script "my_string_parsed.php", which reads the file, and then parses/prints its content. Then, you could call this script through an Ajax request to http://yoursite.com/my_string_parsed.php, and place its result in a specified HTML tag on your page.
W3Schools.com provides an Ajax tutorial here : http://www.w3schools.com/ajax/
A warning concerning Ajax though : an Ajax content loading must never replace the typical HTTP behavior your browser and the server have. If the string in your file is the only content on your page, then the best solution would be the refreshing meta. Ajax should only be used to refresh parts of a page. Never the whole thing.
Why not using a database instead of a file. You could also use jQuery to update your page smoothly.

edit a file via php

I've look around, and not found anything yet. So I'm going to ask a question, sorry if this is 'nooby'
What I want to do is this:
Have a page with a form on it, IE: Form.php
and I want to allow myself to edit another page, IE: index.php
Kind of like a really BASIC two page CMS, edit it on the form.php page, and then it saves on index.php doesn't overwrite, but saves it under the current post that's already there.
Sorry for the 'vague' question, however want to do this fast :P
You can do it the way Patashu said with a database but if your only going to be editing one page you can do it with http://php.net/manual/en/function.fopen.php.
On the Form.php have it open index.php using PHP then have a textarea field that echos out the index.php and then using the Fopen function save over index.php. Make sure you secure Form.php with a password or even using a database. It also depends what programming language you will have inside index.php
Instead of editing HTML or PHP using a PHP file, you should make a database, store content in it that you want to be dynamic, and retrieve from the database when the PHP for the dynamic page executes. Go read about SQL :)
(reposting so it can be chosen as selected answer, if you want!)

Refresh a PHP include using jQuery

I have a PHP include file which contains a lot of user actions i.e. login, logout, toolbars, buttons etc. This file is the same across the entire site hence the include. Inside the include I have jQuery scripts which call other files to validate a user login, log them out, user menus etc. This include is the entire header of the site.
Now I want to reload this include after say a user logs into the site but I do not want to reload the entire page and they could be performing action which I dont want them to lose or I dont want them to be stored as cookies/sessions.
There is a container div (#dynamicHeader for for example) which holds the include.
I have tried the .load() from within the include but that does nothing. I know the content is being replaced correctly as I have tried .html("logged in") and the container div displays this. Does anyone know a way to reload the include from within the include?
Many thanks
You could use $.get() Documentation Here. You would call the page to be included here and set the html of #dynamicHeader to the returned html in the callback.

Categories