Jump to content

How can I pass a variable from my function to another part of my file?


Conjurer

Recommended Posts

I am trying to pass a variable out of a function and reuse it in the page code. 

 

On the web page they see I would like to report to them the email value that is in the database and since that is pulled in the first part of the function I thought maybe I could just put some lines in the html using the $email variable, but when I do it gives me an error message of Undefined variable: email

 

Is there a way I can keep the variable live outside of the functions so I can reuse it instead of having to look up the value again?

 

Here is the code where I am trying to access it:

$username = $_POST['username'];
  echo 'This is the username you submitted: ' .$username .'<br>';
  try
    {
    $password = reset_password($username);
    notify_password($username, $password);   <<<This is the function that finds the email I am trying to pass
    echo "Your new password has been e-mailed to you at [b]$email[/b].<br />";
echo "If you no longer have access to the [b]$email [/b]e-mail account, please contact our webmaster at info@mydomain.com for assistance.<br />";
    echo ' <div id="nav"><a href="http://www.mydomain.com/">mydomain Home</a> <a href="index.php5">Logout</a></div>';  
    }
    catch (Exception $e)
    {
    echo 'Your password could not be reset - please try again later.';
  echo $e;  //added to see error
    }

 

 

And here is the function that looks up the email address I want to pass into the rest of the web page:

//---------------------------------------------------------------------------
// function to notify user of new password
//---------------------------------------------------------------------------

function notify_password($username, $password)
// notify the user that their password has been changed
{
    $conn = db_connect();
    $result = $conn->query("select email from directory 
                            where user_id=(select user_id from users where username = '$username')");[/b]
    if (!$result)
    {
      throw new Exception('Could not find email address.');  
    }
    else if ($result->num_rows==0)
    {
      throw new Exception('Could not find email address.');   // username not in db
    }
    else
    {
      $row = $result->fetch_object();
      $email = $row->email;
      $from = "From: support@mydomain.com \r\n";
      $mesg = "Your password for the directory has been changed. \r\n"
  				."You can login to the membership directory at http://www.mydomain.com/Directory/index.php5 \r\n\r\n"
				."Your username is: $username \r\n"
				."Your new password is: $password\r\n\r\n"
              ."You can either keep this password or change it to one you preferr the next time you log in. "
		  ."To change it, once you log in click on the \"Change Password\" button.\r\n";
      
      
      if (mail($email, 'Mydomain login information', $mesg, $from))
        return true;      
      else
        throw new Exception('Could not send email. Please contact info@mydomain.com for assistance!');
    }
} 

 

Any ideas would be greatly appreciated.

Link to comment
Share on other sites

When you call the function

 

call it like

 

 

$email = notify_password($username, $password)

 

and at the end of the function

 

      if (mail($email, 'Mydomain login information', $mesg, $from))

        return $email;     

      else

        throw new Exception('Could not send email. Please contact info@mydomain.com for assistance!');

    }

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.