Jump to content

Set the time when redirect to the page?


shebbycs

Recommended Posts

  <?php
    mysql_connect("localhost","root") or die(mysql_error());
    mysql_select_db("Regis") or die(mysql_error());



     if (isset($_POST["sub"]))
     {
      $usercheck = $_POST["username"];

       $check = mysql_query("SELECT username FROM registration WHERE username = '$usercheck'") or die(mysql_error());

       $check2 = mysql_num_rows($check);
        //if the name exists it gives an error
     if ($check2 != 0)
     {
      echo("<script LANGUAGE='JavaScript'>window.alert('Sorry, the username" . $usercheck . "is already in use.')</SCRIPT>");
       echo ("<script LANGUAGE='JavaScript'>window.location = 'registration.php'</script>");


      // print("url=registration.php\");
     }

     }
?>
<html><head></head><body>
<form action="submit.php" method="post">

<table border="0"> 

<tr><td colspan=2><h1>Login</h1></td></tr> 

<tr><td>Username:</td><td> 

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

</td></tr> 

<tr><td>Password:</td><td> 

<input type="password" name="pass" maxlength="50"> 

</td></tr> 

<tr><td><a href="register.php">Register</a></td>

<td><input type="submit" name="submit" value="Login">

</td></tr>

</table> 

</form>

</body></html>


 

 

my problem is  if ($check2 != 0)

    {

      echo("<script LANGUAGE='JavaScript'>window.alert('Sorry, the username" . $usercheck . "is already in use.')</SCRIPT>");

      echo ("<script LANGUAGE='JavaScript'>window.location = 'registration.php'</script>");

 

 

when it redirect to registration.php its seem it take more than 5 seconds to redirect any solution to make it faster????

Link to comment
Share on other sites

Don't use JavaScript alerts/redirects to display errors. It's not a user-friendly experience! Instead why not just redirect to the registration page using PHP and pass in a flag to display the error? Better yet, why not avoid redirects at all and just display the registration form again with the inputs pre-filled?

Link to comment
Share on other sites

Okay here's a basic example to work from:

 

<?php

// Define an array to hold errors (done here so we don't have to check the $errors variable exists later)
$errors = array();

// Check if we have post data
if (!empty($_POST))
{
    // Validate input1
    if (empty($_POST['input1']))
    {
        $errors['input1'] = 'Please enter a value for input1';
    }

    // Validate input2
    if (empty($_POST['input2']))
    {
        $errors['input2'] = 'Please enter a value for input2';
    }

    // Make sure there was no errors
    if (empty($errors))
    {
        // Do something with data..
        // Redirect to another page..
    }
}

?>

<h1>Example Form</h1>

<form method="post" action="">

    <!-- input1 -->
    <?php if (isset($errors['input1'])): ?>
        <div class="error"><?php echo $errors['input1']; ?></div>
    <?php endif; ?>
    Input1: <input type="text" name="input1" value="<?php if (isset($_POST['input1'])) echo htmlspecialchars($_POST['input1']); ?>" />

    <!-- input2 -->
    <?php if (isset($errors['input2'])): ?>
        <div class="error"><?php echo $errors['input2']; ?></div>
    <?php endif; ?>
    Input1: <input type="text" name="input1" value="<?php if (isset($_POST['input2'])) echo htmlspecialchars($_POST['input2']); ?>" />

</form>

 

The form will be shown if either there is no POST data, or the POST data is there but contains errors. When successful the user will be redirected so they will never see the form. Errors will also be shown in line with the input, and the values they entered originally will be pre-filled in the inputs (but with some protection to prevent XSS attacks).

 

Most importantly though, no redirects are needed to re-display the form. We should only redirect somewhere else when we actually mean to go somewhere else (or to prevent a POST refresh).

Link to comment
Share on other sites

Made a few mistakes in the HTML:

 

<h1>Example Form</h1>

<form method="post" action="">

    <!-- input1 -->
    <div>
        <?php if (isset($errors['input1'])): ?>
            <div class="error"><?php echo $errors['input1']; ?></div>
        <?php endif; ?>
        Input1: <input type="text" name="input1" value="<?php if (isset($_POST['input1'])) echo htmlspecialchars($_POST['input1']); ?>" />
    </div>

    <!-- input2 -->
    <div>
        <?php if (isset($errors['input2'])): ?>
            <div class="error"><?php echo $errors['input2']; ?></div>
        <?php endif; ?>
        Input2: <input type="text" name="input2" value="<?php if (isset($_POST['input2'])) echo htmlspecialchars($_POST['input2']); ?>" />
    </div>

    <input type="submit" value="Submit" />

</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.