我正在创建一个使用会话变量来保留表单值的应用程序。表单的一部分是动态生成的。这部分没有正确保留变量,我收到了以下错误:
Notice: Undefined variable: childName1 in G:\PleskVhosts\insurancemidam.com\httpdocs\test\includes.php on line 639
现在看起来非常简单;但是,在我的SESSION变量上使用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) "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" }
和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 :(得分:1)
这是一个答案:你可以试着把:
var list = data.Where(!t.Area.HasValue || t => t.Area == response.Area).Select(t => new Status()
{
PK = t.PK,
Area = t.Area,
Description = t.Area.HasValue ? a : b,
OtherStuff = t.OtherStuff
}).ToList();
直接在之后:
global ${'childName'.$counter};
方法while($counter <= $numberOf) {
内的。这是访问displayChild()
的方式,如果它是全局变量。
以下评论不是答案,但在评论中并不合适。
而不是:
$childName1
你可以这样做:
if($_SESSION['formInput']) {
foreach($_SESSION['formInput'] as $key => $value) {
${$key} = $value;
}
}
请参阅:http://php.net/manual/en/function.extract.php
它还会阻止人们评论你的变量。
由于名称if($_SESSION['formInput']) extract($_SESSION['formInput']);
,我应该警告让用户输入直接确定你的PHP变量,这是一个坏主意。不要那样做。如果你真的必须这样做,那么至少在formInput
中使用前缀。