how to get post from same name textboxes in php

杀马特。学长 韩版系。学妹 提交于 2019-12-20 07:13:33

问题


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?


回答1:


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 -->

But then you have to take care of the names yourself instead of letting PHP doing it.




回答2:


Create your text box with names txt[]

<input type='text' name='txt[]'>

And in PHP read them as

$alTxt= $_POST['txt'];
$N = count($alTxt);
    for($i=0; $i < $N; $i++)
    {
      echo($alTxt[$i]);
    }



回答3:


If you want name, you could name the input with txt[name1], then you could get it value from $_POST['txt']['name1']. $_POST['txt'] will be an associative array.



来源:https://stackoverflow.com/questions/9990367/how-to-get-post-from-same-name-textboxes-in-php

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