Skip to content
Advertisement

Rendering conditional HTML elements with PHP

I’m trying to learn PHP to solve an Assesment challenge, where I have to build a Product List & Add Product pages. Reading here and there, watching some tutorials I was able to develop this code so far:

Index.php:

JavaScript

Thing is, inputs for DVD Size, BOOK Weight and Forniture dimensions ( H, W & L ) should be render depending User selection on #prod_type. Currently those inputs show up after the User choose a select option, and hit Add Product button ( related to POST method I pressume, is the closest that i get )

Advertisement

Answer

PHP runs entirely on the server. If you want things to happen in the browser when your user interacts with your webpage (without performing a round-trip to the server), Javascript is your friend.

In this case, I would give each input a data attribute to say which product type they are relevant for. Then you can write a Javascript function to handle changes to the prod_type field and show/hide the correct fields.

For example:

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