I need to load a value from a recently posted page...

for ($i=0; $i<10; $i++)
{
     $_SESSION['someVar'][$i] = $_POST['someVar'][$i]
     $_SESSION['anotherVar'][$i] = $_POST['anotherVar'][$i]
     $_SESSION['yetAnotherVar'][$i] = $_POST['yetAnotherVar'][$i]
}

now the session part works just fine, but the $_POST part just blows up on me! How can I fix this? I want to keep the $_POST there, because I know I can just do $someVar[$i] and that will work but it's not what I want to do. I have seen a foreach loop and thought about that. But, would that keep all the variables tied together with the same index? I really dont want to use a foreach loop if I dont have to, but if thats what I need to use, then so be it.

Thanks!

Dani AI

Generated

Short answer: PHP is warning because $_POST['someVar'] isn’t actually an array in the request. As noted, indexing into $_POST only works if the form fields were submitted as an array or with repeated names. ’s "someVar1, someVar2…" trick will work but forces you to parse names; ’s define() isn’t relevant here.

A cleaner approach is to group related fields into a single nested array so each row's values share the same index. For example:

<!-- server-side generates one index per row so fields stay paired -->
<input type="text" name="rows[0][someVar]" value="">
<input type="text" name="rows[0][anotherVar]" value="">
<!-- repeat with rows[1] , rows[2] ... -->

When submitted you get $_POST['rows'][0]['someVar'], $_POST['rows'][0]['anotherVar'], etc. On the server validate and copy the structure into the session safely:

if (!empty($_POST['rows']) && is_array($_POST['rows'])) {
    foreach ($_POST['rows'] as $i => $row) {
        $clean = [
            'someVar'    => trim((string)($row['someVar'] ?? '')),
            'anotherVar' => trim((string)($row['anotherVar'] ?? '')),
        ];
        $_SESSION['rows'][$i] = $clean;
    }
}

Troubleshooting tips: use var_dump($_POST) to inspect exactly what the browser sent; wrap any indexed access with isset()/is_array() to avoid notices; if you must keep parallel arrays (someVar[], anotherVar[]) iterate one array by key and use that key to fetch the others so indices stay in sync. Always validate and sanitize before storing POST data in $_SESSION.

Recommended Answers

All 4 Replies

$_SESSION['someVar'][$i] = $_POST['someVar'][$i]

$_POST[$i] is not valid unless your 'someVar' FORM element is an array of checkboxes or you have multiple text fields with the same name. Normally, it would just be $_POST.

Whether it is an array or not, you can just do this:

$_SESSION['someVar'] = $_POST['someVar'];

It does not matter whether 'someVar' is an array or not--the session var will still receive the value.

How about this?

$_SESSION['someVar'][$i] = $_POST['someVar'.$i]

In this case, your form variables are someVar1, someVar2,...

why not using define function!?

commented: Reply is not even applicable to the topic. +0

val542, your reply makes no sense. The define() function does not even apply to this scenario.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.