如何从PHP中的同名文本框中获取帖子 [英] how to get post from same name textboxes in php
问题描述
我有一个带有多个动态创建的文本框的表单,现在所有这些文本框都具有相同的名称,可以说txt
,现在有什么方法可以在进行表单处理时使用
I have a form with multiple textboxes which are created dynamically, now all these textboxes are of same name lets say txt
, now is there any way that when form processing happens we could read all the text boxes values using $_POST
method, which are of so called same name. If possible how?
推荐答案
您必须命名文本框txt[]
,以便PHP为您创建一个数字索引数组:
You have to name your textboxes txt[]
so PHP creates a numerically indexed array for you:
<?php
// $_POST['txt'][0] will be your first textbox
// $_POST['txt'][1] will be your second textbox
// etc.
var_dump( $_POST['txt'] );
// or
foreach ( $_POST['txt'] as $key => $value )
{
echo 'Textbox #'.htmlentities($key).' has this value: ';
echo htmlentities($value);
}
?>
否则,最后一个文本框的值将覆盖所有其他值!
Otherwise the last textbox' value will overwrite all other values!
您还可以创建关联数组:
You could also create associative arrays:
<input type="text" name="txt[numberOne]" />
<input type="text" name="txt[numberTwo]" />
<!-- etc -->
但是然后您必须自己照顾名称,而不是让PHP来做.
But then you have to take care of the names yourself instead of letting PHP doing it.
这篇关于如何从PHP中的同名文本框中获取帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!