Hi guys... I'm fairly new to PHP but do have experience with other languages.
Having an issue with sharing variables between multiple PHP scripts on the same page. Here is the deal:

The page in question is called by another page, which then passes a variable in the URL string, ie: www.nnnnn.com?team=HockeyTeam (HockeyTeam being BostonBruins for example)
Once the new page opens up, the variable is retrieved by the following script which has been placed right at the start of the page.

    php?>
    session_start();
    GetTeamName();
    $_SESSION['ReceivedTeamName'] = $TeamToSee;
    $NameOfTeam = $TeamToSee;
    $NameOfTeam = str_replace("_"," ",$NameOfTeam);
    $NameOfTeam = str_replace("*","'",$NameOfTeam);
    echo "<h1><center>Current Team Roster for</center></h1>";
    echo "<h1><center>". $NameOfTeam."</center></h1>";



function GetTeamName() {
   global $TeamToSee;
   $TeamToSee = $_GET["team"];

}
?>

Further down the page another php script kicks in and tries to retrieve $NameOfTeam , which is a modfied $TeamToSee variable. This seems to work but it's a hit and miss. It almost seems like I can only call it once and every subsequent call returns a null. I eventually got it working through some jiggery pokery magic, which I don't really like because it doesn't seem to make sense.

My other issue is the ReceivedTeamName variable, which is has the same value as what was passed to this page by a previous one. I cannot for life pass it to the other script at all. I tried all sorts of things and eventually ended up with $_SESSION (as per above) which still doesn't work. Funny thing here is that if I was to replace

$_SESSION['ReceivedTeamName'] = $TeamToSee;

with

$_SESSION['ReceivedTeamName'] = 'Hockey Team';

then the other php script sees this variable having 'Hockey Team' as its value.
It almost seems that $TeamToSee variable passes its value to $NameOfTeam and then blanks out.
Any help would be greatly appreciated guys. Please remember, I'm new at this.... :)

thanks

You're using too many unnecessary variables bud. Since this is a simple code, the best approach would be to have just one function do all your dirty work (formatting 'n stuff), store what you need in session variables, and use those in the rest of your code. Here, try this ... forgive me for changing all your variable and function names ... but I wanted to conform to php naming standards.

If this works for you, and you have any questions about any part of this, ask away.

<?php

    session_start();

    $_SESSION['RAW_TEAM_NAME'] = getTeamName("raw");
    $_SESSION['READABLE_TEAM_NAME'] = getTeamName("readable");

    function getTeamName($type){
        $team_to_see = $_GET['team'];

        if($type == "readable"){
            $team_to_see = str_replace("_", " ", $team_to_see);
            $team_to_see = str_replace("*", "'", $team_to_see);
        }

        return $team_to_see;
    }

    // This is copied directly from your code
    echo "<h1><center>Current Team Roster for</center></h1>";
    echo "<h1><center>". $_SESSION['READABLE_TEAM_NAME'] ."</center></h1>";

    // This ofcourse is an example of using the session variables anywhere else on your page
    echo "Raw team name is: " . $_SESSION['RAW_TEAM_NAME'] . " and the readable team name is: " . $_SESSION['READABLE_TEAM_NAME'] . "";


?>

This cleans it up quite a bit, thank you very much for that. It works great, the variables are being passed to the other scripts....however I think the variable is being cleared in the script that calls for its use. Here is the other script I'm working on that is on the same page

<?php
$data = file_get_contents('setupdata.txt');
$data = unserialize($data);
extract($data);
$Username = $data['a'];
$Password = $data["b"];
$SenderName = $data["c"];
$Subject = $data["d"];

// Just want to see if the team name displays OK
echo $_SESSION['RAW_TEAM_NAME'];

// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
   ?>
   <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
   From: <input type="text" name="from" value="<?php echo $_SESSION['READABLE_TEAM_NAME']; ?>"><br><br>
   Message To Pool Members : <br><textarea rows="5" cols="40" name="message"></textarea><br>
   <input type="submit" name="submit" value="Send Request">
   </form>
   <?php

} else {

   require 'c:\PHPMail\PHPMailerAutoload.php';

   $mail = new PHPMailer;
   $mail->isSMTP();
   $mail->SMTPAuth   = true;
   $mail->SMTPSecure = "tls";
   $mail->Host       = "smtp.gmail.com";
   $mail->Port = 587;
   $mail->Username   = $Username;
   $mail->Password   = $Password;
   $mail->SMTPDebug = 1;
   $mail->From = $Username;
   $mail->FromName = $_POST["from"];
   $mail->addAddress('awiecek@hotmail.com', 'Hockey Pool');
   $mail->isHTML(true);
   $message = $_POST["message"];
   $message = wordwrap($message, 70);
   $message = $message . $_SESSION['RAW_TEAM_NAME'] . $_SESSION['READABLE_TEAM_NAME'];
   $message = $message . "\r\n \r\n \r\n";
   $message = $message . "This is an outgoing account only, do not reply to this message.";
   $mail->Subject = $Subject;
   $mail->Body    =  $_POST['txtEmail'];
   $mail->Body    =  nl2br($message);

   if(!$mail->send()) {
      echo 'Message could not be sent.';
      echo 'Mailer Error: ' . $mail->ErrorInfo;
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=ShowStandings.php?team='.$_SESSION['RAW_TEAM_NAME'].'">';
      exit(); // End the request


   }   else   {
      echo 'Message has been sent';
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=ShowStandings.php?team='.$_SESSION['RAW_TEAM_NAME'].'">';
      exit(); // End the request

   }


}

?>

When this script initially loads up, the line

// Just want to see if the team name displays OK
    echo $_SESSION['RAW_TEAM_NAME'];

displays the raw team name as per your convention. At the same time, the script loads up a few variables from a file (Subject, Username, Password, etc), so far so good.
As soon as I hit the Submit button, the loaded up variables appear the be retained but the SESSION variables disappear. I know this because the script mails out the message using the username/password that was contained in these variables but the body of the message which contains the SESSION variables is blank. Huh?
Is it possible that
require 'c:\PHPMail\PHPMailerAutoload.php';
has something to do with this?
Even if I was to make up a variable such as
$ThisIsATest = $_SESSION['RAW_TEAM_NAME'];
at the top of the page, the ThisIsATest variable still blanks out.

I'm still wearing a VB6 hat here and this just doesn't make any sense at the moment.

Lol VB6 hat ... heeheehee!

Let's see, do I just give you the solution or do I explain it as well? Am I a nice guy? Ahhh sure why not.

Your script is working exactly the way it's supposed to. The problem is the way the form POST request is being sent. Compare your URL before and after you submit the form. Notice how the "?team=Hockey_Team" disappears after you submit the form. After the page refreshes with the form submit, the session variables are trying to reset themselves with values they're getting from "getTeamName" function, but since the getTeamName function has in essence, nothing to $_GET anymore, it's returning empty values, in turn setting your session variables to blank after the page refreshes.

Solution:

<form action="<?php echo $_SERVER['PHP_SELF'] . "?team=" . $_SESSION['RAW_TEAM_NAME']; ?>" method="POST">

That should just about do it.

Just a few things that have nothing to do with the functionality of your site, but should significantly improve readability/performance/efficiency/logic/whatever else you wanna put here.

1) When you're using $_SESSION, it's best to use a separate "logic only" file with no HTML to set all your session variables for the rest of the code to use. A "config" file, if you will. That way you don't end up with issues like page refresh and session variables trying to re-assign themselves with different values like above and you have one constant value to work with across the board

2) It looks like you're using GitHub's PHPMailer library. As much as I love using that myself, its main purpose is to solve the shortcomings like lack of HTML and attachment support of PHP's built-in mail() function. From the "$message" you're trying to send, it doesn't look like you're using HTML or sending any attachments, so instead of importing un-neccessary classes, it might be best to just format your $message in plain text, and use mail(). That way you reduce the 30+ lines you're using now to maybe 4 or 5.

Son of a.....

You are absolutely correct. I does work now. Thank you very very much! I would have NEVER thought to look there.

See, I was under the impression that the page didn't reload until the very end where I sent

echo '<META HTTP-EQUIV="Refresh" Content="0; URL=ShowStandings.php?team='.$_SESSION['RAW_TEAM_NAME'].'">';

I thought the variable was being kept all the way to the end but you're telling me that it disappears just as soon as the FORM ACTION is called. I thought it was a $_SESSION quirk and that's why I assigned its value to another temporary variable, which also blanked out. Sheesh....now it makes perfect sense.
What can I say? It's a learning curve which I'll have to take.
I really appreciate your time in helping me out with this. Be forewarned though... I will probably post more questions here. he he
Thank you once again!

1) When you're using $_SESSION, it's best to use a separate "logic only" file with no HTML to set all your session variables for the rest of the code to use. A "config" file, if you will. That way you don't end up with issues like page refresh and session variables trying to re-assign themselves with different values like above and you have one constant value to work with across the board

Oh man, that's another ball of wax. Not a bad idea though, I'll have to research this a bit to see how it is being implemented and such.

2) It looks like you're using GitHub's PHPMailer library. As much as I love using that myself, its main purpose is to solve the shortcomings like lack of HTML and attachment support of PHP's built-in mail() function. From the "$message" you're trying to send, it doesn't look like you're using HTML or sending any attachments, so instead of importing un-neccessary classes, it might be best to just format your $message in plain text, and use mail(). That way you reduce the 30+ lines you're using now to maybe 4 or 5.

I think I looked into this and if I remember correctly, there was an issue with the mail() function. Can't remember what it was but I read somewhere that it isn't compatible with Gmail?? I think...
You're absolutely correct, the purpose of the mail function in my program is to send a text type email, no fancy graphics, no attachments.
I'll have to revisit this idea to see if the code can be reduced.
Thank you for the hint.

Let's see, do I just give you the solution or do I explain it as well? Am I a nice guy? Ahhh sure why not.

LOL...yes, you are certainly a nice guy no matter what others say.
You saved me from a ton of research plus a possible coronary, that's for sure. LOL

LOL...yes, you are certainly a nice guy no matter what others say.
You saved me from a ton of research plus a possible coronary, that's for sure. LOL

Haha, cheers mate!
Don't forget to mark this thread solved if you got everything you need from this topic

See ya around.

No problem Bud... chat at you later. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.