[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

redirect after insert has been completed

Posted on 2006-03-23
13
Medium Priority
?
423 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:clonmelog
  • 7
  • 6
13 Comments
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 16273373
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");
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 16273480
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 ;)
0
 
LVL 2

Author Comment

by:clonmelog
ID: 16273796
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!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:Tomeeboy
ID: 16273930
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.
0
 
LVL 2

Author Comment

by:clonmelog
ID: 16274216
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?
0
 
LVL 2

Author Comment

by:clonmelog
ID: 16274249
Ive just tested it in firefox and the page redirects without even being submitted.
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 16274286
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.
0
 
LVL 2

Author Comment

by:clonmelog
ID: 16281484
im using the meta method rather than the second option. would i be better off changing it to that one?
0
 
LVL 2

Author Comment

by:clonmelog
ID: 16281744
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!
0
 
LVL 15

Accepted Solution

by:
Tomeeboy earned 1600 total points
ID: 16282180
Whichever method you use, it should be immediately after the script finishes the data insertion.  Here's the area of the code where it needs to go:

    $db_object->disconnect();
             //redirect("members.php")

          //INSERT the header() function or echo the HTML for the meta refresh here

     }
}

?>
</span>
<h1 class="HedingText">Register</h1>
0
 
LVL 2

Author Comment

by:clonmelog
ID: 16283047
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
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 16290269
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.
0
 
LVL 2

Author Comment

by:clonmelog
ID: 16290285
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 ;-)

http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_21788756.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question