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.