2016-07-14 75 views
-1

我正在创建一个使用会话变量保留表单值的应用程序。表单的一部分是动态生成的。这部分没有正确固定变量和I AM接收以下错误: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; 
    } 

和调用它的页面的一部分(同一个我使用的$ _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'); 

} 

编辑:

这是线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}'> 
+0

其中哪一条是639行? – gladiola

+1

另外,你是否会以维持会话的方式反复运行相同的页面?像,可以从前面的运行或循环加载变量?很多次运行程序,没有打乱价值? – gladiola

+2

变量变量....这永远不是一个好兆头...... –

回答

1

这意味着作为一个答案:你可以尝试把:

global ${'childName'.$counter}; 

后直接:

方法displayChild()内。如果它是全局变量,则这是访问$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