Jump to content

Weird Array Issue


tgavin

Recommended Posts

I'm working with a checkbox group called food[].

One of the checkboxes has the value of 'steak'. this is the one i'm testing with.

I'm using a function to build and print the checkbox.

The function accepts an array ($data) of names and values for each checkbox.

The name of the checkbox is contained in $data['name'], which is passed through the function.

if I var_dump($data['name']) from within the function it prints 'food[]'. perfect.

 

I'm now trying to get the POST values.

 

if i do this, i get an undefined index error

if(isset($_POST[$data['name']])) {
var_dump($_POST[$data['name']]);
}

 

if i do this, it prints 'steak'

if(isset($_POST['food'])) {
var_dump($_POST['food']);
}

 

what am i doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/269164-weird-array-issue/
Share on other sites

var_dump($data);

 

array(6) {

["type"]=>

string( 8 ) "checkbox"

["name"]=>

string(6) "food[]"

["value"]=>

string(5) "steak"

["string"]=>

string(10) "id="steak""

["checked"]=>

bool(false)

["required"]=>

NULL

}

 

print_r($_POST);

 

Array

 

(

[food] => Array

(

[0] => steak

)

 

[submit] => Submit

)

Link to comment
https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383234
Share on other sites

Right, $_POST "food" is an array, and I need to get the values of that array. Instead it returns an undefined index. That's where I'm having the difficulty. I was testing with a string to make sure info was actually being passed. now i see what that wasn't a good idea. sorry for the confusion. :)

 

This is basically what I'm trying to accomplish

 

if(isset($_POST[$data['name']])) {
   foreach($_POST[$data['name']] as $value) {
       if($_POST[$data['name']] == $data['value']) {
           $return .= ' checked="checked"';
       }
   }
}

Link to comment
https://forums.phpfreaks.com/topic/269164-weird-array-issue/#findComment-1383239
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.