Question about reloading form

Haven’t done this for a number of years…

If I submit an HTML form (PHP script) and the form has errors on it, so my script ends up reloading the form/page, is it correct that the page will reload with things being scrolled up to the top of the page?

Yes, that’s correct.

If your PHP code is responsible for drawing the form, you could stick a bit of code in to set the focus to the first field that has a problem when you re-draw it.

Would there ever be a case where that isn’t true?

What if the HTML form is super long and scrolls down past the viewport?

Could reloading the PHP page leave you scrolled down the page?

No. Handling a form is the same as clicking a link to another page. It starts at the top.

I suppose you could indeed use focus on input elements, or anchors to return to a different spot if you wanted to.

@rpkamp,

The reason I ask is because I am building a checkout form that extends beyond the viewport. And if the user forgets to complete a field or enters the wrong information, I will need reload the webpage telling them there were problems, and I just wasn’t sure if the form/page might scroll down to where the issue is at if that makes sense?

In fact this leads to another question - not sure if it can be answered here - but I believe it is considered a better form design to display all of the form errors at the top of the page, right? (As opposed to maybe having an error next to each suspect field.)

If that is correct, I wanted to have a box at the top of my page shwing the user what they got wrong, and that is why I want to be sure if I reload the form/page thatit starts at the top so they see the issues.

Hope that makes sense?

If I were filling in the form, I would just want it to be clear where I had gone wrong. If you put the box with the errors in at the top, make sure that is where the focus is when the page re-loads. If you put the messages alongside the fields where the error(s) occur, then focus on the first one of them on re-load. If you’re putting them at the top, you might provide clickable links directly to the fields where the errors are.

That’s more a UX question, not a PHP question.

As for showing error there is a lot you can do nowadays with HTML5 form validation. You should Google it :slightly_smiling_face:

1 Like