multiple textarea with same name and PHP treatment

只谈情不闲聊 提交于 2019-12-02 11:06:16

问题


I have a dynamic form in which i can add and remove textarea. The name of textareas is MyTextarea[]

<textarea style="display:inline;" name="MyTextarea[]"></textarea>
<textarea style="display:inline;" name="MyTextarea[]"></textarea>

So when I want to treat this textarea with PHP i'm doing a :

echo $_POST['MyTextarea'];

So a Array is display on the screen, up to now it's ok

So I do a print_r($_POST['MyTextarea']); and I have again the same result : Array

I want to know if it's possible to have many textarea with same name with [] to generate an array.

If it's possible how can I do, or what's wrong with my code.

Thanks


回答1:


Which kind of framework are you using, I'm quite sure there is something at one point that is casting you're array into a string, maybe something that apply a treatment on POST variable like this:

foreach ($_POST as $key => $value) {
    if ($value && !$is_magic_quotes_gpc) {
        $_POST["$key"] = addslashes($value);
    }

In this case you've to remove this function... To be sure of what I'm talking about, you can try a var_dump($POST[MyTextarea]) =>string 'Array' (length=5) (should be an array)




回答2:


Yes, in php if you have an input field with a name like this "MyTextarea[]" is posted as an array.

So if you want to access your data, you have to do:

echo $_POST['MyTextarea'][0]; 

If you have multiple textareas with the same name, you'll get an array where each index has one textarea. The first textarea in the form is the first textarea in the array

you could do

foreach ($_POST['MyTextarea'] as $textarea){
//do wat you need
}

This is obviously a killer feature to use if you need to add multiple textareas dinamically.



来源:https://stackoverflow.com/questions/6520667/multiple-textarea-with-same-name-and-php-treatment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!