document.getElementById for PHP

I’m more familiar with JavaScript than I am with PHP. That’s the reason for the framing in the title.

I’m working on a WordPress site. As far as I can tell, it doesn’t really have any XML which I see several tutorials reference. So, what I would like to know is how to select an HTML element by its id and then echo the result without referencing any XML. Thank you.

As PHP runs on the server rather than the client, I would imagine that you would need to extract the information you need while you are drawing the HTML.

Can you expand a bit more on the question?

1 Like

Hey droopsnoot,
Thanks for responding. Below, you should see a screenshot that I included. I’m on a dual screen setup for work, so I made it a point to make the inspector view of the elements super large.

In the screenshot, you’ll notice that the select box is highlighted and it has an id. What I’m seeing help with is, for example, selecting that select box with PHP exclusively. Let me know if there’s something else I need to clarify. Thank you.

It’s still not clear what your intention is.
It is possible for PHP to read an HTML document and pick elements within it based on things like ID and other attributes.
But what do you expect to do with the element once selected?
The above is about reading HTML which has already been written. Unlike JS, PHP can’t alter what’s already written, not without re-writing it.

Hey Sam,
Good to know. I wasn’t aware of that. So, this is what I’m trying to do. Certain products might have a certain option of a select field disabled. If the PHP function is targeting that option to be disabled, when a user selects it, the option will be reset to “Select an option…”

I know how to do it with JavaScript. Trying to figure out how to do that with PHP.

As I said, PHP can’t change what is already written, but it is the usual job of PHP to write the HTML to the page in the first place. So when it is doing that it can write it any way you choose.
When PHP is populating a list of options in a select input, if one option should be disabled, you can add the disabled attribute when you initially write it to the page.

But this sounds a lot like user interaction. Maybe I need to reiterate what has been said about PHP being a server-side language, all of the working out is done in the server before it is sent to the client (browser) and by that time it is set in stone, so to speak. Unlike javascript which is client-side, so things can change in the browser and react to user interactions.
So it may be that this is something you should be doing using js.

1 Like

Okay. Sounds good. Thanks for the reiteration and clarification.

The other Sam has already explained things well but perhaps a little clarification will help.

The way that PHP works is that the compiler reads the PHP file from beginning to end. It generally writes the HTML out as-is and processes the PHP code when it is encountered. Whatever HTML is generated by the PHP code is written and the compiler proceeds from there. It is not clear what you need to do but you just need to figure out a different design depending on what you need to do.

If you need to create some form of interaction where something happens in the client after the page has been sent then you either need to do that using JavaScript or you need to do a post-back. In a post-back the entire page is processed again but your PHP will do something different; there are many articles about that.

If you need to do something during the initial processing of the page where the code needs to refer to HTML elsewhere in the final document then it is a matter of re-designing how you do that. The JavaScript way of doing it will not work.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.