How to access a variable in PHP - php

I am a real newbie at PHP and have, I hope, a simple question.
I have found a variable with Firebug in the DOM section and would like to access it to determine if it is true or false. The variable is called isResponsive and it is found under the DOM section of Firebug. I would post a screen capture but the forum won't let me.
In firebug it says:
window > responsiveDesign and then below it lists all of the variables. One of them is call isResponsive and in the column to the right it says either true or false.
Any help in how to access this variable is very much appreciated!!
Thanks in advance,
Doug

You can't, at least not directly.
PHP runs on the server and generates some (mostly) text which it sends to the browser.
The browser parses that text as HTML, JavaScript, CSS, etc and constructs a DOM from it.
The DOM variables exist in the browser, not on the server where PHP runs.
For PHP to get access to them, you would have to use JavaScript (or something else that runs client side and can access the DOM) to serialise the data to strings, and then send them to the server in an HTTP request.

You can't Access DOM vars in php - they are only available over JavaScript. If you want this value the only way is to pass the var via JavaScript to a php script

Related

Get a JavaScript variable from a remote page?

I am currently working on developing an API for a company. Specifically, here's my issue. They have JavaScript arrays on a webpage that their webmaster updates. I have to pull these arrays into either a simple JS script or PHP file and get the contents of these arrays, which I can then arrange according to the API's specifications and output it as JSON.
How do I pull a JavaScript variable in from a remote page in either PHP or jQuery/JS and make it usable for other applications?
No, I don't have access to the company's website. I have to work off of page scraping for this one.
Thank you!
You can't access private javascript variables remotely, due to Same origin policy. They would have to output the arrays in some kind of readable format that you could access using AJAX, probably as JSON.
Edit: As mentioned below, if the array is explicitly defined as text in a javascript file, you could grab the contents of that file using cURL in PHP
If I where you, I’d use PHP to file_get_contents (or CURL depending on the server config) the page and then parse it based on whatever the markers are to find the value of the variable, assuming it’s written out to the page in the first place.

Get HTML ID in PHP

I am new to PHP and I am wondering if I can access an ID inside HTML and do some editing?
Inside my html file, I have this:
<div id="Msg" class="message"></div>
and normally I would edit the text on the client side in javascript using the following:
document.getElementById('Msg').innerHTML = '<font color="red">Some text...</font>';
Can something like this be done on the server side with PHP? I would like to alter the element before I render the page. If so, what should I use to access the ID and edit the elements?
You can use PHP DOM Extension.
See documentation on extension or direct link getElementById
But, it's backend processing.
If you mean altering the HTML via PHP after it's been rendered in the user's browser, that can't be done.
PHP is server-side, which means it runs on a machine different from the user.
Javascript and HTML are client-side, which means they run on the user's machine.
The server should have no control over anything on the user's machine.
Therefore, accessing the ID from PHP is not possible. :)
You've got to stick with Javascript, which only runs client-side.
If you want to alter the HTML structure before you send to the client, that's possible using the DOM, as #DmitryBergstein points out in his answer.
If you need to access something on the server after the page has been rendered, you need to make an ajax request, then alter the DOM using Javascript after the request has returned some data.
PHP generates HTML before sending it to the client.
You need to figure out where in the PHP this HTML is being generated.
If you really want to update an innerHTML content with PHP once the page is fully loaded, you should have a php script that returns an updated value. Then, you call this script and update your page according to the response. It can be done nicely with an ajax call.
$message = htmlspecialchars($_POST["Msg"]);

Get contents of DOM via PHP

I need to get the contents of a website through PHP, however, the content is only available when JavaScript is enabled. The workaround that I am using now is making an applescript to open the website in Safari, and selecting all of the page content, copying it to the clipboard, and pasting it.
That will be really hard to achieve I guess. If you observe the JS on that page that is responsible for getting the content ready, you may discover its just another AJAX call that you may be able to call directly from your PHP script.
best possible solution: ask the website owner for api/export access ;)
If that is not possible, you can only pray that you can analyze the requests that are initialized via JavaScript and imitate them.
(possible tools: firefox with firebug or tamper data plugin).
Warning the owner of the website might not like this approach, in fact, it may be disallowed to scrape the data automatically
What do you mean by:
the content is only available when JavaScript is enabled
Does the page pull data from somewhere via JS? Would it be easier to analyse where the data is coming from and access that place directly from PHP?

PHP and JQuery Question

Is there a way I can pass a PHP variable from a PHP page to a JQuery page and back to a PHP page?
Maybe I'm wrong but I believe you may be confusing things.
Server Generates page using PHP.
Generated page is composed of HTML, JAVASCRIPT whatever...
Client receives page
Received page is interpreted (JavaScript code is run)
In the end what you are asking for is possible but, by the way you put the question, I though that the above should be clarified.
How to do it?
Say you want to pass id=123 from server to client and then back.
generate page with a tag, say <span id="js-val">123</span>
have client read the contents of id="js-val
client can then resend the 123 using POST or GET that really depends on what you want.
Hope it helps to clear things up.

Send a Variable to an IFrame's Parent in PHP?

How can I transfer an array from an IFrame running a php script, to the parent PHP of the IFrame?
I have an Array of strings in PHP that need to be transferred to the main window so that when the iframe changes pages, the array is still stored.
I could use a cookie for this, though I'm not sure that would be the best idea.
Thanks.
you can't do that in php. iframe is like a new browser window, so they are separate requests. separate requests can not transfer data between each other in a server side language.
if you give some detail as to what you're trying to accomplish, there may be another way around the issue that someone can suggest.
Like Tim Hoolihan said, PHP is a server side language that is only responsible for generating the HTML before it is sent to the browser. Meaning once the page shows up in your browser window, PHP has done it's part.
However, with that said, if you control both the "parent" page and the page being iframed, you can json_encode the array in the page being iframed and set it to Javascript variable, then on load pass it to a Javascript function on the parent page (assuming not violating any browser/domain sandbox constraints). At that point you can do whatever you want with it.
Take a look at jQuery for your core Javascript/Ajax needs.
if you control the iframe, you can save the array in a session variable and make the parent do an asynchronous call to retrieve the array from session.
however Jordan S. Jones solution with only javascript works as well

Categories