I have the following code that is within a file called footer.php
<section class="landing-form">
<form action="%TEMPLATE_DIRECORY_URI%/multipurpose-sliders-templates/slider11/landing-page-form.php" method="post">
<h2>%FORM_TITLE%</h2>
<p class="dsc">Enter your details below and we'll call you.</p>
I would like to include some PHP within the file, but when I add some it doesn't render. I'm not familiar with the %variable%. Is there any way to add PHP code to this type of file?
That looks like a mark up from ASP, not PHP. In order to do this in PHP do the following:
Create a new file to hold your variable. Name it var.php. In there:
<?php $formTitle = 'Whatever you want' ?>
In the HTML of your PHP page,
<? include 'var.php'; ?>
And in your HTML
<h2><? echo $formTitle ?></h2>
I must reiterate that this must be done in a PHP page on a server that is running PHP. This will not work with straight HTML. This will not work on a server that is not executing PHP.
But there you have a simple example how this is supposed to work.
Related
I have this code:
<!DOCTYPE html>
<html>
<?php require_once('./module/mod_head.php'); ?>
<body>
<?php require_once('./core/imp/navbar.inc.php'); ?>
<div class="container" style="margin-bottom: 50px;">
<?php include('./module/mod_preview.php'); ?>
</div>
<div class="container-fluid">
<?php include_once('./module/mod_block_whoami.php') ?>
<div class="collapse" id="summaryId">
<?php include_once('./module/mod_block_summary.php'); ?>
</div>
</div>
<?php require_once('./core/imp/footer.inc.php'); ?>
</body>
</html>
Now this code is saved in a database.
The problem is I want that code to be used as it would be the only code in my index.php file.
But as I said before it is saved in a database and to get that code I need to use php and then the code will be saved in an php variable and echoing that would be like:
<?php echo "<html><?php echo "<morehtml>blablabla</morehtml>"; ?></html>"; ?>
I thought of creating a temporary file that gets overwritten whith the code and then included by the index.php file or whatever file needs to get code from the database.
However I don't know if that is a good idea or if there are any better ways to get php code from an MySQL database and using it.
If you want to use php code from database, try eval (Evaluate a string as PHP code).
$sting // your variable with the data from the DB
<?=eval('?>' . $sting . '<?') ?>
But this is dangerous, from documentation:
"The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand."
PHP offers the eval($string) function. It runs the code in the string you pass it. So, it would be a simple matter to read code from a MySQL database and run it.
But, is this a good idea? Many people don't think so, because it makes it easy for a badguy to pwn your application.
I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()
I need to generate PDF files from HTML templates and plan on using wkhtmltopdf to do that. Inside the HTML templates, I need to be able to use PHP logic to adjust what the template will render. Take this HTML template for example:
<p>Dear <?php echo $firstname; ?>,</p>
<p>Thanks for signing up. You've invited these people along with you:</p>
<ul>
<?php foreach ($invitees as $invitee): ?>
<li><?php echo $invitee; ?></li>
<?php endforeach; ?>
</ul>
<p>Regards,</p>
<p>Chris White</p>
I have no problem being able to pass a HTML template file to wkhtmltopdf but I don't know how to get the PHP logic inside it to run correctly and be able to return the resulting template. I came across this blog post while Googling but the author uses Smarty as a template language: https://davejamesmiller.com/blog/php-html-pdf-conversion-using-wkhtmltopdf
Using Smarty would solve my problem but I don't want to bring in a library to do this when I can just use plain old PHP. Basically, I need a way to pass in variables to the HTML template (in this case $firstname and $invitees), have it execute the PHP code inside the template and then return the resulting template after the PHP has been executed.
Any ideas?
Just save your file as php (for example template.php) and implement there the logic you need.
I did the same also with wkhtmltopdf and it worked great.
I also passed some variables over GET to the template to really get the correct report as pdf.
To save the file I used the PHP session id and saved the file to a folder with write permissions for www-data (Linux) and started the download automatically via Javascript.
I had the same need and I did it like that. Don t really know if my code will help you but why not.
First I used composer to get https://github.com/mikehaertl/phpwkhtmltopdf.
lets say you have a php file " content.php "
<?php
echo "<html>";
echo "<h1>test</h1>";
echo "<html>";
?>
your index.php will be :
<?php
require "vendor/autoload.php";
ob_start();
require('content.php'); //The php file
$content = ob_get_clean();
$pdf = new \mikehaertl\wkhtmlto\Pdf($content);
if (!$pdf->send()) {
throw new Exception('OMG WHY : '.$pdf->getError());
}
If you're not using any template engine, can't u just call your template.php file with some params ?
Something like
$wkhtml2pdf->html2pdf('template.php?firstname=Foo');
(I have no idea how wkhtml2pdf works, this code is just for you to understand the logic)
and in your template.php file :
<p>Dear <?php echo $_GET['firstname']; ?>,</p>
Is there any way to store PHP code in a text file (for a navbar that may change) and access this text file via file_get_contents and add it into a div in a PHP/HTML document?
What you have to do is
<div class="navbar">
<?php include("nav.php"); ?>
</div>
You can use the built in include() function in PHP.
See docs here: http://www.php.net/manual/en/function.include.php
Using this means you essentially read in the contents of the file, and insert it directly where you are making the call. In your case it might be something like:
<div id="menu">
<?php include('partials/menu.php'); ?>
</div>
Whatever you have in the menu.php file (located in partials folder) will be inserted inside the menu div.
As a side note, since it's basically injecting whatever is in the file, you can also use any variables that you set in the menu.php file if need be
Use one of these
www.php.net/include
www.php.net/require
www.php.net/file_get_contents and www.php.net/eval
How can I replace the text on the active document body on a PHP file on code execution?
Do I have to import a file, replace text on it and then echo it or can I just manipulate the document I run the PHP script on?
I am trying to use templates for easier HTML editing like :usernamecomeshere: and then replacing that :usernamecomeshere: with the actual value. I am wondering If I can do it on one file only instead of loading a file and then displaying it.
If I'm getting your question correctly, you don't need to important a file and echo the document. You can directly manipulate the document itself. For example, in the below sample, you can directly echo the contents of $username in a way that's interspersed with HTML code.
index.php
<?php
// handle code to login
$username = "David";
?>
<html>
<body>
<p>Hello, your username is <?php echo $username ?></p>
</body>
</html>
Worth pointing out is that PHP itself is a templating engine. If you want to replace text, you can do it using PHP such as:
<?php
$user = 'Ugur';
?>
<html><head></head>
<body>
<h1>Hello <?php echo $user; ?></h1>
</body>
</html>
Beyond this sort of simple usage, you may want to look at various template engines, which allow you to do much more elegant things, but are more complex. Take a look at mustache, perhaps?
If you're trying to make these modifications after the page has loaded, remember that PHP runs on the server-side, not the user-side. For that, you need Javascript.
you'll want to look up str_replace() on google. You can search an entire string and replace specified keywords simply.