Log in

View Full Version : Getting Span Contents



aztech4mac
10-13-2012, 06:27 PM
Hi All,

I need to get the contents of a span object using PHP. This span resides two levels up (parent.parent). I haven't been able to find an example anywhere and my clumsy attempts have failed.

is there a PHP counterpart for:


var data = document.getElementById('hud2').innerHTML;

james438
10-13-2012, 06:40 PM
Could you post an example? I think I know what you are talking about and this would require PCRE to retrieve the data, but I want to see an example to be sure.

djr33
10-13-2012, 07:02 PM
I don't really follow this request either. As far as I can tell, it's completely impossible. HTML doesn't contain "objects" for PHP-- instead, it just generates text. Javascript, on the other hand, operates within the HTML page and treats objects on the page as objects/things.

You can ask PHP to take some HTML text and look at it like that, but not while it's generating the page, at least not easily.

If you're trying to use PHP to parse another page, that would be where this could apply potentially; but if you're trying to do this while you echo/print text to the browser, it just doesn't work like that.

I'm sure there's some way to do what you're trying to do, but from your post it sounds like you need to understand the fundamentals of PHP a little more before attempting it-- if you can post some examples here that will help. What James said may help too; but I'm just not sure yet.

aztech4mac
10-13-2012, 08:01 PM
hey thanks guys,

I thought i was clear, but I guess not. Ok, 'object' was the wrong thing to use, but I was thinking along the lines of javascript, which does.

So, here is what I have...

A web page with a span containing the text 'x of y' (where x and y are integers as in 3 of 12).

The same page has an iFrame.

Inside that iFrames web page is another iFrame. Or nested iFrames...

The innermost iFrame is generated by PHP and I need the contents of the span in the outermost page ('x of y').

Simply put; is there a counterpart for 'document.getElementById('span id').innerHTML;'? Or in my case, 'parent.parent.document.getElementById('span id').innerHTML;'. This does work in JS.

I do understand the difference in PHP and JS and I can't pass the value because of the intermediate page. My last resort may be using a session var, which I'm trying to avoid for no particular reason.

djr33
10-13-2012, 08:21 PM
Simply put; is there a counterpart for 'document.getElementById('span id').innerHTML;'? Or in my case, 'parent.parent.document.getElementById('span id').innerHTML;'. This does work in JS.That's logically impossible in PHP, because PHP doesn't work like that. There's no way to even conceptualize "parent" in PHP, because PHP doesn't work that way.

You can find a way using string functions or some sort of HTML parser to take HTML, extract an element, then tag out the text from the middle of it, but that first requires getting the full text of that page within PHP.

I'm still not sure I follow the situation-- you can't access an iframe using PHP, because again that's just not something that exists in PHP.

If you can use a session var, that's a reasonable way to do. There may be other ways, but how would it look with sessions? And why do you want to avoid it? Maybe we can help to find another way.

aztech4mac
10-13-2012, 10:39 PM
Thanks again,

Yeah, I wasn't aware of any way, but that did mean there wasn't, so I thought I'd ask. I try to avoid too many Session vars, just so I don't abuse them. One more won't hurt...

traq
10-14-2012, 03:17 AM
You can use something like the DOMDocument (http://php.net/domdocument) class, but it's fairly complicated. You'd probably be better off using javascript (on the client side) to get the contents, and then passing them to php (on the server) directly.

djr33
10-14-2012, 03:29 AM
Here's what I'm confused about: how do you get it into the session var in the first place? If you already can do that, I don't see what the problem is. What you posted above (the JS example) looks like a way to get data. If that's not the problem, then post more information about what you're trying to do with it.

Traq, that works, but only when dealing with HTML inside a string variable; I'm confused at this point about whether that's the situation or not. You can't use those functions to look at the parent of the last-created HTML element via PHP, while PHP is generating the HTML. That's what I'm failing to understand here.

traq
10-14-2012, 03:39 AM
A web page with a span containing the text 'x of y' (where x and y are integers as in 3 of 12).

The same page has an iFrame.

Inside that iFrames web page is another iFrame. Or nested iFrames...

The innermost iFrame is generated by PHP and I need the contents of the span in the outermost page ('x of y').Are you trying to get the value from the "outermost" page and put it in the "innermost" page?

Get the span value using javascript, then put it in a query string in the url of the iframe in question:
var data = document.getElementById('hud2').innerHTML;
document.getElementById( 'myiframe' ).src = 'http://example.com/somepage.php?data=' + data;it's far more complicated because you have several nested iframes, but I think it can still be done. Just not sure how.

var iframe = document.getElementById('iframeId');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;I haven't tried it, but there's your concept.
You'll still need to pass the data to the PHP script that generates the page in that iframe (obviously, *before* it generates it, so you'll probably need to use javascript to create the iframe in the first place).


Here's what I'm confused about: how do you get it into the session var in the first place? If you already can do that, I don't see what the problem is. right - in any case, if you can do that, do do that (hahaha, I said do do) - that's what sessions are for, passing data between pages.


Traq, that works, but only when dealing with HTML inside a string variable; I'm confused at this point about whether that's the situation or not. You can't use those functions to look at the parent of the last-created HTML element via PHP, while PHP is generating the HTML. That's what I'm failing to understand here.
actually, I'm not quite understanding that either, now that I re-read it...? but you're absolutely right

jscheuer1
10-14-2012, 07:21 AM
You actually can use javascript if the pages are all on the same domain. On the top page:


<span id="xofydata">5 of 7</span>

On the nested page, regardless of how deeply it's nested:


<span id="xofydata"></span>
<script type="text/javascript">
document.getElementById('xofydata').innerHTML = top.document.getElementById('xofydata').innerHTML;
</script>

Untested, should work.

I've since tested this and it works fine.