Jump to content

Hiding form after submission with <?php echo $_SERVER['PHP_SELF']; ?>


davidcriniti

Recommended Posts

Hi there,

 

I'm trying to make my code more efficient by using <?php echo $_SERVER['PHP_SELF']; ?> ....where I've been used to having two pages with forms - one for filling out the form, and then a second page to process it / confirm, etc.

 

The page in question is designed to send a reminder of a user's password to their email address.

 

They basically put their email address into a form with one field, next to which it says:

 

"So...You forgot your password eh? Give us your email address and we'll send it to you."

 

I've got the code set up so it sends the email with the password, no problems.

 

However, once they've pressed submit, they can still see the message  "So...You forgot your password eh? Give us your email address and we'll send it to you"...as well as the form field and submit button.

 

I'd love to know what I need to do to hide these after the user has pressed submit??

 

Here's the code:


    <?php
if(isset($_POST['submit'])) 
{ 

$emailaddress = $_POST['emailaddress'];

echo "We've sent an email to $emailaddress, reminding you of your password.";


//MySQL Database Connect
include 'mysql_connect_applications.php'; 

// Get all the data from the "example" table

$result = mysql_query("SELECT * FROM applications2011 WHERE emailaddress= '$emailaddress'  LIMIT 0,1 ")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

$password = $row['password'];
}

// keeps getting the next row until there are no more to get

//***********************Email to forgetful user code***********************

$to = "$emailaddress\n";
$subject = "C2K Application";
$headers = "From: coast2kosci@coast2kosci.com";

$message = "Hi there, 

Seems you forgot your password. Here it is:

Password:  $password

Yours in the long run,

Dave.\n";
if (preg_match(' /[\r\n,;\'"]/ ', $_POST['emailaddress'])) {
  exit('Invalid Email Address');
}
else {
mail($to,$subject,$message,$headers);
}

//***********************End of Email to applicant code***********************
   
}
?>
              
              
                <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                  <p>
                    <label>
                      So...You forgot your password eh? Give us  your email address and we'll send it to you. 
                      <input type="text" name="emailaddress" id="emailaddress" />
                    </label>
                  </p>
                  <p>
                    <label>
                      <input type="submit" name="submit" id="submit" value="Submit Form" />
                    </label>
                  </p>
                </form>

 

Link to comment
Share on other sites

why do you think that cramming more code onto a single page and having the server run through all the code each time using PHP_SELF is more efficient? :confused:

 

to do what you are looking to do, simply add a hidden field to the form, call it what you like and assign it a value of one (1).  have the code at the top of the page check

 if(!isset($_POST['variableName'])) {

//your password comment and submit form here

}else{

//confirmation that the password has been sent

}

Link to comment
Share on other sites

why do you think that cramming more code onto a single page and having the server run through all the code each time using PHP_SELF is more efficient?

 

Well, firstly, thanks Muddy_Funster. I followed your instructions and I got it working.

 

I could have got it working on my own using two php pages (as alluded to in my first post), but I was just thinking of minimising the total number of pages in my site, since there are a lot of forms.

 

Does your quote above mean you would you suggest I keep using the two page method with the form action being to call a confirmation page, rather than using  PHP self?

 

Secondly, although the below code works, I'm not sure if it's good practice to have the form outside of the php tags? I had to interrupt the if / else statement by ending the php code, then ending the form, then starting up php code again.  Like I said it works, but not sure if it's the best way to go about it? - I'm new to this game.

 

Thanks,

Dave

 

 

              <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                 <?php
if(!isset($_POST['hiddenField'])) {


   echo       '
                  <p>
                    <label>
                      So...You forgot your password eh? Give us  your email address and we\'ll send it to you. 
                      <input type="text" name="emailaddress" id="emailaddress" />
                    </label>
                  </p>
                  <p>
                    <input name="hiddenField" type="hidden" id="hiddenField" value="1" />
                  </p>
                  <p>
                    <label>
                      <input type="submit" name="submit" id="submit" value="Submit Form" />
                    </label>
                  </p>';
			  ?>
			  
                </form>
			<?php
}
else

{






$emailaddress = $_POST['emailaddress'];

echo "We've sent an email to $emailaddress, reminding you of your password.";


//MySQL Database Connect
include 'mysql_connect_applications.php'; 

// Get all the data from the "example" table

$result = mysql_query("SELECT * FROM applications2011 WHERE emailaddress= '$emailaddress'  LIMIT 0,1 ")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

$password = $row['password'];
}

// keeps getting the next row until there are no more to get

//***********************Email to forgetful user code***********************

$to = "$emailaddress\n";
$subject = "C2K Application";
$headers = "From: coast2kosci@coast2kosci.com";

$message = "Hi there, 

Seems you forgot your password. Here it is:

Password:  $password

Yours in the long run,

Dave.\n";
if (preg_match(' /[\r\n,;\'"]/ ', $_POST['emailaddress'])) {
  exit('Invalid Email Address');
}
else {
mail($to,$subject,$message,$headers);
}

//***********************End of Email to applicant code***********************
   
}



?>

 

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.