2016-07-14 2 views
-1

세션 변수를 사용하여 양식 값을 유지하는 응용 프로그램을 만듭니다. 양식의 일부가 동적으로 생성됩니다. 이 부분은 변수를 올바르게 유지하지 못하고 다음 오류가 발생합니다.PHP에서 정의되지 않은 변수 - 출력 에코는 괜찮습니까?

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

이제는 매우 간단 해 보입니다. 그러나, 내 세션 변수에 위해서 var_dump를 사용하여이 변수가 정의되는 것을 보여준다 :

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

${$key} = $value; 

} 

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

출력 :

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) "[email protected]" ["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" }

echo "Name is $childName1";

출력 어떤 내 경우 Name is K 것이다.

나는 꽤 당황 스럽다. 나는 언젠가 이것을 위해 고투하고있는 어떤지도든지 평가할 것입니다. 필요한 경우

폼의 일부 인쇄 기능 :

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

하고 호출 페이지의 일부분 (동일 하나 I는 $ _SESSION [ 'formInput'] 변수를 사용하고있다) :

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'); 

} 

편집 :

` $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}'> 
:이 라인 639은

입니다

+0

다음 중 639 행 중 어느 것입니까? – gladiola

+1

또한 세션을 유지하는 방식으로 동일한 페이지를 반복해서 실행하고 있습니까?마찬가지로 변수가 이전 실행 또는 루프에서로드 될 수 있습니까? 값을 설정하지 않고도 프로그램을 여러 번 실행합니까? – gladiola

+2

변수 변수 .... 절대 좋은 부호가 아닙니다. –

답변

1

이는 응답으로 의미 : 당신은 넣어 시도 할 수 :

global ${'childName'.$counter}; 

직접 후 : 방법 displayChild()

while($counter <= $numberOf) { 

. 전역 변수 인 경우 $childName1에 액세스하는 방법입니다.

아래의 설명은 답변이 아니지만 의견에 잘 맞지 않습니다. 대신 일을

:

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

당신은 할 수 :

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

참조 : 그것은 또한 당신의 변수 변수에 대한 주석에서 사람들을 중지 http://php.net/manual/en/function.extract.php

.

이름이 formInput이므로 사용자 입력에 직접 PHP 변수를 지정하게하는 것에 대해 경고해야합니다. 잘못된 생각입니다. 그러지 마. 당신이 정말로해야한다면 적어도 extract()에 접두어를 사용하십시오.

+0

조언을 주셔서 감사합니다. 물론, (당신이 언급했듯이) 문제를 해결하지는 못하지만 코드를 개선하는 모든 포인터에 대해서는 분명히 고맙게 생각합니다. – KellyMarchewa

+1

답변에 시도를 추가했습니다. 이 방법이 작동하는지 확신 할 수 없지만 메서드에'$ childName1'을 정의하지 않은 것 같습니다. –

+0

그게 효과가! 진심으로 감사드립니다. – KellyMarchewa