adding more fields to a form

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hi all,

I wonder if I could do the following task in PHP before searching for
javascript which I prefer not to have in my code:

I have a form to create  new tables in a database and, since I can't
know how many columns needs each table, I want to load the page with  a
single text field in the form and then  with a  button  adding more
fields to the form if required.

so something like this:

text field
 "add more fields" button
"submit" button

everytime the "add more fields" button is clicked the page reloads and
the form has another text field to enter the name of the table column.

Can be done with php instead of javascript ?

Thanks in advance for any tip


Re: adding more fields to a form

Johnny, I'm not sure if I understand you correctly, but you can try

note.. I didn't test this code, but I think this is what you're looking


if ($_POST['Submit'])
    $field = $_POST['field'];

    for ($i = 0; $i < count($field); $i++)
        // echo all results
        echo $field[$i] ."<br />\n";
    // $fc = field count

    $fc = $_POST['fc'];
    if (!is_numeric($fc) || $fc == "")
        // if fc isn't set - default it to 1
        $fc = 1;

    if ($_POST['AddField'])
        // if AddField button was clicked - increment $fc
    <form action="<?PHP echo $_SERVER['PHP_SELF'];?>" method="post">
    for ($i = 1; $i <= $fc; $i++)
        echo "<input type=\"text\" name=\"field[". $i ."]\"
value=\"\"><br />\n";
    <input type="submit" name="AddField" value="add more fields">
    <input type="submit" name="Submit" value="submit">
    <input type="hidden" name="fc" value="<?PHP echo $i;?>">

Re: adding more fields to a form

sorry, I forgot to reply, thanks Adam, this is what I had to do.
For Bosconian and Pedro , yes it's part of an admin tool to create and
manage contact lists, each list has its own table.


Re: adding more fields to a form

Quoted text here. Click to load it

Seems pretty straightforward and yes, PHP can handle everything including

I would simply have a form with a single input and submit button. Every time
an entry is submitted, the page refreshes displaying the same form followed
by the list of previously entered values. The form is always at the top of
the page for convenience. You should also inform the user the table creation
table was successful.

Re: adding more fields to a form

Bosconian wrote:
Quoted text here. Click to load it

Only now did I notice that johnny wants to create tables.

johnny, unless you have a good reason to let users create tables:
    Don't do that.
    Don't allow users to create tables.
    Create all the tables you need before making your script available.

If you're posting through Google read <

Re: adding more fields to a form

Quoted text here. Click to load it

I complete agree. I assume this was some sort of admin tool and not for
public consumption.

Site Timeline