简体   繁体   中英

How to receive array data from post in php?

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"];

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.

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; ?> <?php echo $data; ?> which does not print the value in array, but the string Array which is the type of variable.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM