I am trying to write a func to replace a large chunk of repetitive code:
$inputVals = array( $siteId, $siteName, $numHspaBbu, $numLteBbu, $extAlarmsTerminated, $cellLowPower );
function updateVarVals( $inputVals ) { // assign values to $vars after Update button clicked
foreach( $inputVals as $val ) {
if ( $_POST[ $val ] !== "" ) { // if $_POST value is not empty then assign to $var and corresponding $_SESSION value
$val = $_SESSION[ $val ] = $_POST[ $val ];
} else { // re-assign $_SESSION value to $var
$val = $_SESSION[ $val ];
} // close IF
} // close FOREACH
} // close FUNC
updateVarVals( $inputVals );
but no matter how I quote $val I keep getting:
Notice: Undefined index: <$val value> in C:\xampp\htdocs\database on line 145.
for every iteration of the loop. Why is PHP expecting variable values to be defined here?
print_r( $inputVals );proves it. Then if user alters one of the inputs and clicks update, JS code detectson(change)and sends change via ajax.php file, which I am now trying to use to update php vars accordingly: if ( empty( $_POST[ $val ] ) { then re-assign $_SESSION[ $val ] to $val, otherwise assign changed input value to $var & $_SESSION[ $val ]. And after Update eventprint_r( $inputVals );again proves the changed values are assigned accordingly.