I have some PHP code in my .php file which is showing as it is in the browser. As in, I want the code to execute on form submit, but its showing as plain text in the browser.
<?php
if(isset($_POST['ss-submit']))//submit button name
{
//Describing the Leads object and printing the array
$describe = $connection->describeSObjects(array('Lead'));
print_r($describe);
?>
Below it is an HTML form which displays correctly.
now this .php file is included in a .tpl file.
You can keep your both files different.
In case of smarty the php file is executed first process data and then with the help of assign function you can assign your output from php to the tpl file.
Try as much as you can process data at the php page only
<?php
if(isset($_POST['ss-submit']))//submit button name
{
//Describing the Leads object and printing the array
$describe = $connection->describeSObjects(array('Lead'));
$smarty->assign('lead_array',$describe);
?>
Replace $smarty with your smarty object
Replace lead_array with a name with which you want to access array in .tpl file
now on the .tpl page you can simply write
{$lead_array|print_r}
make sure your form on the .tpl page post on the same php file
it will print your array
This is a sure solution try it once and you will get desired output.
in a .htaccess file
AddType application/x-httpd-php .php .html .tpl
beware this parses all tpl files as if they were php. A templating engine may be better
Related
I want to change my website main page, using php code!, I found the tpl file which I need to change, problem is that I have no experience in tpl and I want to write it with php but it won't work. I am trying to use:
{include_php file="/path/to/somefile.php"}
To use php language but it does not work. How does it work with tpl in Smarty templates using php language.
My suggestion the following is a php file
include_once("/var/www/html/smarty/libs/Smarty.class.php");
$smartyobject=new Smarty();
$varr="stackoverflow"; //php variable it can also be a array";
$smartyobject->assign("website", $varr); //assigning a variable to "website", and this is now a smarty variable.
$smartyobject->display("tplfile"); //this is the actuall file that get displayed, in this tpl file you can use "website" variable {$website}
smarty has so many in functions loops.. so there is no need include any php file. do whatever you want to in the php file save the result in a variable and display it in a tpl file.
I got 2 files. File B is gtting called in File A. Now i need to edit some things in File B for which i need variables from file A.
I now tried to include File A (parent file) to file B like this:
(I am using SMARTY for my page, basically it´s the sam like a normal PHP include)
{include file='file:/standard-usermod.html'}
Now when the file gets included i can indeed use the variables of File A but the layout is a mess. It also includes me Layout things of file A, but i only need it´s variables. Is there a posibility with PHP (it does not have to be written in SMARTY) to include a file just for using the variables without showing it´s layout on the page?
Use ob_clean() after file include
I am trying to roll my own MVC purely for learning purposes, but am hitting a snag when I try to load in the view files. Here's my problem:
Front controller grabs appropriate page controller, page controller include_once() the appropriate view. The view is a file containing HTML and PHP. When I return the include file from the controller, the included file content appear out of sequence of the PAGE layout intended. (example: I want to display HEADER CONTENT FOOTER, but when I include the view's contents, my controller spits out CONTENT, HEADER, 1, FOOTER).
In my front controller I am attempting to set my included file contents to a variable I then echo in between my HEAD and FOOTER in the PAGE template. But this does not work as intended. I can wrap my view file contents in: return "code" but I don't want the added headache of worrying about stray quotes/double quotes in my view code that could break the script. I have tried file_get_contents(), but this only spits out a string (albeit in the right place/sequence in my template) and does not the PHP in my view files correctly (ie at all).
My question is: Is there a way I can return my view file to my front controller using include_once() in a variable? I can't seem to find a adequate answer to this. I'd be happy to add my code if it would help.
You can assign a return value from an include(). A lesser-known feature of this function is that you can have a file like this:
<?php
// config.php
return array('database_host' => 'localhost');
and you can read it in thus:
<?php
$config = include('config.php');
However, you're wanting to do this:
<!-- text.html -->
<h1>Heading</h1>
<p>Hello</p>
and read it in with this:
$html = include('text.html);
You can try doing that, but as soon as the include() scans the file, the output will be sent to the web server. That's how PHP works. Since there is no explicit return value, moreover, the $html variable comes back empty.
Thus, we use the output buffering functions to modify PHP's behaviour: we start buffering, and then we fetch it and clear it. This is then not sent to Apache, unless of course you choose to turn off output buffering and echo what you have collected, in which case it goes back into the normal output buffer and is actually sent.
I have an HTML file that has a rather long navigation menu inside of it. I want to take that menu out of the HTML and place it into an external PHP page and then call it with
<?php include 'navigation.php'; ?> in the HTML file.
I have tried just adding this into the HTML file but it doesn't display anything as well as no errors on the page.
What do I need to do (if it's even possible) to keep the files HTML and use the php require function?
Add this in in your httpd.conf and then you can process PHP code on HTML pages
AddHandler application/x-httpd-php .php .html
Q: Did you give the page a .php suffix? That should be all you need to do.
Remember the way PHP works - you basically "embed" your PHP code in an HTML page, and the server executes the PHP before it serves (the rest of) the HTML.
But in order for PHP to "see" your code, you need to make sure your "HTML page" has a .php suffix.
As a crude workaround, you can add ".html" to the list of file suffixes that PHP will parse.
But this could cause other things to break.
If you want to embed PHP code in your "index.html", the best, cleanest approach is to simply rename it "index.php".
IMHO...
I have two files:
html
php
where html file is calling the php file to do something.
Now i would like to get the PHP session ID (php code) into both my html file as well as my php file.
I would like to know how would I do so so that both html file and php file have the same PHP session ID.
Thanks for your help in advance.
You can't have PHP sessions in HTML files. Best to just change the HTML file to a PHP file.
As mentioned in the comments - make sure you start the session at very top of the file before any spaces before the opening php tags.
To retrieve the session ID use php function session_id() but if the both files are on same domain you just need to call session_start at the very top and it'll just use same session across.
EDIT
To answer your qs in comments below -
Yes, a PHP file can just have HTML code but no PHP code at all or add PHP where required. e.g.
Myfile.php
<html>
<head> ... </head>
<body>
<h1>some title</h1>
......
......
Go to next page (<?php echo $_GET['next_page']; ?>)
......
</body>
</html
so you just open and close php tags where you needed php stuff to go.
To print session ID - just use somewhere in the HTML of PHP file.
Yes, session_id() will give you the same session ID in php2 file - again, make sure you call session_start function at the very top in the php2 file too.
If you have access to your apache configuration, or a simple .htaccess file, you can tell Apache to handle php code inside of an .html file. You can do this by creating an .htaccess file (remember the . (dot) as the first character in that filename) on the document root of the site (probably public_html/) and putting this into it:
# Add this to public_html/.htaccess file
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
You should be able to reload the html page and your PHP code will run great.