开发者

multiple textarea with same name and PHP treatment

开发者 https://www.devze.com 2023-03-16 08:34 出处:网络
I have a dynamic fo开发者_JAVA百科rm in which i can add and remove textarea. The name of textareas is MyTextarea[]

I have a dynamic fo开发者_JAVA百科rm 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


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号