Show notification window when submitting a form

So, I have these two pages: pageOne.php and pageTwo.php . The form is in pageOne.php :

 <form method="post" action="pageTwo.php"> .... </form> 

and after completing the entire build-data validation and sending emails in pageTwo.php (the reason I do everything on two separate pages is to avoid re-sending the data when the page is refreshed ... it was the easiest way to deal with this problem). So far, everything is working fine.

Now I want to display a success / failure message using the notification field after submitting the form, and have tried several things without any luck. For instance. when I tried THIS solution on pageTwo.php , the popup does not appear and I think that since I have this header on top of this page

 <?php header("Location: http://TestPages.com/pageOne.php"); ?> <?php if( $_POST ) { //collect the data //insert the data into DB //send out the mails IFF the data insertion works echo "<script type='text/javascript'>alert('It worked!')</script>"; }else echo "<script type='text/javascript'>alert('Did NOT work')</script>"; ?> 

And when you tried this second solution in pageOne.php , I get a warning window that appears every time I refresh the page and get an error message, even if the data was inserted into the database and the letters were sent. pageOne.php :

 <html> <body> <?php if( $GLOBALS["posted"]) //if($posted) echo "<script type='text/javascript'>alert('It worked!')</script>"; else echo "<script type='text/javascript'>alert('Did NOT work')</script>"; ?> <form method="post" action="pageTwo.php"> .... </form> </body> 

and pageTwo.php :

 <?php header("Location: http://TestPages.com/pageOne.php"); ?> <?php $posted = false; if( $_POST ) { $posted = true; //collect the data //insert the data into DB //send out the mails IFF the data insertion works } ?> 

Why is this simple thing not working :( is there any simple way to fix it? Thanks!

UPDATE

So, I made some changes according to drrcknlsn sugession, and this is what I still have .... pageOne.php :

  <?php session_start(); if (isset($_SESSION['posted']) && $_SESSION['posted']) { unset($_SESSION['posted']); // the form was posted - do something here echo "<script type='text/javascript'>alert('It worked!')</script>"; } else echo "<script type='text/javascript'>alert('Did NOT work')</script>"; ?> <html> <body> <form method="post" action="pageTwo.php"> .... </form> </body> </html> 

and pageTwo.php :

 <?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION['posted'] = true; //collect the data //insert the data into DB //send out the mails IFF the data insertion works header('Location: http://TestPages.com/pageOne.php'); exit; } ?> 

The page redirection and success message now works with these changes, but I get an error message every time I open / refresh the page (I know because the session key is not set yet) ... how can I avoid this? Thanks again!!

+4
source share
2 answers

First, the pair indicates:

  • Variables (even global ones) are not shared between requests , as you are trying to do in the bottom example. In order for $posted be available on both pages, you must somehow save it. This usually involves setting a session variable (for example, $_SESSION['posted'] = true; ), but it can also be stored in a cookie, in a database, in the file system, in the cache, etc.

  • Use something like if ($_SERVER['REQUEST_METHOD'] === 'POST') instead of if ($_POST) . . While the latter is probably safe in most cases, it is better to get used to using the former, as there is where $_POST may be empty with a valid POST request, and this can be a difficult bug to track.

One potential sample to solve your problem using the tip above:

pageOne.php:

 <?php session_start(); if (isset($_SESSION['posted']) && $_SESSION['posted']) { unset($_SESSION['posted']); // the form was posted - do something here } ?> ... <form>...</form> 

pageTwo.php:

 <?php session_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_SESSION['posted'] = true; // do form processing stuff here header('Location: pageOne.php'); exit; } // show an error page here (users shouldn't ever see it, unless they're snooping around) 
0
source

This seems to be a problem with the area. Application:

 global $posted = true; 

http://php.net/manual/en/language.variables.scope.php

-1
source

Source: https://habr.com/ru/post/1486908/


All Articles