Jump to content

Archived

This topic is now archived and is closed to further replies.

rndilger

passing var to function

Recommended Posts

Hello everyone. I'm having trouble passing vars to a function.

Here's the situation...

I call the SendEmail function using a case switch with the following link:

[code]<a href='members/admin.php?action=SendEmail&userid=$row[userid]'>$row[email]</a>[/code]

The userid and email are successfully pulled from the MySQL db.

Here is the code I'm having trouble with:

[code]function SendEmail(){
      global $db, $prefix, $admin, $userid, $username, $fullname, $email, $msg_error, $subject, $msg, $site_name, $site_email, $site_url1;

        $result = $db->sql_query("SELECT * FROM ".$prefix."_users WHERE userid='$userid'");
        $row = $db->sql_fetchrow($result);
       
        $username = $row['username'];
        $fullname = $row['fullname'];
        $email = $row['email'];

        include("/home/indiana/public_html/members/header.php");
        SendEmailForm();
        include("/home/indiana/public_html/members/footer.php");
}

function SendEmailForm(){         
      global $db, $prefix, $admin, $userid, $username, $fullname, $email, $msg_error, $subject, $msg, $site_name, $site_email, $site_url1;
           
    if (is_logged_in_admin($admin)) {   
   
        navigation_menu();
         
        echo "<div class='center'><form method='POST' action='members/admin.php'>
              <table align='center' border='0' width='500' cellpadding='2'>
                        <tr>
                            <td colspan='2' align='center' class='ExtraLarge black bold'>"._SEND_EMAIL_TO."<span class='ExtraLarge bold blue'>$fullname ($username)</td>
                        </tr>
                        <tr><td colspan='2'>&nbsp;</td></tr>
                        <tr><td colspan='2'>$msg_error</td></tr>
                        <tr>
                            <td width='50' align='right'>"._TO."</td>
                            <td><span class='black bold'>$email</span></td>
                        </tr>
                        <tr>
                            <td align='right'>"._SUBJECT."</td>
                            <td><input type='text' name='subject' size='50' value='$subject'></td>
                        </tr>
                        <tr>
                            <td valign='top' align='right'>"._MESSAGE."</td>
                            <td>
                                <textarea rows='15' name='msg' cols='50'>$msg</textarea>
                                <br>
                                ---------------------
                                <br>
                                $site_name<br>
                                $site_url1                                 
                      </td>
                        </tr>
                        <tr><td colspan='2'>&nbsp;</td></tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>
                                <input type='hidden' name='action' value='do_SendEmail'>
                                <input type='submit' value='"._SEND."'>
                            </td>
                        </tr>
                    </table></form></div>";

      }else{
          echo "<br />"._NOT_AUTHORIZED1."";               
    }           
}

function do_SendEmail(){
      global $db, $prefix, $admin, $userid, $username, $fullname, $email, $msg_error, $subject, $msg, $site_name, $site_email, $site_url1;

    if (is_logged_in_admin($admin)) {
           
        $message .= "$msg\n\n";
        $message .= "-----------------------\n";
        $message .= "$site_name\n";
        $message .= "$site_url1\n";
         
        $to = "$fullname <$email>";
        $header = "From: $site_name <$site_email>\n";
        $header .= "Reply-To: $site_email\n\n";
           
            if((!$subject) || (!$msg)){

                if(trim(empty($subject))){ }
                    if(trim(empty($msg))){ } 
                     
                    //print the error message and reload the form.
                $msg_error = ""._EMAIL_SEND_ERROR."";
                SendEmail();
                exit();
          }else{
                     
                mail($to, $subject, $message, $header);
                msg_redirect(""._MSG_SENT."","members/admin.php","3");               
            }                       

      }else{
          echo "<br />"._NOT_AUTHORIZED1."";               
    }
}  [/code]


This functions work perfectly when initially called. However, if the subject or msg fields are left blank, the form is supposed to be reloaded and the msg_error should be displayed. That error message is displayed correctly, but I cannot figure out how to pass the $username, $fullname, and $email vars back to the SendEmail function. Therefore, when the form is reloaded, no email exists to send the mail to and the functions fail.

Any help with this would be appreciated. I've tried all the different ways I can think of. I'm still fairly new at PHP, so please be patient with me! I'm guessing there's something easy I'm missing.

Thanks,

Ryan

Share this post


Link to post
Share on other sites
I may be  barking up the wrong tree here...but have you tried declaring the $vars at ie:

$action=$_GET['action'];
$userid=$_GET['userid........and so on

This should enure that the vars contained in the url are read and utilised.

Share this post


Link to post
Share on other sites
That is because the form is using the POST method to pass the vars, so there will be no $_GET vars.

It looks like your SendEmail() function is using global vars to get the values, do you have register_globals switched on?

Share this post


Link to post
Share on other sites
Yes, globals are turn on. I know this because all of the other functions I've written also use globals.

Share this post


Link to post
Share on other sites

×

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.