Include a php file with a lot of includes using ajax .load() - php

I'm trying to load an php file through ajax using load(). Here's my code:
var loadUrl = "myfile.php";
$("#lst-results").load(loadUrl);
So, when I do this, it works:
<?php
$test = 'Hi :3';
?>
<h3>Hi</h3>
<h3><?php echo $test ?></h3>
But I need to include a file that contain a lot of includes and functions using a lot of different classes. I've tryed to include this file and use it as a function like this, but does not work and Nothing appears int the load() results. This function return an array.
<?php
include 'path/myfile.php';
$result = myFunction();
?>
I can't figure out how to solve it. :(

You need to "echo" the $result.
<?php
include 'path/myfile.php';
$result = myFunction();
echo $result;

Related

insert content into Template through Variable

I want to use my index.php page as my template for all my other pages. So I'm printing it out with the code below.
echo file_get_contents("index.php");
I've added this piece of code into the template (index.php) where i want to display the contents. of whichever page im on.
<?php
echo $index_content;
?>
So when I use
echo file_get_contents("index.php");
to get my page template, on for example users.php. In the users.php file I want to use the code below
$index_content = echo "string";
to then print out my page contents where I added this variable
<?php
echo $index_content;
?>
My problem is when I say $index_contents = echo ("string");
it's not printing anything out. onto my template. or it prints the stuff out but at the end or the beginning of the template. not where i've inserted my variable. Why wont it echo out my stuff where I've inserted my variable.
file_get_contents() give you the source of your file.
If I get you right you want to use include instead. Also don't echo in a variable but assign the value and echo it in the template.
users.php:
$content = 'what ever';
include 'template.php';
other.php:
$content = 'other page';
include 'template.php';
template.php:
echo $content
If you call users.php output will be "what ever". If you call other.php output will be "other page".
You are storing the return value of "echo" in $index_content, which is empty.
Just omit the echo when assigning the string to the variable.
The other problem is, with file_get_contents you don't evaluate the php expression where you echo out the $index_content.
Instead, you should use include('index.php') in users.php, and set the variable $index_contents before that.

PHP - Reading contents of another PHP file without it echoing

I am trying to reach the contents of a PHP file without the file actually outputting what it would usually do. Here is my test code:
File1 (test1.php)
<?php
ob_start();
include_once './test2.php';
$test = ob_get_contents();
echo $test;
?>
and here is file2 (test2.php)
<?php
$testVar = 'Name!';
?>
<div class="testClass"><?php echo $testVar?></div>
<p>Spam2</p>
and I want it to only do this because of the
echo $test
line NOT because the file is outputting the content.
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
due to the echo, but it returns this
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
So how do I get it to only return the content once?
Don't echo $test;. PHP is executing as it should. Since ./test2.php shows Spam in HTML it appears on the page, then you assign the page contents to a variable and echo it. What do you expect?
If you have 2 files say: app/index.php and app/config.php you can just use the return keyword to return some content from the config.php file. And then, when you include the file whatever you returned from config.php can be saved to a variable.
Example:
First return whatever you want from the config.php file (could be an array, string, etc).
<?php
return ['name' => 'Spam'];
Then in the index.php:
<?php
$contents = include_once('config.php');
echo $contents;

Passing variable from PHP to Smarty

I had two script, one in .php and one in .tpl
I need to pass the variable in php to the tpl.
I tried this one, but nothinng works (but somehow
it works for one or two days, and after that,
it showed blank,
if i create another php script just to echo the variable, it works.
PHP Code:
<?php
$usdidr2 = "12610.198648";
$usdidr2 = number_format($usdidr,2,',','.');
echo $usdidr2;
session_start();
$regValue = $usdidr2;
$_SESSION['regUSDIDR1'] = $regValue;
?>
SMARTY Code:
<li>
<a href="example.php"><strong>
{php}
session_start();
$regValue = $_SESSION['regUSDIDR1'];
$regValue2 = number_format(45.99*$regValue,2,',','.');
echo "Rp. ".$regValue."";
print_r($regValue);
{/php}
</strong></a>
</li>
Here is the syntax to send data from php to tpl
$smarty->assign('variable name with which you can access the data in tpl', $php_data_you_want_to_send);
Update:
$smarty->assign('rate',$usdidr2);// you just need to write rate without $
You can access it in smarty like {$rate} if it is string
You can access it in smarty like {$rate|print_r} if it is array
You can use this syntax:
$res = "Hello World!";
$this->context->smarty->assign('result', $res);
And passing to .tpl file like this:
{$result}
Hope this helping you.

PHP & HTML: How to include .php in concatenated PHP string var

Example of what i am trying to do:
<?php
$html_element = '<form><label>country</label><select>'.include_once("country_list.php").'</select></form>';
?>
I have tried the above and the following:
<?php
$html_element = '<form><label>country</label><select><?php include_once("country_list.php"); ?></select></form>';
?>
Of course the "$html_element" is then echoed in some div later. So how can i include the "country_list.php" in this php string variable so that it will pull correctly?
If you want to include the data, no matter which file type it is, do like this:
<?php
$html_element = '<form><label>country</label><select>'.file_get_contents("country_list.php").'</select></form>';
?>

How to use variable defined in one php tag in another php tag?

I wrote a php page which has two php tags and one script tag inside it .
<?php
$value = $_GET['hash'];
?>
<script>
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
echo $cmd;}
?>
}
</script>
I want to use $value inside another php tag ( like above it has the file I want to open ), but I am not able to do it.Is the scope of variable limited to one php tag ? if yes how can I solve this problem Please help
Your code works perfectly. The variables in one PHP tag is accessible from all other tags, unless you define them inside a PHP function.
The reason you are not seeing the echo on the screen is because the echo prints to the Javascript function.
If you view the source of the generated page, the file contents will be there.
Try this:
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
?>
alert( <?php echo $cmd; ?> );
<?php
}
?>
}
execute();
if $value is a get then you don't need to access it as a file, it should just be a short string.
just above line 7 (the one with $readfile = file...
type:
echo "alert(The hash value is: ".$value.")";
This will make an alert display (as it is in a script tag)
p.s you should have in your opening tag

Categories