2

I am almost embarrassed to put this up but I am perplexed and I am on a deadline. Probably a simple typo that I can't see.

I have these three lines of code: ($p is part of a POST array)

echo "<pre>";
print_r($p);
echo "Foo:".$p['fn2'];

And the output is this.

Array
(
    ['fn1'] => uobQEC8IGQcd
    ['fn2'] => P1ZVSXBJzloE
)
Foo:

Why doesn't $p['fn2'] print? This is driving me nuts. I shortened this for the example but there are many more FNs in there too and none will display individually, yet I can see them when I dump the array. Arrrgh!

UPDATE: on suggestion, a little more code... this is inside a foreach loop that starts:

foreach ($_POST['pairs'] as $key=>$p) {

UPDATE 2:

VARDUMP:

array(10) {
    ["'fn1'"]=> string(12) "uobQEC8IGQcd"
    ["'fn2'"]=> string(12) "P1ZVSXBJzloE"
    ["'state1'"]=> string(6) "active"
    ["'state2'"]=> string(6) "active"
    ["'crit1'"]=> string(2) "27"
    ["'crit2'"]=> string(2) "24"
    ["'warn1'"]=> string(2) "18"
    ["'warn2'"]=> string(2) "12"
    ["'owner2'"]=> string(5) "Adobe"
    ["'name2'"]=> string(5) "Rack1"
}

UPDATE 3: I am setting up the post variable arrays like this (in js)

$('<input>').attr({ type: 'hidden', name: "pairs["+fn1+"]['fn1']", value:fn1 }).appendTo('form');
$('<input>').attr({ type: 'hidden', name: "pairs["+fn1+"]['fn2']", value:fn2 }).appendTo('form');
0

3 Answers 3

1

It's likely not working because you got the keys enclosed in two sets of quotes, which leads to an undefined index error. If you remove a pair of the quotes around the keys it should work:

Notice in your keys: ["'fn1'"]

<?php

$p = array (  "state1" => "active",
              "state2" => "active",
              "crit1" => "27",
              "crit2" => "24",
              "warn1" => "18",
              "warn2" => "12",
              "owner2" => "Adobe",
              "name2" => "Rack1" 
            );

    echo "<pre>";
    print_r($p);
    echo "Foo:".$p['state2'];

?>

Output:

<pre>Array
(
    [state1] => active
    [state2] => active
    [crit1] => 27
    [crit2] => 24
    [warn1] => 18
    [warn2] => 12
    [owner2] => Adobe
    [name2] => Rack1
)
Foo:active
Sign up to request clarification or add additional context in comments.

Comments

0

Aha! Vardump revealed the answer... When I was setting up the arrays from returned javascript I was doing this:

    $('<input>').attr({ type: 'hidden', name: "pairs["+fn1+"]['fn1']", value:fn1 }).appendTo('form');

instead of this:

$('<input>').attr({ type: 'hidden', name: "pairs['+fn1+'][fn1]", value:fn1 }).appendTo('form');

Shouldn't have had single quotes around array elements...

Comments

0

If you write your html by this way.

<form method="post" action="test.php">
  <input name="value[test]"></input>
  <button type="submit">submit!</button>
</form>

You can access in the test.php by this way.

var_dump($_POST["value"]["test"]);

Otherwise, if you do this.

<form method="post" action="test.php">
  <input name="value['test']"></input>
  <button type="submit">submit!</button>
</form>

You will get a undefined index error if you try this.

var_dump($_POST["value"]["test"]);

Because now you should access like this.

var_dump($_POST["value"]["'test'"]);

In other words, the quotes are added automatically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.