0

I am having a problem to receive array data from select option. Please see code below

Here is my html file

<?php

     $data = array(
             "id" => "1",
             "name" => "John"
     );

?>

<select name="member">
     <option value="`<?php echo $data; ?>`">John</option>
</select>

And here is my php file

$val = $_POST["member"];  
echo $val["id"];
4
  • 3
    Please try to re-word this question. Commented Aug 2, 2013 at 3:31
  • @CORRUPT you put an extra $ at the beginning of your statement. var_dump is a function, not a variable. Commented Aug 2, 2013 at 3:37
  • $val = $_POST['member'] is going to be a string, not an array. So $val['id'] will fail. Commented Aug 2, 2013 at 3:37
  • you can not echo array like you are echoing .. Commented Aug 2, 2013 at 3:39

3 Answers 3

1

If you do really want to pass an array from a form, I strongly recommend you to use serialize function.

So in your code.

<select name="member">
    <option value="`<?php echo serialize($data); ?>`">John</option>
</select>

Then in your back-end,

$data = unserialize($_POST["member"]); 

Try this. I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you mean?

<select name="member">
     <option value="<?php echo $data['id']; ?>">
     <?php echo $data['name']; ?>
     </option>
</select>

Because in your code, you use <?php echo $data; ?> which does not print the value in array, but the string Array which is the type of variable.

1 Comment

Sorry for my code and my bad english. I just want an array in option tag and I will use index of it in backend
0

Just use the ID and check on the server the other info. Something like this (PHP FIDDLE)(http://phpfiddle.org/main/code/mvr-zde)

2 Comments

Your fiddle won't work on the second submit, best check that.
I intended it for 1 submission. just re-add the data under the if statement if you'd want it to be show again.

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.