Using Wordpress with update_user_meta and php array.

Had a quick question regarding dynamic input fields and posting it to php.

<input type="text" name="test[]" />

These fields get added dynamically as well using jquery which works fine, the issue im having is posting the array individually.

To save this I understand I have to do this.

$test = $_POST['test[]'];

I am using wordpress with this so i am saving the data to user_meta like so.

update_user_meta($user_id, 'test', $test_values);

Now in order for me to post this variable why doesnt this work.

$test = get_user_meta($user_id, 'test');

echo "$test[0]";

//...some more php here and add another variable from test

echo $test[1];

//....etc.

echo $test[2];

Thanks for any help guys and gals

Recommended Answers

All 5 Replies

moved to next reply.

Using Wordpress with update_user_meta and php array.

Had a quick question regarding dynamic input fields and posting it to php.

<input type="text" name="test[]" />

These fields get added dynamically as well using jquery which works fine, the issue im having is posting the array individually.

To save this I understand I have to do this.

$test = $_POST['test[]'];

I am using wordpress with this so i am saving the data to user_meta like so.

update_user_meta($user_id, 'test', $test_values);

Now in order for me to post this variable why doesnt this work.

$test = get_user_meta($user_id, 'test');

echo "$test[0]";

//...some more php here and add another variable from test

echo $test[1];

//....etc.

echo $test[2];

Thanks for any help guys and gals

Hey, you just you WHILE when calling the inputs in array. The code should be like this.

$test = $_POST['test'];
   $n        = count($test);
   $i        = 0;

  
   while ($i < $n)
   {
      echo "$test[$i]";
      $i++;
   }

hope that will helps.

Hey thanks for the reply...

That's what I would think and have tried it. What happens when I do this is it outputs only one letter of the word.

Do I need to use serialize or implode when inserting the data?

$test = implode(',',$_POST["test"]);		
update_user_meta ($user_id, "test", $test);

Then get the data with this

$test = explode(",",get_user_meta ($user_id, "test", true));

This is what I am trying right now but it doesn't seem to want to work, if you might have any ideas it would be very much appreciated.

Thanks

Oh forgot to mention this though...

while($i < count($test)) {
echo "$test[$i]";
$i++;
}

When I change $test[$i] to just $test it prints the whole word but I am unable to get only the first chunk.

Hi there,

I have finally figured it out, and it was so dumb, I feel like a total noob. =)

It seems that wordpress serializes and unserializes automatically.

So all that is needed is just $_POST - Thanks for the help though.

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.