I have 2 pages of html: a.html & b.html.
a.html contains text "hello" and a link to b.html.
When I click the link to b.html, I want the "hello" text to also come on specific place in the b.html page.
How can i do this? Kindly let me know.
To bring it along, you'll need to pass it somehow. It could be something you keep in an input element on the screen, then when you submit it (as a form) it will be available as a $_REQUEST element. You could also do this by putting your desired welcome message in a hidden element and submitting it in the same manner.
Alternatively, you could put the value you want in a URL string manually. For example, the link in a.html to go to b.html could be called by:
<a href="b.html?welcome=welcome%20to%20the%20show">
Doing things on the URL string is more PHP than html, and if your server's not setup correctly you may not be able to get values from the URL string in an HTML document. So, in that case it's probably easier just to make it a .php file. Again, not that it can't be done, but it might not be setup to work that way by default.
.html pages are static, so you'd have to copy the desired text manually into the second page.
If you want to do it via PHP, then the simplest way is to put the common text into a file, like:
my_html.html:
<p>This is the common text which should be included in both pages</p>
and
a.php:
... lots of stuff ...
<?php include('my_html.html'); ?>
... more stuff ...
and
b.php:
... other stuff ...
<?php include('my_html.html'); ?>
... more other stuff ...
It sounds like you want to "include" some common text between multiple pages. If you're limited to client-side functionality, it's possible to do this by writing some Javascript to populate an HTML element with a string via DOM, and then include that javascript on both pages. Both HTML pages would have to define the target element.
If you have server-side technology flexibility, I would suggest you look at PHP. Implement both pages as .php pages, and use include() to include a common file (e.g. common.php) in both pages.
You could use php sessions and store the text there so e.g.
a.html
<?php
session_start();
$_SESSION['session_name']="Hello";
echo $_SESSION['session_name'];
?>
b.html
<?php
session_start();
echo $session['session_name'];
?>
A session is more or less a cookie and what you are doing here is storing the string "Hello" in a cookie name "session_name". You can then display the cookies content by using the php echo command :)
Related
I am currently working on a website that uses a login system I made.
I have a div that holds a bar that goes on the top of the website. This bar is supposed to change depending if you're a guest, logged in, or a admin. (Therefor there are three separate divs - guestmode, membermode and adminmode)
I have tried to make php print the html when wanted, and it worked. The only problem is that it's messy and not at all easy to edit.
Is it possible for php to print html without using 'echo' or 'print', but copying from another file?
Basically I want to copy normal html syntaxed-code from a file somewhere and turn it into something that php can print, then of course print it.
Is there something out there that can do that?
Thanks in advance!
For PHP, use something like:
echo file_get_contents('path/to/my_html_snippet.my_ext'); // echo instantly
$stored = file_get_contents('path/to/my_html_snippet.my_ext'); // or save it for later in a string
Create a file at path/to/my_html_snippet.my_ext that contains the HTML you want to print.
Its seems like you need a templates.
See mustache (examples and docs - https://github.com/bobthecow/mustache.php)
I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.
Example:
Index Page:
<?php include('templates/123/index.php'); ?>
templates/123/index.php page
<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>
I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.
I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)
My preferred method of doing stuff like this involves starting my code with:
ob_start(function($c) {
$replacements = array(
"username"=>"Kolink",
"rank"=>"Awesome"
);
return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
},$c);
});
Two steps I suggest
Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
use strtr() function to replace your placeholder i.e {username} with actual values
I think this will server your needs.
I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern
I want to use php to easily maintain my website, but I simply can't figure out the language - I've found some tuts online, and some other questions here, but none help me.
I've divided my site into some .php files, header/footer and such - And using
works fine..
Now I want the content of my site, to update according to which menu I click on at my site.
http://dawtano.com/pp/
If I click on "about" I want the "Hello World" to open inside my content div, but I can't get the right php code to do it.
I think you should do this---
Note: This will only work if the CSS styling are on the current directory! ()
<div>
<?php
$html_page = implode('', file('http://dawtano.com/pp/'));
echo $html;
?>
</div>
Hope this helps!
well currently your links are taking you to a separate page entirely. So why not just code it so that your include file is specific to the page. i.e, on about.php, use something like
include 'about_content.php
in your contetnt div.
If you're looking for your content to load dynamically into the content div you'll need to look into using ajax to fetch the content pages.
One popular way to construct the site is to have a single php script which displays content based upon a $_GET variable like 'page' or 'content', and then make the link as:
'http://dawtano.com/pp/index.php?page=helloworldcontent'
Using this method, you would need to check if the variable ($_GET['page']) is set using isset(), and then make sure the string is safe... as anybody with a browser could just type in some mumbo-magic script and hijack your site:
'http://dawtano.com/pp/index.php?page=somecleaverlycraftedhax'
Once it exists and is safe, add the '.php' to the file name and include that file... if it exists! If it doesn't exist, then you will need some code to handle that, probably by displaying a 'File not Found' message, or redirecting home, or something.
I prefer not to do this because it is a pain to make safe, and I feel like it is pretty ugly. What I do instead is put all the header/footer/navbar/title bar scripts into seperate 'display' functions, and put them in another file.
Then include this file with the function definitions, and call all the 'display' functions to set up the page. So every php script in your site might look like:
<?php
include 'html_display_functions.php';
/* put lines here to parse $_GET and $_POST, session_start()/$_SESSION, etc... */
print_html_pre_content();
print '<p>Hello, world!</p>';
print_html_post_content();
?>
Since every script will have this structure, you can just create a template file once. When you want to create a new page for your site, copy the template, rename the copy to the php filename you want, and add content between the two print functions.
You also keep the ability to modify the header/footer/navbar/title bar for the whole site in a central location, namely the included file with the functions.
You might be looking for some sort of Template Engine which allows you to create your pages out of variable parts. You could have a look at TBS, which is more or less what is suggested by the name. But there is a whole lot more engines out there which could do the job.
If that's already too much over the top, maybe Apache SSI (Server Side Includes) are a try for you.
A little suggestion from my side, I am often using Apaches mod_rewrite in connection with a single controller.php file. Apaches mod_rewrite will then send all request to the controller.php which will fetch the appropriate page parts for the requested page using TBS and return the respective page. So you have the controll of the page in one location only.
To your original question about.php could look like:
<?php
include('header.php');
?>
// original page content as html for about.php
// assuming header ends with the starting div <div> where you like the content to appear
// and footer starts with the closing div </div>
// if you need variable content here, simply use <?php echo $your_variable ?>
<?php
include('footer.php');
?>
The best way would be to use a switch statement:
http://php.net/manual/en/control-structures.switch.php
Something like this:
<?php
include("header.php");
$page = $_GET['page'];
switch($page)
{
case "about":
include "about.php";
break;
case "faq":
include "faq.php";
break;
case "help":
include "help.php";
break;
default:
include "home.php";
}
include("footer.php);
?>
Then just make all of your links look like this:
http://www.example.com/index.php?page=home
Just replace home with the correct page.
I basically have a div on my site that always has the same stuff. However, this div is not present on all pages which is why I won't use the dynamic web template. I was wondering if it was possible for PHP to get the code from a document on the server and put in into the div?
Ex:
<div id="section...
then my text file contains
<p>hello</p>
Basically I want PHP to put it into the div when the user sees it.
If theres a smarter way of acheiving this I'd be open to it as well.
Thanks
Simply include() the file. The interpreter will drop out of PHP mode back into HTML mode, outputting anything it encounters.
Did you try this in the first place?
<div id="section"><?php include ('path-to-your-file') ?></div>
Just have a file with some echo statements that generate your div and then include it only when required.
How to control whether to include it or not depends on how your website is built.
For instance if you have DB entries for your pages add a column called includediv (you can probably come out with a better name...) and if that is true you include the file otherwise not.