How would I create this array structure in an HTML form?

后端 未结 2 1908
半阙折子戏
半阙折子戏 2021-01-21 03:41

I have the following HTML form, which is being generated dynamically from a table in a MySQL database.

相关标签:
2条回答
  • 2021-01-21 04:04

    The following appeared to work for me:

    <input type='text' name='chapters[0][id]'>
    <input type='text' name='chapters[0][name]'>
    <input type='password' name='chapters[0][password]'>
    
    <input type='text' name='chapters[1][id]'>
    <input type='text' name='chapters[1][name]'>
    <input type='password' name='chapters[1][password]'>
    
    0 讨论(0)
  • 2021-01-21 04:20

    You can simply create your form like this

    <form method="post" name="chapters">
    <?php 
    for($i = 0; $i <3; $i++)
    {
        echo "ID: <input type='text'  name='chapters[$i][id]' /> <br />";
        echo "Name: <input type='text' name='chapters[$i][name]' /> <br />";
        echo "Password: <input type='text' name='chapters[$i][password]' /> <br /> ";
        echo "<Br />";
    }
    ?>
    <input type='submit'>
    </form>
    

    Sample PHP

    if(isset($_POST['chapters']))
    {
        echo "<pre>";
        print_r($_POST['chapters']);
    }
    

    Sample Output

    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => Name1
                [password] => Password1
            )
    
        [1] => Array
            (
                [id] => 2
                [name] => name 2
                [password] => password 2
            )
    
        [2] => Array
            (
                [id] => 2
                [name] => name 3
                [password] => Password
            )
    
    )
    
    0 讨论(0)
提交回复
热议问题