Skip to content
Advertisement

How to stop the form input type select data from erasing after PHP form validation?

I’m working on an e-commerce website, and I’m trying to validate a form using PHP. So with my current code, the data doesn’t get erased after I submit the form and if there is an error, all the data stays in the form and shows an error message, but I cannot make the same for the select options. What am I missing here?

JavaScript

Advertisement

Answer

Instead of echoing the value into the input, you need to echo selected on the correct option. It should look something like this.

Also, valid HTML will only allow one option in a select to have the selected attribute. Your default option (<option disabled selected>Select Month</option>) might be causing a problem.

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