how to get post from same name textboxes in php

后端 未结 3 1799
半阙折子戏
半阙折子戏 2021-01-26 06:25

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 proc

相关标签:
3条回答
  • 2021-01-26 06:53

    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.

    0 讨论(0)
  • 2021-01-26 07:10

    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]);
        }
    
    0 讨论(0)
  • 2021-01-26 07:17

    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.

    0 讨论(0)
提交回复
热议问题