Reputation: 1200
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?
Upvotes: 1
Views: 17886
Reputation: 5620
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.
Upvotes: 1
Reputation: 32404
PHP generates HTML before sending it to the client.
You need to figure out where in the PHP this HTML is being generated.
Upvotes: 1
Reputation: 57318
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.
Upvotes: 1
Reputation: 66
You can use PHP DOM Extension.
See documentation on extension or direct link getElementById
But, it's backend processing.
Upvotes: 2