4

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

<form method="post" name="chapters" action="ChaptersUpdate.php">
<input type='text' name='id' value='{$row['id']}'>
<input type='text' name='name' value='{$row['name']}'>
<input type='password' name='password' value={$row['password']};>
<input type='submit'>
</form>

I am looking for a way to make it such that when the form is submitted, the following data structure gets passed in $_POST:

 [chapter] => Array
  (
    [0] => Array
        (
            [id] => corresponding chapter ID
            [name] => corresponding chapter name
            [password] => corresponding chapter password
        )

    [1] => Array
        (
            [id] => corresponding chapter ID
            [name] => corresponding chapter name
            [password] => corresponding chapter password
        )

)

I tried various combinations of name='chapter[][id]' / name='chapter[][name]' / name='chapter[][password]' with little success. The array data structure never looks like what I want it to look like.

Any ideas?

2 Answers 2

2

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]'>
Sign up to request clarification or add additional context in comments.

Comments

0

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
        )

)

3 Comments

Right, so I am actually doing something similar. The PHP component that actually creates these input fields is: while ($row=mysql_fetch_array($result)) { echo "<input type='text' name='chapter[$i][id]' value='{$row['id']}'>"; } where $result = mysql_query("SELECT * FROM chapters"). Weird thing is, when I check the source code of the actual resulting HTML page, it doesn't show "chapter[1][id]"...it just shows "chapter[][id]". Could that be a part of the problem?
look at my code you would see for($i = 0; $i <3; $i++) ... what is when they are created ..
ohhhh, so in my case, would the for loop go within the while() loop that I have at the beginning?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.