KellyMarchewa KellyMarchewa - 4 months ago 6
PHP Question

Undefined variable in PHP - Output Echoes Fine Though?

I am creating an application that uses a session variable to retain form values. A part of the form is dynamically generated. This part is not retaining variables correctly and I am receiver the following error:

Notice: Undefined variable: childName1 in G:\PleskVhosts\insurancemidam.com\httpdocs\test\includes.php on line 639


Now this would seem pretty straightforward; however, using a var_dump on my SESSION variable reveals that this variable IS defined:

if($_SESSION['formInput']) {
foreach($_SESSION['formInput'] as $key => $value) {

${$key} = $value;

}

echo var_dump($_SESSION['formInput']);


outputs:

array(124) { ["hasSpouse"]=> string(0) "" ["children"]=> string(1) "1" ["action"]=> string(6) "enroll" ["firstName"]=> string(2) "FT" ["address"]=> string(12) "R R MO 63751" ["ssn"]=> string(9) "123123123" ["lastName"]=> string(4) "TEST" ["email"]=> string(7) "k@y.com" ["gender"]=> string(6) "female" ["location"]=> string(8) "BK #5866" ["birth"]=> string(10) "1919-11-20" ["insuranceRadio"]=> string(24) "ft employee Silver 11.00" ["insuranceDisplaySelection"]=> string(0) "" ["childName1"]=> string(1) "K" ["childBirthmonth1"]=> string(2) "03" ["childBirthday1"]=> string(2) "20" ["childBirthyear1"]=> string(4) "1918" ["childSSN1"]=> string(9) "123123123" ["childGender1"]=> string(6) "female" ["step1"]=> string(9) "stepChild" ["childAddress1"]=> string(0) "" ["childCity1"]=> string(0) "" ["childState1"]=> string(0) "" ["childZip1"]=> string(0) "" ["healthSig"]=> string(1) "K" ["healthSigDate"]=> string(10) "07/14/2016" ["primaryOneFirst"]=> string(1) "L" ["primaryOneLast"]=> string(1) "M" ["primaryOneAddress"]=> string(1) "R" ["primaryOneCity"]=> string(1) "R" ["primaryOneState"]=> string(2) "MO" ["primaryOneZip"]=> string(5) "63761" ["primaryOnePercent"]=> string(3) "100" ["primaryOneSSN"]=> string(9) "123123123" ["primaryOneRelationship"]=> string(3) "SEL" ["primaryTwoFirst"]=> string(0) "" ["primaryTwoLast"]=> string(0) "" ["primaryTwoAddress"]=> string(0) "" ["primaryTwoCity"]=> string(0) "" ["primaryTwoState"]=> string(0) "" ["primaryTwoZip"]=> string(0) "" ["primaryTwoPercent"]=> string(0) "" ["primaryTwoSSN"]=> string(0) "" ["primaryTwoRelationship"]=> string(0) "" ["contingentOneFirst"]=> string(0) "" ["contingentOneLast"]=> string(0) "" ["contingentOneAddress"]=> string(0) "" ["contingentOneCity"]=> string(0) "" ["contigentOneState"]=> string(0) "" ["contingentOneZip"]=> string(0) "" ["contingentOnePercent"]=> string(0) "" ["contingentOneSSN"]=> string(0) "" ["contingentOneRelationship"]=> string(0) "" ["contingentTwoFirst"]=> string(0) "" ["contingentTwoLast"]=> string(0) "" ["contingentTwoAddress"]=> string(0) "" ["contingentTwoCity"]=> string(0) "" ["contingentTwoState"]=> string(0) "" ["contingentTwoZip"]=> string(0) "" ["contingentTwoPercent"]=> string(0) "" ["contingentTwoSSN"]=> string(0) "" ["contingentTwoRelationship"]=> string(0) "" ["lifeSig"]=> string(5) "KELLY" ["lifeSigDate"]=> string(10) "09/14/2016" ["enrollVision"]=> string(5) "false" ["visionDependentFirst"]=> string(0) "" ["visionDependentLast"]=> string(0) "" ["visionDependentBirthmonth"]=> string(0) "" ["visionDependentBirthday"]=> string(2) "00" ["visionDependentBirthyear"]=> string(4) "1900" ["visionDependentGender"]=> string(4) "male" ["visionDependentSSN"]=> string(0) "" ["visionDependentTwoFirst"]=> string(0) "" ["visionDependentTwoLast"]=> string(0) "" ["visionDependentTwoBirthmonth"]=> string(0) "" ["visionDependentTwoBirthday"]=> string(2) "00" ["visionDependentTwoBirthyear"]=> string(4) "1900" ["visionDependentTwoGender"]=> string(4) "male" ["visionDependentTwoSSN"]=> string(0) "" ["visionDependentThreeFirst"]=> string(0) "" ["visionDependentThreeLast"]=> string(0) "" ["visionDependentThreeBirthmonth"]=> string(0) "" ["visionDependentThreeBirthday"]=> string(2) "00" ["visionDependentThreeBirthyear"]=> string(4) "1900" ["visionDependentThreeGender"]=> string(4) "male" ["visionDependentThreeSSN"]=> string(0) "" ["visionDependentFourFirst"]=> string(0) "" ["visionDependentFourLast"]=> string(0) "" ["visionDependentFourBirthmonth"]=> string(0) "" ["visionDependentFourBirthday"]=> string(2) "00" ["visionDependentFourBirthyear"]=> string(4) "1900" ["visionDependentFourGender"]=> string(4) "male" ["visionDependentFourSSN"]=> string(0) "" ["visionDependentFiveFirst"]=> string(0) "" ["visionDependentFiveLast"]=> string(0) "" ["visionDependentFiveBirthmonth"]=> string(0) "" ["visionDependentFiveBirthday"]=> string(2) "00" ["visionDependentFiveBirthyear"]=> string(4) "1900" ["visionDependentFiveGender"]=> string(4) "male" ["visionDependentFiveSSN"]=> string(0) "" ["visionDependentSixFirst"]=> string(0) "" ["visionDependentSixLast"]=> string(0) "" ["visionDependentSixBirthmonth"]=> string(0) "" ["visionDependentSixBirthday"]=> string(2) "00" ["visionDependentSixBirthyear"]=> string(4) "1900" ["visionDependentSixGender"]=> string(4) "male" ["visionDependentSixSSN"]=> string(0) "" ["visionDependentSevenFirst"]=> string(0) "" ["visionDependentSevenLast"]=> string(0) "" ["visionDependentSevenBirthmonth"]=> string(0) "" ["visionDependentSevenBirthday"]=> string(2) "00" ["visionDependentSevenBirthyear"]=> string(4) "1900" ["visionDependentSevenGender"]=> string(4) "male" ["visionDependentSevenSSN"]=> string(0) "" ["visionDependentEightFirst"]=> string(0) "" ["visionDependentEightLast"]=> string(0) "" ["visionDependentEightBirthmonth"]=> string(0) "" ["visionDependentEightBirthday"]=> string(2) "00" ["visionDependentEightBirthyear"]=> string(4) "1900" ["visionDependentEightGender"]=> string(4) "male" ["visionDependentEightSSN"]=> string(0) "" ["visionSig"]=> string(5) "KELLY" ["visionSigDate"]=> string(10) "09/28/1994" ["insuranceSubmit"]=> string(4) "true" }


and
echo "Name is $childName1";


outputs what it should
Name is K
in my case.

I am quite perplexed; I have been struggling with this for quite sometime and would appreciate any guidance.

If needed, the function that prints a part of the form:

public function displayChild($numberOf) {
$html ="<fieldset><h2 class='legend'>Eligible Children</h2><div class='message'>If you checked foster child/step child, or if your child is over the maximum age of 26 or handicapped, contact Rhonda Medlin for the necessary form</div>";
$counter = 1;
while($counter <= $numberOf) {

$html .= "<div class='childField' id='child{$counter}'><h3>Child Data {$counter}</h3><div class='row'><div class='formGroup colHalf'><label>Child Name: </label><input type='text' data-validation='[OPTIONAL, NAME]' data-validation-label='Field' name='childName{$counter}' value='{${'childName' . $counter}}' /></div><div class='formGroup colHalf'><label>Date of Birth:<select name='childBirthmonth{$counter}'>
<option value=''> </option>
<option value='01'>January
</option>
<option value='02'>February
</option>
<option value='03'>March
</option>
<option value='04'>April
</option>
<option value='05'>May
</option>
<option value='06'>June
</option>
<option value='07'>July
</option>
<option value='08'>August
</option>
<option value='09'>September
</option>
<option value='10'>October
</option>
<option value='11'>November
</option>
<option value='12'>December
</option>
</select>


<select name='childBirthday{$counter}'>"; for($i = 1; $i <= 31; $i++) {
if($i < 10) {
$i = "0" . $i;
} $html .= "<option value='$i'>$i</option>";
} $html .= "</select>
<select name='childBirthyear{$counter}'>";

for($i = 1900; $i <= date("Y"); $i++) {
$html .= "<option value='$i'>$i</option>";
} $html .= "</select></div>

</div>

<div class='row'><div class='formGroup colHalf'><label> SSN: </label><input type='text' placeholder='xxxxxxxxx' data-validation-message='Please enter a 9 digit SSN without dashes.' data-validation='[L==9, INTEGER]' value='{${childSSN . $counter}}'name='childSSN{$counter}' id='childSSN{$counter}' /></div><div class='formGroup colHalf'><label>Gender: </label><select data-validation='[NOTEMPTY]' value='{${childGender . $counter}}' name='childGender{$counter}'><option value='male'>Male</option><option value='female'>Female</option></select></div></div>

<div class='row'><label>Please Check All That Apply:</label></div><ul class='checkBoxGroup' /><li>Handicapped: <input type='checkbox' name='handicap{$counter}' value='handicap' /></li><li>Full-Time Student: <input type='checkbox' name='student{$counter}' value='student' /></li><li>Step Child: <input type='checkbox' name='step{$counter}' value='stepChild' /></li><li>Foster Child: <input type='checkbox' name='foster{$counter}' value='foster' /></li><li>Different Address (than yours): <input type='checkbox' name='diff{$counter}' value='differentAddress' /></li></ul>

<div class='childAddressBox'><div class='row'><div id='childAddress{$counter}'><div class='formGroup colHalf'><label>Address: </label><input type='text' value='{${childAddress . $counter}}'}' name='childAddress{$counter}' /></div><div class='formGroup colHalf'><label>City: </label><input type='text' size=20 value='{${childCity . $counter}}' name='childCity{$counter}' /></div></div>

<div class='row'><div class='formGroup colHalf'><label>State: </label><input type='text' size=2 value='{${childState . $counter}}' name='childState{$counter}'/></div><div class='formGroup colHalf'><label>Zip</label><input type='text' size=5 value='{${childZip . $counter}}' name='childZip{$counter}' /></div></div>
</div>
</div></div>";
$counter++;
}
echo $html;
}


and a portion of the page that calls it (same one I am using the $_SESSION['formInput'] variable in):

elseif ($hasChildren) { // has children, but no spouse

$pdoObject = $db->getHealthPlans($status, false, true);
echo $printer->displayTable($status, 'health', $pdoObject);

$pdoObject = $db->getDentalPlans($status, false, true);
echo $printer->displayTable($status, 'dental', $pdoObject);

$pdoObject = $db->getComboPlans($status, false, true);
echo $printer->displayTable($status, 'combo', $pdoObject);
echo "</div><div id='insuranceDisplayContainer'><h3>Selected Plan: </h3><h3 id='insuranceDisplay'></h3><input type='hidden' name='insuranceDisplaySelection' id='insuranceDisplaySelection' /></div>";

echo $printer->displayChild($children);
echo $printer->displayAgreement('health');

}


EDIT:

This is line 639:

` $html .= "<div class='childField' id='child{$counter}'><h3>Child Data {$counter}</h3><div class='row'><div class='formGroup colHalf'><label>Child Name: </label><input type='text' data-validation='[NAME]' data-validation-label='Field' name='childName{$counter}' value='{${'childName' . $counter}}' /></div><div class='formGroup colHalf'><label>Date of Birth:<select data-validation='[NOTEMPTY]' data-validation-message='Please make a selection.' name='childBirthmonth{$counter}'>

Answer

This is meant as an answer: You could try to put:

global ${'childName'.$counter};

directly after:

while($counter <= $numberOf) {

within the method displayChild(). This is the way to access $childName1, if it is a global variable.

The remark below is not an answer, but it doesn't fit very well in a comment.

Instead of doing:

if($_SESSION['formInput']) {
  foreach($_SESSION['formInput'] as $key => $value) {
    ${$key} = $value;
  }
}

You could do:

if($_SESSION['formInput']) extract($_SESSION['formInput']);

See: http://php.net/manual/en/function.extract.php

It also stops people from commenting on your variable variables.

Because of the name formInput, I should warn about letting user input directly determine your PHP variables, that's a bad idea. Don't do that. If you really have to do it then at least use a prefix in extract().

Comments