Skip to content

PHP go to another page and getElementById

I have 2 pages

  1. HTML page with a form
  2. PHP action page for the form

Based on this, if everything is successful with the form submission, I want to go back to the first page and open up a modal using document.getElementById.

I know to do this, I would need the header(“Location: blabla”) function in my PHP page to go back to the first page, but how am I supposed to open up the modal after getting to that page?

Any help would be appreciated, thank you very much.

Answer

It is better to use ajax in this case, yet if you have to do it this way you can use the query prams, for example you redirect the user to your html page like this

header("Location: blabla.html?m=some message to show") 

then in your html page, you need to check if this param exists then display the modal like this

searchParams = new URLSearchParams(window.location.search);
if (searchParams.has('m')) { // searchParams.get('m') then open the modal or do what ever you want. }

edit: you can use any other param instead of the message, it could be what modal to show, or whatever