Help with echo function on PHP [SOLVED]

This is a discussion on "Help with echo function on PHP [SOLVED]" within the PHP Forum section. This forum, and the thread "Help with echo function on PHP [SOLVED] are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jun 6th, 2008, 13:57
Up'n'Coming Member
Join Date: Jan 2006
Location: Belfast
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Help with echo function on PHP [SOLVED]

Hi,

I have a registration page:
http://www.sapphirediscounts.com/register.php

Once you enter the details, it gives the thankyou message but keeps the form etc below.

Is there a way I can change it, so once someone clicks the 'register' button, it sends the details and then redirects the user to say /thankyou.php

My script is as follows

Many thanks

Ben

PHP: Select all

<?php
$dbh
=@mysql_connect ("localhost""name""name");
if (!
$dbh) {
echo 
'<p>Unable to connect to the database server at this time</p>';
exit();
}
mysql_select_db ("sapphire_sapphire");
if (!@
mysql_select_db('sapphire_sapphire')) {
exit(
'<p>Unable to select the database at this time.</p>');

// function for generating errors
function errors($error){
if (!empty(
$error))
{
$i 0;
while (
$i count($error)){
echo 
"<p><span class=\"warning\">".$error[$i]."</span></p>\n";
$i ++;}
}
// close if empty errors
// close function
//This code runs if the form has been submitted
if (isset($_POST['submit'])) 
{
// check feilds are not empty
$firstName trim($_POST['firstName']);
if (
strlen($firstName) < 3) {
$error[] = 'First Name Must be between 3 and 20 charactors.';
}
if (
strlen($firstName) > 20) {
$error[] = 'First Name Must be between 3 and 20 charactors.';
}
 
// check fields are not empty
$lastName trim($_POST['lastName']);
if (
strlen($lastName) < 3) {
$error[] = 'Last Name Must be between 3 and 20 characters.';
}
if (
strlen($lastName) > 20) {
$error[] = 'Last Name Must be between 3 and 20 characters.';
}
// check fields are not empty
$username trim($_POST['username']);
if (
strlen($username) < 3) {
$error[] = 'username Must be between 3 and 20 characters.';
}
if (
strlen($username) > 20) {
$error[] = 'username Must be between 3 and 20 characters.';
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST[] = addslashes($_POST['username']);
}
$usercheck $_POST['username'];
$check mysql_query("SELECT username FROM members WHERE username = '$usercheck'"
or die(
mysql_error());
$check2 mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
$error[] = 'Sorry, the username <b>'.$_POST['username'].'</b> is already in use.';
}
// check fields are not empty
$password trim($_POST['password']);
if (
strlen($password) < 5) {
$error[] = 'password Must be between 5 and 20 characters.';
}
if (
strlen($password) > 20) {
$error[] = 'password Must be between 5 and 20 characters.';
}
// check fields are not empty
$password2 trim($_POST['password2']);
if (
strlen($password2) < 5) {
$error[] = 'confirm password Must be between 5 and 20 characters.';
}
if (
strlen($password2) > 20) {
$error[] = 'confirm password Must be between 5 and 20 characters.';
}
// this makes sure both passwords entered match
if ($_POST['password'] != $_POST['password2']) {
$error[] = 'Your passwords did not match.';
}
// check for valid email address
$email $_POST['email'];
$pattern '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!
preg_match($patterntrim($email))) {
$error[] = 'Please enter a valid email address';

// checks if the email is in use
if (!get_magic_quotes_gpc()) {
$_POST[] = addslashes($_POST['email']);
}
$emailcheck $_POST['email'];
$emailcheck1 mysql_query("SELECT email FROM members WHERE email = '$emailcheck'"
or die(
mysql_error());
$emailcheck2 mysql_num_rows($emailcheck1);
//if the name exists it gives an error
if ($emailcheck2 != 0) {
$error[]
'Sorry, the email address <b>'.$_POST['email'].'</b> is
already in use, Please choose another email address.'
;
}
// if validation is okay then carry on
if (!$error ) {
// get data from form
// We now get all the data from the form and add them to variables
$firstName $_POST['firstName'];
$lastName $_POST['lastName'];
$username $_POST['username'];
$password $_POST['password'];
$email $_POST['email']; 
// convert name to capital first letter then lowercase
$firstName ucfirst(strtolower($firstName));
$lastName ucfirst(strtolower($lastName));
 
if(!
get_magic_quotes_gpc())
{
$firstName addslashes($firstName);
$lastName addslashes($lastName);
$username addslashes($username);
$password addslashes($password);
$email addslashes($email);
}
 
// escape any harmful code and prevent sql injection
$firstName mysql_real_escape_string($firstName);
$lastName mysql_real_escape_string($lastName);
$username mysql_real_escape_string($username);
$password mysql_real_escape_string($password);
$email mysql_real_escape_string($email);
 
// removal all code from data
$firstName strip_tags($firstName);
$lastName strip_tags($lastName);
$username strip_tags($username);
$password strip_tags($password);
$email strip_tags($email);
 
//convert to capital first letter and the rest lowercase
$firstName ucwords(strtolower($firstName));
$lastName ucwords(strtolower($lastName));
$username ucwords(strtolower($username));
 
// now we insert it into the database
$insert1 "INSERT INTO members (firstName, lastName, username, password, email)
VALUES ('$firstName', '$lastName', '$username', md5('$password'), '$email')"
;
$result1 mysql_query($insert1) or die('Error : ' mysql_error());
 
//send email
$to "$email";
$subject "Registration Information";
$body
"Hi $firstName $lastName, \n\n Welcome to SapphireDiscounts.com \n\n Here is your
account information please keep this as you may need this at a later
stage. \n\nYour username is $username \n\n your password is $password
\n\n 
Your account is pending authorisation by the Sapphire team and should be activated within the next 48 hours. We will email you again when your login is activated.
\n\n Regards Site Admin \n\n"
;
$additionalheaders "From: <support@sapphirediscounts.com>\r\n";
$additionalheaders .= "Replt-To: support@sapphirediscounts.com";
if(
mail($to$subject$body$additionalheaders)){}
//send email to admin
$to "support@sapphirediscounts.com";
$subject "New member at SapphireDiscounts.com";
$body
"Hello admin\n\n There is a new member just registered to SapphireDiscounts.com
here are there details.\n\n Name: $firstName $lastName \n\n Username:
$username \n\n Password: $password \n\n Email: $email\n\n Regards Site Admin \n\n"
;
$additionalheaders "From: <youremail@domain.com>\r\n";
$additionalheaders .= "Replt-To: youremail@domain.com";
if(
mail($to$subject$body$additionalheaders)){}
 
echo 
"<div class=\"textorange3\"><p>Welcome to Sapphire Discounts, <b>$username</b>. Your account is pending authorisation, please check your email for details. You will be able to access SapphireDiscounts.com once your account has been authorised by the Sapphire Team.</h2>";
 
// end validation 
// end if posted 
 
//display any errors
errors($error);
?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<div class="h2">To gain access to the Sapphire Discounts website, please enter your details using the form below. <br><br>The scheme is only open to staff currently employed by <b>gem</b>.</div>
<p>
<table width="100%" border="0" cellpadding="0">
  <tr>
    <td width="20%"><div class="h2">First Name:</label></td>
    <td class="h2"><input name="firstName" type="text" maxlength="20" <?php if(isset($error)) {echo "value='$firstName'";} ?> />&nbsp;&nbsp;<i>Enter your first name</i></td>
  </tr>
  <tr>
    <td><label><div class="h2">Last Name:</label></td>
    <td class="h2"><input name="lastName" type="text" maxlength="20" <?php if(isset($error)) {echo "value='$lastName'";} ?> />&nbsp;&nbsp;<i>Enter your surname</i></td>
  </tr>
  <tr>
    <td><label><div class="h2">Choose a Username:</label></td>
    <td class="h2"><input name="username" type="text" maxlength="20" <?php if(isset($error)) {echo "value='$username'";} ?> />&nbsp;&nbsp;<i>Maximum 20 characters</i></td>
  </tr>
  <tr>
    <td><label><div class="h2">Password:</label></td>
    <td class="h2"><input name="password" type="password" maxlength="20" />&nbsp;&nbsp;<i>Between 5 and 20 characters</i></td>
  </tr>
  <tr>
    <td><label><div class="h2">Confirm Password:</label></td>
    <td class="h2"><input name="password2" type="password" maxlength="20" />&nbsp;&nbsp;<i>Re-enter your password</i></td>
  </tr>
  <tr>
    <td><label><div class="h2">Email:</label></td>
    <td class="h2"><input name="email" type="text" maxlength="255" <?php if(isset($error)) {echo "value='$email'";} ?> />&nbsp;&nbsp;<i>Enter a valid <b>gem</b> email address</i></td>
  </tr>
</table>
 
<p>
 
 
<input type="submit" name="submit" value="Register"> <div class="h2">By clicking 'Register', you confirm that you have read, understood and agree to the <a href="http://www.sapphirediscounts.com/about.php#terms" target="_blank"><u>Sapphire Terms and Conditions.</a></u>
</form>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jun 6th, 2008, 18:39
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,404
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
re: Help with echo function on PHP [SOLVED]

My idea is that if:
PHP: Select all

// now we insert it into the database
$insert1 "INSERT INTO members (firstName, lastName, username, password, email)
VALUES ('$firstName', '$lastName', '$username', md5('$password'), '$email')"
;
$result1 mysql_query($insert1) or die('Error : ' mysql_error()); 
All works fine, you have been successful right?

So:

PHP: Select all

if($insert1) {
echo 
'<p>Thanks for registering!</p>';
} else {
echo 
'<p>Something went wrong!</p>';

Would work? Just change the echos to whatever you want!
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jun 6th, 2008, 18:47
Up'n'Coming Member
Join Date: Jan 2006
Location: Belfast
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
re: Help with echo function on PHP [SOLVED]

Hi,
That bit is fine - but once you have submitted the form, it shows whatever message is in the echo (for example 'Thanks for registering'), but then still displays the original form fields below that text!

Therefore users might get confused and resubmit!

So I was wanting a script where instead of seeing the text 'thanks for registering', you get directed to another page which has the thankyou message and not the original form still!

Any ideas?

Many thanks

Ben
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jun 6th, 2008, 19:17
Jack Franklin's Avatar
Moderator

SuperMember
Join Date: May 2007
Location: Cornwall, England
Posts: 1,404
Blog Entries: 8
Thanks: 18
Thanked 14 Times in 14 Posts
re: Help with echo function on PHP [SOLVED]

Try using:
PHP: Select all

<?php
   header
'Location: http://www.yoursite.com/new_page.html) ;
?>
__________________
Jack Franklin - Webforumz Moderator
(x)HTML | CSS | PHP | MySQL | JQuery (Javascript)
Contact: My Blog | Twitter | Delicious
Want Lessons? PM me.
If you think I've helped, please press the 'Thanks' Button.
Last Blog Entry: A Week with VBulletin (Aug 28th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jun 6th, 2008, 19:33
Junior Member
Join Date: May 2008
Location: Colorado
Age: 19
Posts: 27
Thanks: 0
Thanked 1 Time in 1 Post
re: Help with echo function on PHP [SOLVED]

well the last post works great but another thing you could do is change the forms action value to thankyou.php or w/e you wanted instead of the $_SERVER['PHP_SELF'] super global since that just keeps you on the current page.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jun 6th, 2008, 19:56
Up'n'Coming Member
Join Date: Jan 2006
Location: Belfast
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
re: Help with echo function on PHP [SOLVED]

Thanks for your comments - just have a few more questions....!

Jack - where in the script would I insert that code you supplied? I had read about adding the URL in the header, but wasn't sure what to add!!

luv2php - I take it you mean editing this line:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
What would I edit it to? And would it still submit the data to the database?

Thanks again for your help with this - am slowing self learning PHP so your advice is very much appreciated!

Last edited by sing2trees; Jun 6th, 2008 at 20:33.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Jun 6th, 2008, 21:00
Junior Member
Join Date: May 2008
Location: Colorado
Age: 19
Posts: 27
Thanks: 0
Thanked 1 Time in 1 Post
re: Help with echo function on PHP [SOLVED]

It should still submit into the db, but it will just redirect you to your new page printing "thanks for registering $username!" or something of that sort. and you would for instance change this line:

<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">

to something like this:

<form action="thankyou.php" method="post">
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Jun 6th, 2008, 21:02
Junior Member
Join Date: May 2008
Location: Colorado
Age: 19
Posts: 27
Thanks: 0
Thanked 1 Time in 1 Post
re: Help with echo function on PHP [SOLVED]

or....you could even create attach an "action" string to the end of your $_SERVER['PHP_SELF'] instead.......

something like:


<form action="<?php $_SERVER['PHP_SELF'];?>?action=register" method="post">

which should eliminate all form fields etc...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Jun 6th, 2008, 21:38
Up'n'Coming Member
Join Date: Jan 2006
Location: Belfast
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
re: Help with echo function on PHP [SOLVED]

Hi!

Right - I tried the <form action="thankyou.php" method="post"> and it does redirect you to 'thankyou.php' but doesn't submit the data to the database, therefore doesn't work as the registration process!

Not sure how the other option would work if the form fields are eliminated!

Any ideas?! aghh this is driving me mad!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Jun 6th, 2008, 22:19
Junior Member
Join Date: May 2008
Location: Colorado
Age: 19
Posts: 27
Thanks: 0
Thanked 1 Time in 1 Post
re: Help with echo function on PHP [SOLVED]

eliminated meaning - they will not be shown on the page after its submitted...and sorry about my first try not helping im not a guru, therefore i still screw up a lot.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Jun 7th, 2008, 07:40
Up'n'Coming Member
Join Date: Jan 2006
Location: Belfast
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
re: Help with echo function on PHP [SOLVED]

Hi,

I tried the other option you suggested, but the form fields still show!! Agh just want to make them disappear!!

And I really do appreciate you helping - sorry if my post sounded otherwise!

Just not sure how to make it disappear! I guess I could look for a new register script, but seeing this one works, I don't really want to! Hmmm. Not sure now!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Jun 7th, 2008, 07:58
Up'n'Coming Member
Join Date: Jan 2006
Location: Belfast
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
re: Help with echo function on PHP [SOLVED]

Found a code which works!!!

echo '<meta http-equiv="refresh" content="0;url=http://wikipedia.org"/>';
echo 'Please wait';

Where content=0 - the 0 being the time delay!!

Thanks again for your help with this!!!
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati