adding more fields to a form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnny

    adding more fields to a form

    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

    johnny

  • Adam Plocher

    #2
    Re: adding more fields to a form

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

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

    <?PHP

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

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

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

    if ($_POST['AddField'])
    {
    // if AddField button was clicked - increment $fc
    $fc++;
    }
    ?>
    <form action="<?PHP echo $_SERVER['PHP_SELF'];?>" method="post">
    <?PHP
    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;?>">
    </form>
    <?PHP
    }
    ?>

    Comment

    • Bosconian

      #3
      Re: adding more fields to a form

      "johnny" <mr_one1999@yah oo.com> wrote in message
      news:1137524462 .091908.55470@g 14g2000cwa.goog legroups.com...[color=blue]
      > 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
      >
      > johnny
      >[/color]

      Seems pretty straightforward and yes, PHP can handle everything including
      validation.

      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.


      Comment

      • Pedro Graca

        #4
        Re: adding more fields to a form

        Bosconian wrote:[color=blue]
        > "johnny" <mr_one1999@yah oo.com> wrote in message
        > news:1137524462 .091908.55470@g 14g2000cwa.goog legroups.com...[color=green]
        >> 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.[/color]
        >
        > Seems pretty straightforward and yes, PHP can handle everything including
        > validation.
        >
        > 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.[/color]

        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 <http://cfaj.freeshell. org/google>

        Comment

        • Bosconian

          #5
          Re: adding more fields to a form


          "Pedro Graca" <hexkid@dodgeit .com> wrote in message
          news:slrndsqog4 .h04.hexkid@ID-203069.user.ind ividual.net...[color=blue]
          > Bosconian wrote:[color=green]
          > > "johnny" <mr_one1999@yah oo.com> wrote in message
          > > news:1137524462 .091908.55470@g 14g2000cwa.goog legroups.com...[color=darkred]
          > >> 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.[/color]
          > >
          > > Seems pretty straightforward and yes, PHP can handle everything[/color][/color]
          including[color=blue][color=green]
          > > validation.
          > >
          > > I would simply have a form with a single input and submit button. Every[/color][/color]
          time[color=blue][color=green]
          > > an entry is submitted, the page refreshes displaying the same form[/color][/color]
          followed[color=blue][color=green]
          > > by the list of previously entered values. The form is always at the top[/color][/color]
          of[color=blue][color=green]
          > > the page for convenience. You should also inform the user the table[/color][/color]
          creation[color=blue][color=green]
          > > table was successful.[/color]
          >
          > 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 <http://cfaj.freeshell. org/google>[/color]

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


          Comment

          • johnny

            #6
            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.

            johnny

            Comment

            Working...