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
Share on other sites

var_dump($data); What's in it?

The undefined index means that $data['name'] cannot be set to 'food'. It's set to something that doesn't exist in $_POST.

You can also do a print_r() on any of those arrays to see whats in them.

Link to comment
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

)

Edited by tgavin
Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.