Jump to content

Redirecting to a new page after mysql insert


perky416

Recommended Posts

Hi everyone,

 

I have a page that contains a registration form that writes the users details to a mysql database. Im having some difficulties redirecting to a thank you page after the details have successfully  been submitted.

I have tried using

header("location: http://whatever.com/thankyou.php");

at the very top of the page however i get redirected as soon as the page loads.

 

Please could someone suggest a good way to redirect to the thankyou page after the data has been inserted to the database? I have included my code if it helps.

 

Thanks

 

<? header("Location: thanks.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>

<form name="registration_form" method="post">
First Name: <input value = "<?php print $_POST["first_name"]; ?>" type="text" name="first_name"><br />
Last Name: <input value = "<?php print $_POST["last_name"]; ?>" type="text" name="last_name"><br />
Email: <input value = "<?php print $_POST["email"]; ?>" type="text" name="email"><br />
Confirm Email: <input value = "<?php print $_POST["confirm_email"]; ?>" type="text" name="confirm_email"><br />
Username: <input value = "<?php print $_POST["username"]; ?>" type="text" name="username"><br />
Password: <input type="password" name="password"><br />
Confirm Password: <input type="password" name="confirm_password"><br />
<input type="submit" name="submit" value="Register">
</form>

<?php
$connect=mysql_connect("localhost","leemp5_admin","p7031521");
mysql_select_db("leemp5_database",$connect) or die (mysql_errno().":<b> ".mysql_error()."</b>");

if (isset($_POST['submit']))
{
if (empty($_POST['first_name'])){
echo ('Please enter your first name<br />');}

if (empty($_POST['last_name'])){
echo ('Please enter your last name<br />');}
   
if (empty($_POST['email'])){
echo ('Please enter your email address<br />');}

if (!empty($_POST['email'])){	
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
echo ('The email address you entered is not valid<br />');}}

$email = $_POST["email"];
$emailquery = mysql_query("SELECT * FROM users WHERE email='$email'");
if(mysql_num_rows($emailquery) != 0){
echo ('The email you have entered is already in use<br />');}

if (empty($_POST['confirm_email'])){
echo ('Please confirm your email address<br />');}

if (!empty($_POST['email']) && !empty($_POST['confirm_email']) &&($_POST['email']) != ($_POST['confirm_email'])){
echo ('The emails you entered do not match<br />');} 
   
if (empty($_POST['username'])){
echo ('Please enter your username<br />');}

if (empty($_POST['password'])){
echo ('Please enter your password<br />');}

if (empty($_POST['confirm_password'])){
echo ('Please confirm your password<br />');}

if ($_POST['password'] != $_POST['confirm_password']){
echo ('The passwords you entered do not match');} 

$username = $_POST["username"];
$usernamequery = mysql_query("SELECT * FROM users WHERE username='$username'");
if(mysql_num_rows($usernamequery) != 0){
echo ('The username you have entered is already in use<br />');}

if (!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['email']) && !empty($_POST['confirm_email']) && !empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['confirm_password']) && ($_POST['email']) == ($_POST['confirm_email']) && (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) && ($_POST['password'] == $_POST['confirm_password']) && (mysql_num_rows($emailquery) == 0) && (mysql_num_rows($usernamequery) == 0))
{			$insert_query = 'insert into users (username,first_name,last_name,email,password) 
				values
				(
				"' . $_POST['username'] . '", 
				"' . $_POST['first_name'] . '",
				"' . $_POST['last_name'] . '",
				"' . $_POST['email'] . '",
				"' . $_POST['password'] . '"
				)';

mysql_query($insert_query);}
}
?>

</body>
</html>

Link to comment
Share on other sites

maybe this would come in handy

 

register.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>

<form name="registration_form" method="post">
First Name: <input value = "<?php print $_POST["first_name"]; ?>" type="text" name="first_name"><br />
Last Name: <input value = "<?php print $_POST["last_name"]; ?>" type="text" name="last_name"><br />
Email: <input value = "<?php print $_POST["email"]; ?>" type="text" name="email"><br />
Confirm Email: <input value = "<?php print $_POST["confirm_email"]; ?>" type="text" name="confirm_email"><br />
Username: <input value = "<?php print $_POST["username"]; ?>" type="text" name="username"><br />
Password: <input type="password" name="password"><br />
Confirm Password: <input type="password" name="confirm_password"><br />
<input type="submit" name="submit" value="Register">
</form>
</body>
</html>

 

reguser.php

<?php
$connect=mysql_connect("localhost","leemp5_admin","p7031521");
mysql_select_db("leemp5_database",$connect) or die (mysql_errno().":<b> ".mysql_error()."</b>");

if (isset($_POST['submit']))
{
if (empty($_POST['first_name'])){
echo ('Please enter your first name<br />');
header('Refresh: 3; url=register.php');
}

if (empty($_POST['last_name'])){
echo ('Please enter your last name<br />');
header('Refresh: 3; url=register.php');
}
   
if (empty($_POST['email'])){
echo ('Please enter your email address<br />');
header('Refresh: 3; url=register.php');
}

if (!empty($_POST['email'])){	
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
echo ('The email address you entered is not valid<br />');
header('Refresh: 3; url=register.php');
}}

$email = $_POST["email"];
$emailquery = mysql_query("SELECT * FROM users WHERE email='$email'");
if(mysql_num_rows($emailquery) != 0){
echo ('The email you have entered is already in use<br />');
header('Refresh: 3; url=register.php');
}

if (empty($_POST['confirm_email'])){
echo ('Please confirm your email address<br />');
header('Refresh: 3; url=register.php');
}

if (!empty($_POST['email']) && !empty($_POST['confirm_email']) &&($_POST['email']) != ($_POST['confirm_email'])){
echo ('The emails you entered do not match<br />');
header('Refresh: 3; url=register.php');
} 
   
if (empty($_POST['username'])){
echo ('Please enter your username<br />');
header('Refresh: 3; url=register.php');
}

if (empty($_POST['password'])){
echo ('Please enter your password<br />');
header('Refresh: 3; url=register.php');
}

if (empty($_POST['confirm_password'])){
echo ('Please confirm your password<br />');
header('Refresh: 3; url=register.php');
}

if ($_POST['password'] != $_POST['confirm_password']){
echo ('The passwords you entered do not match');
header('Refresh: 3; url=register.php');
} 

$username = $_POST["username"];
$usernamequery = mysql_query("SELECT * FROM users WHERE username='$username'");
if(mysql_num_rows($usernamequery) != 0){
echo ('The username you have entered is already in use<br />');
header('Refresh: 3; url=register.php');
}

if (!empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['email']) && !empty($_POST['confirm_email']) && !empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['confirm_password']) && ($_POST['email']) == ($_POST['confirm_email']) && (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) && ($_POST['password'] == $_POST['confirm_password']) && (mysql_num_rows($emailquery) == 0) && (mysql_num_rows($usernamequery) == 0))
{			$insert_query = 'insert into users (username,first_name,last_name,email,password) 
				values
				(
				"' . $_POST['username'] . '", 
				"' . $_POST['first_name'] . '",
				"' . $_POST['last_name'] . '",
				"' . $_POST['email'] . '",
				"' . $_POST['password'] . '"
				)';

mysql_query($insert_query);}
echo "<br><center>You have successfully registered!</center>";
header('Refresh: 3; url=index.php');
}
?>

 

after all if {}, before the syntax

}

i have added

header('Refresh: 3; url=register.php');

so that user will be redirected to the register page after it.

 

two lines before the end of the reguser.php, it show that it will echo successful and redirect to index.php after 3 seconds

Link to comment
Share on other sites

So if the user makes errors, only one error is reported, and they're redirected back to a now empty form? From a user-friendliness standpoint, that's a horrible way to do it. All fields should be validated, all validation errors should be reported at once on the form with the values the user entered already pre-filled so they can make all corrections at once and resubmit.

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.