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_aga in']) == "")
{
$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->getMessag e());
}
$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['locatio n']);
// 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['locatio n']);
}
$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->getMessag e());
}
$db_object->disconnect();
//redirect("members.php")
?>
<h1 class="HedingText">Registe red</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">Registe r</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_e rror?>
</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_erro r?>
</td></tr>
<tr><td><div align="left" class="style3">E-Mail*:</d iv></td><t d>
<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</d iv></td><t d>
<input type="text" name="location" maxlength="150">
</td></tr>
<tr>
<td> </td>
<td> </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
Im using the following code as a aprt of my registration form:
<?php
require('db_connect.php');
?>
<?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_aga
{
$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("/.*@.*..*/",
$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->getMessag
}
$name_checkk = $name_check->numRows();
if ($name_checkk != 0) {
die('Sorry, the username: <strong>'.$_POST['uname'].
}
// 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['locatio
// 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'] = '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['locatio
}
$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->getMessag
}
$db_object->disconnect();
//redirect("members.php")
?>
<h1 class="HedingText">Registe
<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">Registe
<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 align="left">
<input type="text" name="uname" maxlength="40"><?=$uname_e
</div></td></tr>
<tr><td><div align="left" class="style3">Password*:<
<input type="password" name="passwd" maxlength="50"><?=$passwd_
</td></tr>
<tr><td><div align="left" class="style3">Confirm Password*:</div></td><td>
<input type="password" name="passwd_again" maxlength="50"><?=$passwd_
</td></tr>
<tr><td><div align="left" class="style3">E-Mail*:</d
<input type="text" name="email" maxlength="100"><?=$email_
</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</d
<input type="text" name="location" maxlength="150">
</td></tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr><td colspan="2" align="right">
<div align="left">
<input type="image" src="images/submit_button.
</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
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">Registe red</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 ;)
<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">Registe
<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 ;)
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.
// 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.
ASKER
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?
ASKER
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.
$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.
ASKER
im using the meta method rather than the second option. would i be better off changing it to that one?
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.p hp:10) in /home/substan/public_html/ register.p hp on line 207
Warning: Cannot modify header information - headers already sent by (output started at /home/substan/public_html/
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.
ASKER
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
https://www.experts-exchange.com/questions/21788756/Registration-form-works-in-Firefox-but-not-in-IE.html
header("Location: members.php");