php - $session data returns only 0 -
i'm trying put script math user.
works fine when try put in session , try show value user return 0 if set 0.
know did wrong?
<?php session_start(); if( isset( $_server['http_x_requested_with'] ) ){ $class1 = filter_var($_post['class1'], filter_sanitize_string); $class2 = filter_var($_post['class2'], filter_sanitize_string); $class3 = filter_var($_post['class3'], filter_sanitize_string); $class4 = filter_var($_post['class4'], filter_sanitize_string); $class5 = filter_var($_post['class5'], filter_sanitize_string); $class1c = $class1 * 35; $class2c = $class2 * 5; $class3c = $class3 * 7.5; $class4c = $class4 * 26; $class5c = $class5 * 2.5; $totaal1 = $class1c + $class2c + $class3c + $class4c + $class5c; $res = array($class1c, $class2c, $class3c, $class4c, $class5c, $totaal1); foreach($res $name => $var) { $_session[$name] = $var; } $result = array("error" => false, "html" => null); $result["error"] = false; $result["html"] = "<h3>session information: var_dump($_session[$class1c]) ($_session[$class2c]) ($_session[$totaal1])</h3>"; } else { $result["error"] = true; $result["html"] = "<h3>error</h3>"; } echo json_encode($result); exit; ?>
you cannot call var_dump
inside double quoted string, , var_dump not return anything: display things.
even if could, $class1c
not valid index $_session
keeping same logic code, may change line following:
$result["html"] = "<h3>session information:"; ob_start(); var_dump($_session[0]); // contains $class1c echo $_session[1]; // contains $class2c echo $_session[5]; // contains $totaal1 $result["html"] .= ob_get_clean(); $result["html"] .= "</h3>";
edit: if want use indexes 'class2c', 'totaal1' etc.. need init $res
follow:
$res = array( 'class1c' => $class1c, 'class2c' => $class2c, 'class3c' => $class3c, 'class4c' => $class4c, 'class5c' => $class5c, 'totaal1' => $totaal1 );
then, loop set $_session
set correct indexes, , able use $_session['class1c']
proper values.
Comments
Post a Comment