0
votes

I have a registration.php page and another registration_checker_page.php.

If username and / or email is taken after they submit, then it'll redirect them back to the previous (registration) page.

But then their filled in data is lost. Is there a way to redirect them without clearing their data?

if(mysql_num_rows($queryUser) > 0){
   echo '<p><div id="redirect"/></p>';
   }

function  redir {
code ... location.href = "registration.php";
}

Edit: By filled data it is first name, username, postcode etc. But other information like password etc will be removed.

4
Just store the values in their session and then repopulate the form if those values exist - John Conde
If I open a session for registration, and if they successfully register then what happens to the login session I have? Wouldn't it conflict? PS it creates a login session after they've logged in* - Martynogea
Sessions can be created and edited at any time. Like any array, you can remove the values after they login. Or any time you want. You can also have different keys for different areas of your site to keep values from conflicting. - John Conde
Why are you using to separate scripts for this anyway? Use one script to display the form and do the validation – then you can output the form with the already entered and send data pre-filled as many times as validation fails. - CBroe
John Conde Oh right. Cheers. @CBroe You recommend me deleting the registration_checker php, and move it to the register.php? I do have form validation with JS on the register php, but it's only for reg exp checks. - Martynogea

4 Answers

1
votes

You can use session or cookie for same

if(mysql_num_rows($queryUser) > 0){
echo '<p><div id="redirect"/></p>';
}

function  redir() {
$userName = $_POST['username'];
$email = $_POST['email'];

setcookie("userName", $userName, time()+3600);  /* expire in 1 hour */
setcookie("emial", $email, time()+3600);

code ... location.href = "registration.php";
}

And at your form

<input type="text" name="userName"  value="<?php echo isset($_COOKIE['userName']) ? $_COOKIE['userName']:'' ?>">
<input type="text" name="email"  value="<?php echo isset($_COOKIE['email']) ? $_COOKIE['email']:'' ?>">

You can above same with using session.

Hope this will help

1
votes

The best way to solve this, in MNSHO[1], is to make sure those values are never lost in the first place.

Lets assume, for the same of discussion, that you have a field in your HTML called 'username'

If you are using a GET (or a regular redirect), you can include username as part of the querystring, so it is available on the registration page.

window.location.href = "registration.php?username=" . $_REQUEST(username);

And then, on your registration page:

<input name="username" type="text">

What you'd want to do is look and see if you have an existing value that came in.

<?php
$usernameValue = "";
if (isset($_REQUEST['username'])) {
  $usernameValue = $_REQUEST['username'];
}

// Typing on the fly, I may have the type of quotation marks
// flipped around.  N
echo '<input name="username" type="text" value="$usernameValue">';
// or this...
echo "<input name='username' type='text' value='$usernameValue'>";

?>

Now, if you are redirecting to this page from another...

[1] My Not-So-Humble Opinion

0
votes

You could save them a get variable and append that to the file you are referring them too, or do it server side and save them by a temporary ID.

First possibility:

location.href="[email protected]&u=thisdude"

0
votes

Use AJAX to check for duplicate username/email immediately after the textbox loses focus and notify the user accordingly. That is how modern pages do it.

Is there a reason why you are waiting for the user to fill the complete form before checking for duplicate username/email?