Skip to content
Advertisement

Delete html elements with php

I was wondering if I had an HTML page, how would I delete an element with php, by the name or id of it. so I could have HTML code like this

<div>
    <span id='todelete' name='todelete'>This should be deleted</span>
    <?php
       delete('todelete');
    ?>
</div>

or something like that, and the web browser would just show an empty div without the span. Is that possible, or is it more useful to just use javascript for this.

Basically, what I’m saying is before the page is compiled, can you delete elements from it, based on some conditionals, not after the page has loaded

Advertisement

Answer

It appears you are getting confused between server side and client side scripting languages.

PHP is a server side language meaning that once it has been compiled that’s it, it can’t be changed dynamically in the users browser.

JavaScript on the other hand is a client side scripting language and can manipulate what is on the page dynamically.

Therefore what you are asking to do is impossible using pure PHP once the page has been rendered.

You can use AJAX technology to call a PHP script from within a page already in a browser using JavaScript. You can then use the PHP script being called by the AJAX request to return something that you can use to update a particular section of your page, but there is no pure PHP way that doesn’t involve JavaScript at all to do this.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement