I have a HTML5 file respectively a simple webpage.
and I have a php file.
this file includes a function getKursname(string)
The function is working properly when called in the php file.
This all happens on XAMPP.
I now want to update some values and strings in my html page.
this should happen only on load.
this ist a code the code in the html:
<div class="w3-bar" id="myNavbar">
<a class="w3-bar-item w3-button w3-hover-black w3-hide-medium w3-hide-large w3-right" href="javascript:void(0);" onclick="toggleFunction()" title="Toggle Navigation Menu">
<i class="fa fa-bars"></i>
</a>
<i class="myfont icon-heart"></i> HOME
<i class="myfont icon-users"></i> ÜBER UNS
<i class="myfont icon-th"></i> KURSE
<i class="myfont icon-mail-alt"></i> KONTAKT
</a>
</div>
I want to have the innerhtml (e.g. KONTAKT)
to be read from the database.
the php function returns a string.
I can call the funcion with getKursname(string) within php.
my idea was:
<i class="myfont icon-mail-alt"></i>getKursname("kurs1")
but this is not working... :-(
thanks in advance
Dirk
Assuming you have a directory structure similar to this:
www/
index.html
functions.php
and you want to inject the content before the index.html is sent to the browser.
In which case, the solution would be, to rename index.html to index.php and at the point where you want the output of the function, you write
<?php include_once 'functions.php'; echo getKursname("kurs1"); ?>
(or require_once instead of include_once, and you have to include it only once before it is used, to load the function definition)
however, standard is to put includes in the header:
index.php
<?php
require_once 'functions.php';
?><html>
<head><!-- ... --></head>
<body><!-- ... other stuff -->
<?=getKursname("kurs1");?>
<!-- rest of page -->
</body></html>
to clarify: <?=[something] ?> is short for <?php echo [something] ?> and everything between <?php and the next ?> is evaluated by php, everything else (like the html code) is just printed as is.
Related
<?php
session_start();
if (isset($_SESSION['id']) && isset($_SESSION['username'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" type="text/css" href="dashboard.css">
<script src="https://kit.fontawesome.com/463f6f67c2.js" crossorigin="anonymous"></script>
</head>
<body class="dashboard">
<h1>Halo,<?php echo $_SESSION['name']; ?></h1>
<div class="wrapper">
<div class="sidebar">
<h2>Sidebar</h2>
<ul><!--sidebar list-->
<li><i class="fa-solid fa-house-user"></i>Home</li>
<li><i class="fa-solid fa-user-tie"></i>Data Petugas</li>
<li><i class="fa-solid fa-users-rectangle"></i>Data Kelas</li>
<li><i class="fa-solid fa-money-check"></i>Data Siswa</li>
<li><i class="fa-solid fa-clock"></i>History Pembayaran</li>
</ul>
</div>
</div>
</div>
Logout
</body>
</html>
<?php
}else{
header("Location: index.php");
exit();
}
?>
in this case if i click 'petugas' on sidebar the web making the new tab/window without the sidebar,i want the sidebar stay in the page and load the 'petugas.php' without make a new tab/window,just in the 'petugas' section
This cannot be solved only by PHP.
PHP is executed server-sided. This means, the moment you see the page, all PHP is done. Calling another PHP script means calling a new page.
If you want to change content asynchronously on your page, you either need a different technology that works after the page is loaded, like JavaScript performing AJAX-Requests to another script and altering the HTML that has already been generated.
Edit: The answer by #Henrique shows methods for this approach.
Or you can "trick" this behaviour by loading everything upfront and hide the things you do not want to show from the beginning using CSS. Needless to say that this is not a good practice, but solves your question.
If you write each page yourself, you can put the sidebar to some sort of wrapper that loads with every page you link to. It will redraw on every link click, but it will be there for every page. And it seems the least complicated way for your situation.
I'm creating a theme for my php project. I want to decrease my code amount by creating a simple theme.html page and include that to every page.
Can you help me guys?
Example theme.html:
<html>
<body>
<h1>Welcome to panel</h1>
<ul>
<li><a class="home" href="#">Home</a></li>
<li><a class="about" href="#">About us</a></li>
<li><a class="contact" href="#">Contact us</a></li>
</ul>
<!-- content -->
<div class="content"><!-- other page codes will write here --></div>
<!-- content -->
</body>
<html>
And now i want to include this code to index.html and write form codes to content div? Help me...
You can do it using php
for that you have to save the files .php like index.html into index.php
then include the file like below
<?php include 'theme.php';?>
You can also use twig which is a great template manager for php (initially made for Symfony) and really easy to use.
I have researched some answers that talk about php, javascript, iframes etc. but I have tried a couple and none of them work. I am new to HTML coding.. and coding in general!
<link rel="menu" href="menu.html"> does nothing
<!--#include virtual="/menu.html" --> does nothing (presumably because its a comment?)
<iframe src="page.html"></iframe>
or object... both place the menu in a silly little scroll box.
I want to run my menu on my page as if it were a function in C. Where I can just include it, and it be there, or just link it.
Thanks for the help!
Ryan
webpage file: biology.html
menu file: menu.html
<div class="container">
<img src="homeicon.jpg" width="50" alt="Home">
<div class="redhover">
<div class="dropdown">
<button class="dropbtn">GCSEs</button>
<div class="dropdown-content">
Chemistry
Biology
</div>
</div>
<div class="dropdown">
<button class="dropbtn">A-Levels</button>
<div class="dropdown-content">
Chemistry
Biology
</div>
</div>
<div class="dropdown">
<button class="dropbtn">University</button>
<div class="dropdown-content">
Telecommunications
Electronic Engineering
</div>
</div>
<div class="dropdown">
<button class="dropbtn">More</button>
<div class="dropdown-content">
About me
Youtube
</div>
</div>
</div>
</div>
You can use php to include files on other pages. Here is some example code to get you started:
<?php
require_once('menu.php');
?>
You can put this in your HTML page appropriately, however you must make sure that php can be processed on your server and the file containing php code must end in the .php extension.
There are also other methods of including files via php, see here:
http://php.net/manual/en/function.include.php
and
http://php.net/manual/en/function.require.php
Edit - I'm not a big fan of this approach, but it will work on Github pages.
Create a file called nav.js with your menu defined as a js variable, then use javascript to insert it into an empty div created on each page. This way to update your nav you only have to ever edit nav.js Not pretty but it works
nav.js
var navigation = "<nav>";
navigation += "<ul>";
navigation += "<li>Home</li>";
navigation += "<li>About</li>";
navigation += "</ul>";
navigation += "</nav>";
document.getElementById("navigation").innerHTML = navigation;
Other pages
<div id="navigation"></div>
<!--rest of page goes here.-->
<script src="nav.js"></script>
Fiddle: https://jsfiddle.net/ze3hLxx8/1/
There are multiple ways to include a file into another depending on the backend technology you wish / want / need to use.
PHP
The most common way to do it in php is by using the include or require statement inside a php file.
In your specific case your biology.html file must be converted to a biology.php file and then you can add the relative code to include the file:
<?php include('menu.php');?>
This simple statement will add the content in your menu.php file to the current page. This will not work if php is not present on the server and obviously will not work locally without a local development environment
The differences between require and include can be found on the official documentation:
include: http://php.net/manual/en/function.include.php
require: http://php.net/manual/en/function.require.php
SSI
Another method is to use Server Side Includes. To use the SSI it must be supported and enabled on the webserver. To use SSI you need to change the extension from biology.html to biology.shtml and then add the following statement:
<!--#include file="menu.html" -->
More information on server side includes can be found on wikipedia: https://en.wikipedia.org/wiki/Server_Side_Includes
/index.php
/navbar.php // <a id="#this"></a><div id="that"></div>
/user/search.php
/user/register.php
I have this folder structure in a project I'm currently doing. and initially thought of placing navbar in a separate file for me to include in all other files I have. but when I include navbar.php in a file other than whatever is in root, relative paths won't work so I now use an absolute path. navbar.php implements Bootstrap accordion btw, my problem is with that, I can't make it work with all the file path issue I currently have.
What am I doing wrong? What should be done? and any comments on how I structure my files? would PHP's include_path sort this thing out (reading up on this atm) ?
EDIT:
navbar.php:
<div id="accordion">
<ul class="nav nav-sidebar">
<li>HOME</li>
<li>SEARCH</li>
<li>REGISTER</li>
</ul>
<div class="collapse">
<div id="search"> . . . </div>
<div id="register"> . . .</div>
</div>
</div>
index.php:
<?php include("navbar.php") ?>
...some <html>
user/search.php:
<?php include("../navbar.php") ?>
...some <html>
if I use relative path for navbar <a href="">, all is fine but not when I'm inside user folder
and if I use an absolute path, idk, how can I implement it properly to target a specific tag (in my case, an anchor pointing to a div) inside itself?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
CLARIFICATION: Being that I've never used PHP, it was unclear how to format the PHP file, so that when you include it in the index, then header(nav bar) would show up. No posts explained why I had to change my index.html to index.php to make it work. And again, being that I haven't used PHP, I was under the impression that changing the extension meant that I would have to convert my HTML code to PHP commands. I was trying to avoid having to change my whole code around. That's where the confusion was.
I was going to delete the question once I got it figured out. Then again, I'm sure I'm not the only person that may run into this while making their first website.
SOLUTION. Save only the nav code in html language with a PHP extension (header.php). Change index/home file from .html to a .php extension (don't have to modify any code. but accepts the include prompt). Then, include the header.php in your index.php. Same goes for your footer. Thanks for the help everyone.
*ORIGINAL POST: Through another post, I was told it would be easier to use a nav/footer on multiple pages by using PHP.
I created a file and tried including it and it is not displaying. I'm not sure what I am doing wrong.
There is some CSS in my nav bar as well, do I need to import the css file in the PHP file? or does the index.html file automatically attach to the included (php) file.
PHP Code:
<?php
echo '<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
</nav>';
?>
HTML Code (include):
<body>
<?php include '/header.php';?>
....other code....
</body>
If there's a problem with the PHP file, if I'm missing something, can someone show an explain please?
no need to add <?php ?> code in the header file just keep it as it is :
Create a new file named header.php and add this code to it
<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
Include header.php anywhere you want
Remove the <?php tag from your code. As you generated html output. <?php tags are used to generate output by php.
<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
</nav>
If you want to use php tags, than you have to write the code in this way:
<?php
echo '
<nav>
<div>
<a href="/">
<div id="logo"><img src="/Images/7serviceLOGOblue2.png" alt="Home"/></div>
<div id="headtag"><img src="/Images/title.png" alt="Home"/></div>
<div id="tagline"><img src="/Images/tag_line.png" alt="Home"/></div>
</a>
</div>
<div>
Home
<a href="/about.html" >About</a>
<a href="/services.html" >Services</a>
<a href="/pricing.html" >Pricing</a>
<a href="/contact.html" >Contact Us</a>
<input id="srchbar" type="search" placeholder="Search">
</div>
</nav>
';
?>
Your problem is most likely the pathing of you include. Often you'd need to specify from the server root, and not just /header.php.
To specify your including-path, you could specify the root like this
include $_SERVER['DOCUMENT_ROOT']."/header.php";
You can also set the default include path in your php.ini file, thus making the $_SERVER['DOCUMENT_ROOT'] an automatic process, that can be done with running this code, that will alter your php.ini file.
ini_set('include_path', '/usr/lib/pear');
As others have pointed out, you don't need the contents of your /header.php to be echoed out as it's pure HTML. Anything that's included will be included the way it is, so having the contents of /header.php as pure HTML just makes for better practice, as it's easier to read.