I have a variable set in my main file (main.php), and need the second file (uploads.php) to reference the variable as it is set in the first file. It is returning undefined right now tho.
The second file is loaded with $.load into the first file: code example below -
Main.php Contents:
<?php $accountName = get_option('account_name'); ?>
<div id="uploads"></div>
Load Your Playlist
function loadUploadsFile() {
jQuery('#uploads').load('uploads.php');
}
Uploads.php file contents
<?php echo $account_name; ?> <== returns undefined
$url = 'http://www.somewebsite.com/' . $accountName . '/page/'
/* more code below running a query/loop etc. */
As you may be able to tell, I want Uploads.php to reference the variable decleration in Main.php but is is not pulling the value, it is just returning undefined. Uploads.php loads into the uploads div, but without the account name set the content is just blank.
Would I need to pass the variable to Uploads.php through ajax? I've tried session variables but couldn't get that to work. I was trying an ajax request but I am new to it so couldn't get that nailed. Any help would be great.
Session variables should work after all. Make sure you have the following:
In Main.php
session_start();
$_SESSION["account_name"] = "test";
In Uploads.php
echo $_SESSION["account_name"];
You could pass it as a GET parameter in your jQuery call like:
jQuery('#uploads').load('uploads.php?account=<?php echo($accountName);?>');
And then in your uploads.php file get it from the request like:
$accountName = $_GET['account'];
Some other options are storing it in the session, setting a cookie, or using jQuery .post to send it as POST data.
Since you are doing jQuery, you might want to look at http://api.jquery.com/jQuery.post/.
Your code should be something like (not sure if exactly)
$.post('uploads.php', { account_name: "<?= get_option('account_name') ?>" }, function(data) {
$('.result').html(data);
});
And then you can use this variable in your uploads.php by referencing $_POST['account_name'].
PS: I assume you are having PHP 5.4x
Save the account name in a session
Main.php
<?php
session_start();
$accountName = get_option('account_name');
$_SESSION['accountName'] = $accountName;
?>
Uploads.php
<?php echo $_SESSION['accountName']; ?>
Related
I have a problem with the following code. How do i put 2 included files into 1 line include?
I already tried it like the code below but nothing is shown (blank page). It should show the url.
Url: theme/default/main/index.php (it would be)
application-sql-realtime.php = (should show 'default' or anything else when user changing their themes template it connect to sql)
<?php include('./theme/<?php include_once("config/application-sql-realtime.php");?>/main/index.php');?>
Refer image for code
include_one returns boolean
Maybe can be like this:
<?php
$theme = exec("php config/application-sql-realtime.php");
include("./theme/{$theme}/main/index.php");
?>
But I think you better put it in some class/function
<?php
// inside application-sql-realtime.php you declare a function to return theme name eg: getThemeName
include("config/application-sql-realtime.php");
$theme = getThemeName();
include("./theme/{$theme}/main/index.php");
?>
Make a class with a static or non static method to retrive the value you need (e.g. 'default'). Then you can implement this value in your string to include the first file. Following an example of a static method call:
<?php
$val = EgClass::getPathPart();
include('./theme/'.$val.'/main/index.php');
?>
I have three files in a server (000webhost.com):
"Test01.php" (main file),
"database.txt" (saved data, which will be changed by users),
"save_txt.php" (the file which gets data from the main file and writes it to the "database".
"Test01.php" is supposed to show a simple list, with a few names in a table (single column, multiple lines).
Those names will be retrieved from a file named "database.txt".
Everytime some user click on a name, that name will be sent to the bottom of the list, and the list will be saved to "database.txt", so the next user will see the changes made by the last one.
A function in "Test01.php" sends the changed list to a second file ("save_txt.php"), which is supposed to write it back to "database.txt".
I can manage to retrieve the data from the txt file, and the clicking events as well, but I still can't find a way to save the data into the txt file...
In fact, I don't understand why my variable isn't seen from inside the second php file ("save_txt.php").
To retrieve data I use:
<?php
$Data_from_File = file("database.txt",FILE_IGNORE_NEW_LINES);
?>
And the script:
var sSaved_Data = <?php echo json_encode($Data_from_File); ?>;
The listing stuff works fine.
I get many names from the txt file and store it into an array. Then I display it in the table. No problem from reading the file.
I send data to "save_txt.php" by doing this:
var sNew_Data = " is blue";
.
.
.
xmlhttp.send("php_Data_to_Save=" + sNew_Data);
But, could anyone tell me why the simple code below doesn't work?
https://rbonphp.000webhostapp.com/Test01.php
"save_txt.php" is just like this:
<?php
$var1 = $_POST["php_Data_to_Save"];
echo $var1;
?>
In time: in this example I just want to see " is blue" echoed in the screen (no matter where). I just want to understand how to get the data back to "save_txt.php".
Later I will try to write $var1 to "database.txt".
But first things first...
:-(
As I said, this "Test01.php" is just a test. The list and all the clicking events I wrote in another file. That part works just fine.
* Edited *
Let's try to put it all in a few lines.
The main file (Test01.php) does:
var sNew_Data = " is blue";
// there's more code for the XMLHttpRequest function
xmlhttp.send("php_Data_to_Save=" + sNew_Data);
The secondary file (save_txt.php) does:
<?php
$var1 = $_POST["php_Data_to_Save"];
echo $var1;
?> // and this is ALL its code, just these 4 lines.
That line echo $var1; should simply show " is blue" on the screen.
Right???
Extra info: Test01.php is a step prior to make the following page to work:
https://rbonphp.000webhostapp.com/DailyTasks1.php
Your code is working.
Calling Send_Data_to_Server() returns "The sky is blue" which is what you wanted, as the sent data is var sNew_Data = sSaved_Data + " is blue"; and it is correctly echoed by save_txt.php
Note: your commented jquery ajax call is wrong however, that's not how you define the sent data, check the first example at http://api.jquery.com/jquery.ajax/
You'd write this:
$.ajax({
url: 'save_txt.php',
data: { php_Data_to_Save : sNew_Data },
type: 'POST'
});
first, you didnt call Send_Data_to_Server() anywhere, you've just declared it.
secont, you just send the request to server, but don't store the answer anywhere.
I need to set a dynamic id for every document load. This ID should be set in PHP. Now, when i set a session_start(), the php code would be executed twice because the ID-code in generated document source differs from the same code in the alerted script variable. There are no runtime errors.
What's wrong with this code and how can i prevent the session to re-execute my code after session_start() ?
And when the code would be executed twice (var idvar contains a different value) why the heck is alert executed only once?
I simplified the script so you can try for your self:
<?php
// Session start generates two different ID's
session_start();
// Create ID
$time = microtime(1);
$parts = explode('.', (string)$time);
$idvar = strtoupper(strrev(dechex($parts[0]) . dechex($parts[1]))) . dechex(rand());
?>
<script>
// Contains a new generated ID after session_start()
var IDvr = '<?php print $idvr; ?>';
alert(IDvr);
</script>
Screenshots:
Try move the id initialization just before session_start() i suspect it related on how things executed in php after session_start().
Edit
still unable to reproduce it
I found this:
PHP Session storing behavior into variables?
But even when i add a favicon to the page it don't works. Also the page is standalone and not included as require in another page.
Even this is not working!
// Session start generates two different ID's
if(!isset($_SESSION) && !isset($idvr)) {
session_start();
// Create ID
$time = microtime(1);
$parts = explode('.', (string)$time);
$idvr = strtoupper(strrev(dechex($parts[0]) . dechex($parts[1]))) . dechex(rand());
}
Update:
Now corrected:
With view-source:pgname and session initialized the code would be executed again in source view!
I am trying to integrate two pieces of code together. The existing code already generates a hash code and the function is called with this URL
header(Location: http://".$_SERVER['HTTP_HOST']."/subfolder/controller.php?document&validate&patient_id=".$pid."&document_id=".$nid."&process=true");
Is there another way to execute this function without doing a header redirect because the header redirect is causing the code to halt processing upon redirect.
In the controller file there is only one line echo Controller::act($_GET);
I tried to convert it to a function. I tried.
include_once controller.php //class file
function hash_tag($pid, $nid){
$filetag = "document&validate&patient_id=".$pid."&document_id=".$nid."&process=true";
echo Controller::act($filetag);
}
hashtag($pid, $nid);
Any help would be greatly appreciated.
Code for the Controller.class.php file can be seen here
https://github.com/juggernautsei/Drag-N-Drop/blob/master/library/classes/Controller.class.php
You didn't post your code inside controller.php but considering that your first approach was accessing that code via url parameters, I'll assume that you are executing variables in that code as GET variables (example: $_GET['patient_id']).
If that is the case, now that you are executing that code via include_once you have to change the way you set your variables in controller.php because there is not more $_GET.
You are trying to pass the string from the GET request to the controller.
But Contract::act() works on an array ($_GET is a superglobal array).
Build an array inside the function and assign the parameters of the function to it, then pass it to the controller, like so:
include_once 'controller.php';
function hash_tag($pid, $nid)
{
$array = array();
$array['document'] = 1;
$array['validate'] = 1;
$array['patient_id'] = $pid:
$array['document_id'] = $nid;
$array['process'] = 1;
echo Controller::act($array);
}
hashtag($pid, $nid);
I begin with vars.php:
<?php
include('goo.php');
$googl = new goo_gl('http://myurl.info?=sdfdsfs'.uniqid());
$url1 = $googl->result();
$link=$url1;
$message=$msgarray[rand(0,count($msgarray)-1)];
$picture="http://img6.imageshack.us/img6/4540/7.jpg";
?>
I want to feel http://mmyurl.info?=sdfdsfs'.uniqid() into the goo.gl api to spit out a shortened url
and then use this information in vars.php which is in the header''
this info is then used on another page where $link is called, but i can never get it to work properly
You should give a query string variable a name first:
$googl = new goo_gl('http://myurl.info?myvar=sdfdsfs'.uniqid());
Now you can access value of myvar via $_GET['myvar'].