views:

172

answers:

2

I have two scripts that I'm working on. The first receives $agentID via GET (as well as some other data via other GET variables), then looks up $firstName and $lastName in a database by $agentID. Once done, it displays $firstName, $lastName and $agentID on the screen, as text in a form (not in a form input). After the form is submitted, $agentID and the form data are to be written to the database as part of a new record, then $agentID, $firstName, $lastName, and the form data are stored in SESSION variables so the data can be displayed on a confirmation page. The trouble I'm having is that $agentID, $firstName, $lastName are not written to the database, to SESSION variables, and it won't even put the values in an email! Both scripts start a session as the first thing. I've checked the first script over and over to make sure the variables aren't being over-written, unset, or anything.

Here's some code and I hope someone can see what I apparently am not. This is from the first script:

session_start();
$mySQLdb = EstablishConnection("table");
$agentID = $_GET['agent'];
$agentData = SurveyAgent($agentID);
$agentDataArray = explode(".", $agentData);
$agentFirstName = $agentDataArray[0];
$agentLastName = $agentDataArray[1];
$agentFullName = $agentFirstName." ".$agentLastName;

The call to SurveyAgent establishes its own connection to the database, using the same code as the first line in the previous block. SurveyAgent() is in an included file. Here's the important bits from SurveyAgent():

$mySQLselect = "SELECT lname, fname FROM table WHERE id_no='$userID';";
$sponsorData = $mySQLrow[1].".".$mySQLrow[0];
return $sponsorData;

I had originally put the data returned from the database into an array and returned the array, but when things weren't working, I changed it to just concatenating the two pieces with a period between to use explode (as the code currently does). Finally, the first script wraps up this way (after the write to the database):

$_SESSION['agentID'] = $agentID;
$_SESSION['agentName'] = $agentFullName;
$body = $agentFullName.", ".$agentID;
mail("email@address", "test", $body, "From: email@address");
header("Location: http://www.domain.com/path/to/script.php");

The second script starts like this:

session_start();
$agentID = $_SESSION['agentID'];
$agentName = $_SESSION['agentName'];

The second script receives several other variables via SESSION from the first script. $agentID and $agentName are the only two variables I am having trouble with. I have tried changing the variables' names, including the SESSION keys' names. If I hard-code the value of $agentID, instead of receiving it via GET, everything works fine. It makes no sense to me why the first script is displaying the data received via GET and the database query, but won't pass them anywhere. Any help is appreciated. If I need to post more code, I will do that. Thanks!

+1  A: 

Are you trying to reference global variables from inside a function without declaring them global?

wallyk
A: 

The scripts are on the same domain. Variable scope is not the problem, as I am storing and displaying data received either by GET or returned from function SurveyAgent(). I apologize for leaving out the code that actually performs the select query. I didn't consider it important because I know SurveyAgent() is doing what it is supposed to do, else the first script would be unable to display the agent's first and last names in the browser. However, as requested, here is the full code from SurveyAgent():

$userID = $userID; // $userID is passed in by the function call.
$mySQLdatabase = EstablishConnection("database");
// EstablishConnection connects to the server and selects the database provided as an argument. This code is known good, as it is used in multiple PHP scripts on the same domain.
$mySQLselect = "SELECT lname, fname FROM table WHERE id_no='$userID';";
$mySQLquery = mysql_query("$mySQLselect") or die ("This user id does not exist.");
$mySQLrow = mysql_fetch_row($mySQLquery);
$sponsorData = $mySQLrow[1].".".$mySQLrow[0];
return $sponsorData;

So, that is the body of the function SurveyAgent(). I did use var_dump in two different places in my main script. The first time var_dump is called, it displays the variables' data and the information about them - and this meets expectations. The second time it is called, however, the variables are all empty! I use a hidden form input called 'sent' with a value of 'true' to indicate whether the form has been submitted or not.

Here is the full code, as it is right now:

$agentID = $_GET['agent'];
//$agentID = "000001";
$ipAddress = $_GET['ip_address'];
// Uncomment the following line and comment out the line after when a date is passed via GET.
//$date_generated = $_GET['date_generated'];
$date_generated = date("n/j/Y");
$time_generated = $_GET['time_generated'];
$first_name = $_GET['first_name'];
$last_name = $_GET['last_name'];
$mailing_address = $_GET['address1'];
$mailing_city = $_GET['city'];
$mailing_state = $_GET['state'];
$mailing_postal_code = $_GET['postal_code'];
$mailing_country = "US";
$primary_phone = $_GET['phone_number'];
$email = $_GET['email'];

$dateMassacre = explode("/", $date_generated);

$dateMassacre[0] = preg_replace("/[^\d]/", "", $dateMassacre[0]);
$dateMassacre[1] = preg_replace("/[^\d]/", "", $dateMassacre[1]);
$dateMassacre[2] = preg_replace("/[^\d]/", "", $dateMassacre[2]);

if ($dateMassacre[0] < 10)
{
    $dateMassacre[0] = "0".$dateMassacre[0];
}

if ($dateMassacre[1] < 10)
{
    $dateMassacre[1] = "0".$dateMassacre[1];
}

$date_generated = $dateMassacre[2]."-".$dateMassacre[0]."-".$dateMassacre[1];

$agentData = SurveyAgent($agentID);
$agentDataArray = explode(".", $agentData);
$agentFirstName = $agentDataArray[0];
$agentLastName = $agentDataArray[1];
$agentFullName = $agentFirstName." ".$agentLastName;

var_dump($agentFirstName, $agentLastName, $agentFullName, $agentID, $agentData, $agentDataArray); // var_dump #1
var_dump($first_name, $last_name, $mailing_address, $mailing_city, $mailing_postal_code, $mailing_state, $primary_phone, $email); // var_dump #2

if (isset($_POST['sent']))
{
    var_dump($agentFirstName, $agentLastName, $agentFullName, $agentID, $agentData, $agentDataArray); // var_dump #3
    var_dump($first_name, $last_name, $mailing_address, $mailing_city, $mailing_postal_code, $mailing_state, $primary_phone, $email); // var_dump #4

The code goes on from there to process the additional input from the form. I added a pair of var_dump calls, as above, when I get down past this point:

$first_name = $_POST['first_name'];
$firstNameFlag = ValidateFirstName($first_name, false);

$last_name = $_POST['last_name'];
$lastNameFlag = ValidateLastName($last_name, false);

$mailing_address = $_POST['mailing_address'];
$mailingAddressFlag = ValidateAddress($mailing_address, false);

$mailing_city = $_POST['mailing_city'];
$mailingCityFlag = ValidateCity($mailing_city, false);

$mailing_state = $_POST['mailing_state'];
$mailingStateFlag = ValidateState($mailing_state, false);

$mailing_postal_code = $_POST['mailing_postal_code'];
$mailingPostalCodeFlag = ValidatePostalCode($mailing_postal_code, false);

$primary_phone = $_POST['primary_phone'];
$primaryPhoneFlag = ValidateTelephoneAlternate($primary_phone, false);

$email = $_POST['email'];
$validator = new EmailAddressValidator();
if ($validator->check_email_address($email))
{
    $email = strtolower($email);
    $emailFlag = true;
}
else
{
    $emailFlag = false;
}

var_dump($agentFirstName, $agentLastName, $agentFullName, $agentID, $agentData, $agentDataArray); // var_dump #5
var_dump($first_name, $last_name, $mailing_address, $mailing_city, $mailing_postal_code, $mailing_state, $primary_phone, $email); // var_dump #6

The output of var_dump #1 before submitting the form: string(7) "Clement" string(5) "Smith" string(13) "Clement Smith" string(6) "000002" string(13) "Clement.Smith" array(2) { [0]=> string(7) "Clement" [1]=> string(5) "Smith" }

The output of var_dump #2 before submitting the form: string(6) "Norrin" string(4) "Radd" string(19) "1234 S. Shepherd Rd" string(9) "City Name" string(5) "12345" string(2) "WI" string(10) "1234567890" string(24) "[email protected]"

The output of var_dump #1, #3, and #5 after submitting the form: string(0) "" string(0) "" string(1) " " NULL string(1) "." array(2) { [0]=> string(0) "" [1]=> string(0) "" }

The output of var_dump #2 and #4 after submitting the form: NULL NULL NULL NULL NULL NULL NULL NULL

The output of var_dump #6 after submitting the form: string(6) "Norrin" string(4) "Radd" string(19) "1234 S. Shepherd Rd" string(9) "City Name" string(5) "12345" string(2) "WI" string(10) "1234567890" string(24) "[email protected]"

I find it interesting that the variables in the even numbered var_dump calls lose their data after the form is submitted, until after I have re-imported the data from POST. Another interesting fact is that if I use this commented out line "//$agentID = "000001";" (and comment out the GET line immediately above it), then the variables in the odd numbered var_dump calls never lose their data. Also, I'm not sure why some variables would be reported as "string(0)" and others as "NULL", but I don't consider that to be very important. Perhaps it is and, if so, please advise.

I am going to try adding form inputs from which I can re-acquire the agent's information after the form is submitted and see if that makes a difference. I'll post back with the results.

EDIT

I added a pair of hidden form inputs from which I can re-gain the agent's data from POST and this works! Thank you for the suggestion to use var_dump to track the problem because that was critical to being able to solve the problem.

Clem