Link to home
Start Free TrialLog in
Avatar of clonmelog
clonmelog

asked on

redirect after insert has been completed

Hi,

Im using the following code as a aprt of my registration form:

<?php
require('db_connect.php');     // database connect script.
?>

<?php
$error = false;
if (isset($_POST['submit'])) { // if form has been submitted
     /* check they filled in what they supposed to,
     passwords matched, username
     isn't already taken, etc. */
     
     if(trim($_POST['uname']) == "")
     {
          $error = true;
          $uname_error = '<img src="images/error.gif">';
     }
     if(trim($_POST['passwd']) == "")
     {
          $error = true;
          $passwd_error = '<img src="images/error.gif">';
     }
     if(trim($_POST['passwd_again']) == "")
     {
          $error = true;
          $passwd_again_error = '<img src="images/error.gif">';
     }
     if(trim($_POST['email']) == "")
     {
          $error = true;
          $email_error = '<img src="images/error.gif">';
     }
     
      // check e-mail format
     if (!preg_match("/.*@.*..*/", $_POST['email']) | preg_match("/(<|>)/", $_POST['email'])) {
            $error = true;
          $email_error = '<img src="error.gif">';
     }
     
      // check passwords match
     if ($_POST['passwd'] != $_POST['passwd_again']) {
          $error = true;
          $passwd_error = '<img src="images/error.gif">';
          $passwd_again_error = '<img src="images/error.gif">';
     }
     
     
     if(!$error)
     {
           // check if username exists in database.
     
           if (!get_magic_quotes_gpc()) {
                 $_POST['uname'] = addslashes($_POST['uname']);
           }
     
     
     
           $name_check = $db_object->query("SELECT username FROM users WHERE username = '".$_POST['uname']."'");
     
           if (DB::isError($name_check)) {
                 die($name_check->getMessage());
           }
     
           $name_checkk = $name_check->numRows();
     
           if ($name_checkk != 0) {
                 die('Sorry, the username: <strong>'.$_POST['uname'].'</strong> is already taken, please pick another one.');
           }
     
           
     
         
     
           // no HTML tags in username, mobile, location, password
     
           $_POST['uname'] = strip_tags($_POST['uname']);
           $_POST['passwd'] = strip_tags($_POST['passwd']);
           $_POST['mobile'] = strip_tags($_POST['mobile']);
           $_POST['location'] = strip_tags($_POST['location']);
     
     
     
           // check show_email data
     
           if ($_POST['show_email'] != 0 & $_POST['show_email'] != 1) {
                 die('Nope');
           }
     
           /* the rest of the information is optional, the only thing we need to
           check is if they submitted a website,
           and if so, check the format is ok. */
     
           /*if ($_POST['website'] != '' & !preg_match("/^(http|ftp):///", $_POST['website'])) {
                 $_POST['website'] = 'http://'.$_POST['website'];
           }*/
     
           // now we can add them to the database.
           // encrypt password
     
           $_POST['passwd'] = md5($_POST['passwd']);
     
           if (!get_magic_quotes_gpc()) {
                 $_POST['passwd'] = addslashes($_POST['passwd']);
                 $_POST['email'] = addslashes($_POST['email']);
                 $_POST['mobile'] = addslashes($_POST['mobile']);
                 $_POST['location'] = addslashes($_POST['location']);
           }
     
     
     
           $regdate = date('m d, Y');
     
           $insert = "INSERT INTO users (
                       username,
                       password,
                       regdate,
                       email,
                       mobile,
                       location,
                       show_email,
                       last_login)
                       VALUES (
                       '".$_POST['uname']."',
                       '".$_POST['passwd']."',
                       '$regdate',
                       '".$_POST['email']."',
                       '".$_POST['mobile']."',
                       '".$_POST['location']."',
                       '".$_POST['show_email']."',
                       'Never')";
     
           $add_member = $db_object->query($insert);
     
           if (DB::isError($add_member)) {
                 die($add_member->getMessage());
           }
     
           $db_object->disconnect();
               //redirect("members.php")
     ?>
     
     
     <h1 class="HedingText">Registered</h1>
     
     <p class="style3">Thank you, your information has been added to the database, you may now <a href="members.php" title="Login">log in</a>.</p>
     <span class="style2">
     <?php
     }
}

?>
</span>
<h1 class="HedingText">Register</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td><div align="left" class="style3">Username*:</div></td><td>
  <div align="left">
    <input type="text" name="uname" maxlength="40"><?=$uname_error?>
  </div></td></tr>
<tr><td><div align="left" class="style3">Password*:</div></td><td>
<input type="password" name="passwd" maxlength="50"><?=$passwd_error?>
</td></tr>
<tr><td><div align="left" class="style3">Confirm Password*:</div></td><td>
<input type="password" name="passwd_again" maxlength="50"><?=$passwd_again_error?>
</td></tr>
<tr><td><div align="left" class="style3">E-Mail*:</div></td><td>
<input type="text" name="email" maxlength="100"><?=$email_error?>
</td></tr>
<tr><td><div align="left" class="style3">Mobile Number </div></td><td>
<input name="mobile" type="text" id="mobile" maxlength="150">
</td></tr>
<tr><td><div align="left" class="style3">Location</div></td><td>
<input type="text" name="location" maxlength="150">
</td></tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr><td colspan="2" align="right">
  <div align="left">
  <input type="image" src="images/submit_button.jpg" alt="Submit" name="submit" value="Sign Up" img>
   
  </div></td></tr>
</table>
<div align="left"></div>
</form>

<?php



?>


what i want is for the page to actually redirect to a page called "members.php" in the same directory after the sql query has been carried out that inserts their details into the db. what is the most efficient way of carying this out?

thanks
Avatar of Tomeeboy
Tomeeboy
Flag of United States of America image

Well, an easy way to redirect is to use header() after your query (assuming nothing else has been output to the browser yet.  Example:

header("Location: members.php");
Since you are using a "Thank you" message already, you may want to use a meta refresh instead.  Example:

<META HTTP-EQUIV=Refresh CONTENT="5; URL=http://www.yoursite.com/members.php">

Example usage:

<html>
<head>
<title>Registration Complete!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META HTTP-EQUIV=Refresh CONTENT="5; URL=http://www.yoursite.com/members.php">
</head>

<body>
 <h1 class="HedingText">Registered</h1>
     
     <p class="style3">Thank you, your information has been added to the database.  This page will automatically redirect you in 5 seconds, or you may <a href="members.php" title="Login">click here</a>to log in now.</p>
</body>
</html>

     <?php
     }
}


You would need to tweak this a bit to work with your existing html output (you should probably put an } else { statement that excludes the registration form HTML if they just registered as well), but hopefully you get the general idea.  Hope that helps ;)
Avatar of clonmelog
clonmelog

ASKER

I actually meant to take out the thank you. i actually want to send it to a page that will have the thank you message on it rather than having it within the page. im having issues wit the full script working in IE so i want to try something else!
Well, then header() would work fine.  If you're wanting to send the user to members.php, but have the page display a message when they arrive, do something like this:

// data inserted into database successfully

header("Location: members.php?reg=1");

Then in members.php, where you want the message to display:

if (!empty($_GET['reg'])) {
     // display Thank You message
}

Remember, you have to use header() before anything has been output to the browser (including html, blank lines, etc.) or you will end up getting "Headers already sent" errors.
OK a problem ive run into with that coe is that i have validation on all of the fields which generates an error image next to each field thats incorrect... the page is just redirecting now. how can i work around that?
Ive just tested it in firefox and the page redirects without even being submitted.
The call to header() should be right after this line:

$db_object->disconnect();

This section of code is within the if (!$error) statement, so if there are errors in the form, it should never reach the header call, it should skip over that entir section and display the form again with error images.
im using the meta method rather than the second option. would i be better off changing it to that one?
Im having trouble placing that code in the correct places on my script. where exactly are you suggesting that the lines of code go? use the code in my first post if you need to. im not really familiar with this header method that your suggesting, i thought a javascript redirect would be the method used so it shows how much i know!
ASKER CERTIFIED SOLUTION
Avatar of Tomeeboy
Tomeeboy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I get the following error when i apply that function now:

Warning: Cannot modify header information - headers already sent by (output started at /home/substan/public_html/register.php:10) in /home/substan/public_html/register.php on line 207
Make sure that nothing it being output to the browser before you you try using header(), this includes any html, whitespace, etc..  The header() function has to be the first thing sent to the browser, or you'll get that error.
ive changed the way the login was working slightly myself, ad changed to j#using a javascript redirect although i cant see that your ideas were down the right road. i am making use of the ?reg=1 on the redirect URL also, hence ive awarded you the points. My only issue now is that it works in firefox and not IE which is baffling. I have opened a new questions though as i feel you have more than answered this one. if you feel the urge to contribute its all yours ;-)

https://www.experts-exchange.com/questions/21788756/Registration-form-works-in-Firefox-but-not-in-IE.html