Jump to content


Photo

passing var to function


  • Please log in to reply
5 replies to this topic

#1 rndilger

rndilger
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 25 July 2006 - 12:55 PM

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:

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

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

Here is the code I'm having trouble with:

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."";                 
     } 
} 



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

#2 willpower

willpower
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationWarrington, Cheshire, UK

Posted 25 July 2006 - 01:02 PM

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.


Wait...I've got it...Bollocks, it's gone again!

If at first you don't succeed....stop using ASP and get onto PHP

#3 rndilger

rndilger
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 25 July 2006 - 01:04 PM

You know, that's one thing I didn't try!

I give it a go and let you know.

Thanks,

Ryan

#4 rndilger

rndilger
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 25 July 2006 - 01:09 PM

So far, no luck with your suggestion...

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 25 July 2006 - 02:12 PM

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?
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 rndilger

rndilger
  • Members
  • PipPip
  • Member
  • 28 posts

Posted 25 July 2006 - 02:14 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users