So, Im creating a library for other uses, but how can I make content from a file specificly go within the <head> or <body> html tag attribute etc...
for example, this is what im trying to make.
<html>
<head>
<?php include('content/starter/library.php'); ?>
<!-- From that included file, theres a script that put content in the head.-->
</head>
<body>
<!-- From that included file, theres a script that put content in the body -->
</body>
</html>
Im just trying to find another way instead of making multiple files for specific sections and do
<html>
<head>
<?php include('content/starter/library_head.php'); ?>
</head>
<body>
<?php include('content/starter/library_body.php'); ?>
</body>
</html>
Which I don't really want to do. Im not very good with javascript so, There no hope of me trying to figure out how to do this with javascript. Thanks for the answers in the future.
If you want to use one file (as your questions suggests) then one method is to create variables or functions in your library.php file and then echo them in your template
// contents of the library.php file...
<?php
$head_content = "put your <head> content here";
$body_content = "put your <body> content here";
?>
// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
<?php echo $head_content ?>
</head>
<body>
<?php echo $body_content ?>
</body>
</html>
UPDATE
To answer the question in your comment, here's an example using a function. You can put all of your code in a function and then just echo that anywhere in your document.
<?php
// contents of library.php...
function head() {
$return = '<link href="file.css" rel="stylesheet">';
$return .= '<link href="another_file.css" rel="stylesheet">';
return $return;
}
// your HTML file...
<html>
<head>
<?php echo head(); ?>
</head>
PHP functions explained: http://www.w3schools.com/php/php_functions.asp
Related
Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try
I'd like to select an included html element with jQuery. How can I achive this?
index.php
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
file.php
<?php
echo '<div class="test">test</div>';
?>
select.js
let a = $('.test');
console.log(a); // returns undefined
Try executing your script after the page has fully loaded.
$(window).on("load", function() {
let test = $('.test');
console.log(test);
});
Code should be organized as follows: <?php Php.Code.Goes.Here ?>. This can be surrounded by HTML, for instance, <b><?php print("HELLO!"); ?></b>. This will display: HELLO!
So...you see the problem then, with...
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
The HTML is IN the PHP. You want this:
<html>
<head>
...
</head>
<body>
<?php include('file.php'); ?>
</body>
<html>
This code, of course, shouldn't have executed at all, since <?php <html> isn't really valid PHP syntax.
For example you have output <head> tag and don't know title at that moment.
You will know title at the end of code.
How to set title from that place?
Simplified template looks like:
<html>
<head>
<title>We don't know title</title>
</head>
<body>
<article>
<!--at this place we include code, that output article and know title-->
</article>
</body>
</html>
Thanks.
First thing to try, the code that is calculating the title I assume occurs after you have outputted the <head> HTML. Are you able to move this code to before the <head> and store any HTML in a variable to be printed after?
Otherwise you would need to set the answer using a script that executes when the page is fully loaded. Something like
//index.php
<head>
<title></title> //blank title
</head>
<body>
<?php
//code that finds title
$title = 'I am set later';
?>
//All other page output
<script>
//This will run at the end of the body after everything else.
document.title = <?php echo $title; ?>
</script>
</body>
</html>
edit: nicely caught
How to pass the php variables from dynamically created HTML page to next php file.
For Example. I have the following php code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>
Now when I see dynamically created HTML page, it just shows me the following code with .html extension; so how can I pass the php variable from this page to next file:
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
try to replace
<a href='comments.php?id=`$uid`'>
to
<a href='comments.php?id=$uid'>
try this... Your mistake is here 'comments.php?id=$uid' is string no php code use correct like this 'comments.php?id=".$uid."'.
<?php
session_start();
$uid = $_SESSION['uid'];
$doc = new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=".$uid."'> comments</a>
</body>
</html>
");
echo 'wrote:' . $doc->savedHTMLFile("/home/user/project1/test1.html") . 'bytes';
?>
Basically, you can use $_GET[''] method to do it. You just insert it into the URL, but I think, this will make the site vulnerable to SQL Injection.
remove back ticks(`) around $uid or use this code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
comments
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>
How can I return only the body of a PHP file?
An example of how the file would look like:
<html>
<head>
<title>Test Page</title>
</head>
<body>
<div class="content"><p>Hello World</p></div>
</body>
</html>
How can I only return the body of the page with PHP?
<div class="content"><p>Hello World</p></div>
I would use a GET variable to indicate I only require the body, else the entire page is needed.
<?php
// read and interpret your GET-input variable:
$body_only = isset($_GET['body_only']); // you can enhance/change this condition to match your needs
if(!$body_only)
{
// if NOT only body was requested, output intro (head, etc.):
?>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<?php
}
// always output body content:
?>
<div class="content"><p>Hello World</p></div>
<?php
if(!$body_only)
{
// if NOT only body was requested, output outro:
?>
</body>
</html>
<?php
}
?>
Personally, I prefer usual C-style syntax in such cases (i.e. <?php if(...) { ?> ... <?php } ?>), so I've used it in the code above.
PHP also offers alternative syntax, which you may find more readable:
<?php if (...): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
Read this for more info on escaping PHP code from HTML.
Also, consider using a template engine.
There's the PHP DOM you can use, it is quite simple and doesn't really need any explanation however if you find yourself having trouble don't worry to write a comment!