سؤال

I have a problem that I have failed to solve.

Within my index.php, I am clicking on an anchor and calling a javascript function to display a lightbox that contains a form to capture user registration details. i.e:

<a href="javascript:loginLightBox();">register</a>

The div that contains the form:

<div class="userRegistration">
 <form name="userReg" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
 <label class="regLabel">Email address</label>
 <input type="text" name="userEmail" id="userEmail" class="regInput"/>
 <label class="regLabel">Password</label>
 <input type="text" name="userPassword" id="userPassword" class="regInput"/>

 <input type="hidden" name="userReg" value="1"/>  <!-- Note 1 -->
 <input type="submit" class="Button" value="Register" />
</form>

since this div is contained in index.php, you can see that the action for the form resubmits index.php, so I have php code that tests whether $_POST['userReg'] == 1 (see Note 1 comment in form above), i.e.

<?php
   if ($_POST['userReg'] == "1") {
       process the form, e.g check if userid already in database, etc etc.....

   }
?>

The problem I have is, if I refresh the page, since $_POST['userReg'] is in the user agent/web browser, and has been set to 1, the code will always try to reprocess the form.

I tried to unset($_POST['userReg']), but that will not work since the web browser resets it, thus once I submit the form, all subsequenct form refreshes will always get the lightbox redisplayed even when the user has registered, so my test for $_POST['userReg'] == 1 is useless, it will always be true.

The main difficulty is I would like to use just the main page(index.php) for user registration in a small corner of the page and avoid annoying the user with many pages to navigate to.

I have no AJAX knowledge in case that might be one way to solve this.

Others have suggested a concept known as POST-REDIRECT_GET, however this seems to redirect to a second page and I don't want to do this. Is there a way I can fix this problem.

Thanks for taking time to read my problem.

هل كانت مفيدة؟

المحلول

A post variable is not in the User Agent, thats a string to identify the browser ;)

When you refresh the page after a submit, the browser (at least chrome) asks you if you want to resubmit the post data and then does the exact same request again including your post data.

If you really want to work arround this (and you can't check if the user is logged in or something like this) use a session variable:

<?php
session_start(); // call this at the top of your document

if ($_POST['userReg'] == "1" && !isset($_SESSION["reg"])) {
  $_SESSION["reg"] = true;
  // process the form, e.g check if userid already in database, etc etc.....
}
?>

A redirect may also work:

<?php
// this must be at the top of your document 

if ($_POST['userReg'] == "1") {
  // process the form, e.g check if userid already in database, etc etc.....
  header("Location: index.php?registration=".$status); 
  // use if($_GET["registration"] == "...") to give feedback
  // close db connection etc 
  die(); // and exit, the user is redirected

}
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top