Receiving form variables as an array

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

    Receiving form variables as an array

    I have setup an HTML page were the user will be able to set the number
    of text boxes they want to enter data in. The number of text boxes
    will be saved as a hidden form variable. Below is a quick HTML code
    that will show what im talking about.

    <Form>
    <input type="textbox" name="n1">
    <input type="textbox" name="n2">
    <input type="textbox" name="n3">
    <input type="hidden" name="num_boxes " value="3">


    When the user submits the data, it pass the info to a PHP page where
    the info will be used to insert into a mysql database. What is the
    best way to go about doing this? There are two methods i came up with,
    and 1 will be a pain to do, and the other im not sure how to
    accomplish.

    1. Perform if /else statements such as
    if($n1 != ""){
    insert $n1 into table
    }
    if($n2 != ""){
    insert $n2 into table
    and continue upto a max value

    This would work if there were 3 textboxes, however what if there are
    30? Then this is extremely inefficient.


    2. Save $n1 to $n3 into an array so that i could just perform a loop
    for(int $i = 0; $i<$num_boxes;$ i++){
    insert $array[i] into table

    is method two possible? If so, can someone point me in the right
    direction?
    Thanks for your help.
  • stephan beal

    #2
    Re: Receiving form variables as an array

    DG wrote:[color=blue]
    > 1. Perform if /else statements such as
    > if($n1 != ""){
    > insert $n1 into table
    > }
    > if($n2 != ""){
    > insert $n2 into table
    > and continue upto a max value
    >
    > This would work if there were 3 textboxes, however what if there are
    > 30? Then this is extremely inefficient.[/color]

    something like this will iterate until nX is not set:

    $i = 1;
    while( true ) {
    $varname = "n$i";
    if( ! isset( $_POST[$varname] ) ) break;
    $val = $$varname; # yes, that's two $$
    ....
    }

    of course, adjust $_POST to whatever you like.

    --
    ----- stephan beal
    Registered Linux User #71917 http://counter.li.org
    I speak for myself, not my employer. Contents may
    be hot. Slippery when wet. Reading disclaimers makes
    you go blind. Writing them is worse. You have been Warned.

    Comment

    • stephan beal

      #3
      Re: Receiving form variables as an array

      stephan beal wrote:[color=blue]
      > $val = $$varname; # yes, that's two $$[/color]

      That will only work if nX is in the global space. It is far better to pull
      it from $_{POST,GET}.


      --
      ----- stephan beal
      Registered Linux User #71917 http://counter.li.org
      I speak for myself, not my employer. Contents may
      be hot. Slippery when wet. Reading disclaimers makes
      you go blind. Writing them is worse. You have been Warned.

      Comment

      • stephan beal

        #4
        Re: Receiving form variables as an array

        stephan beal wrote:[color=blue]
        > $i = 1;
        > while( true ) {
        > $varname = "n$i";[/color]

        Sorry, i forgot this very important part: ++$i

        --
        ----- stephan beal
        Registered Linux User #71917 http://counter.li.org
        I speak for myself, not my employer. Contents may
        be hot. Slippery when wet. Reading disclaimers makes
        you go blind. Writing them is worse. You have been Warned.

        Comment

        • Jochen Daum

          #5
          Re: Receiving form variables as an array

          Hi DG!
          On 25 Jul 2003 07:47:34 -0700, programguru@hot mail.com (DG) wrote:
          [color=blue]
          >I have setup an HTML page were the user will be able to set the number
          >of text boxes they want to enter data in. The number of text boxes
          >will be saved as a hidden form variable. Below is a quick HTML code
          >that will show what im talking about.
          >
          ><Form>
          ><input type="textbox" name="n1">
          ><input type="textbox" name="n2">
          ><input type="textbox" name="n3">
          ><input type="hidden" name="num_boxes " value="3">
          >
          >
          >When the user submits the data, it pass the info to a PHP page where
          >the info will be used to insert into a mysql database. What is the
          >best way to go about doing this? There are two methods i came up with,
          >and 1 will be a pain to do, and the other im not sure how to
          >accomplish.
          >[/color]
          3.

          <input type="textbox" name="n[]">
          <input type="textbox" name="n[]">
          <input type="textbox" name="n[]">


          Try that...

          HTH, Jochen


          --
          Jochen Daum - CANS Ltd.
          PHP DB Edit Toolkit -- PHP scripts for building
          database editing interfaces.
          Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

          Comment

          Working...