Submitting Multiple Form fields in an array / loop

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

    Submitting Multiple Form fields in an array / loop

    I have the following form

    <FORM NAME="form1" METHOD="POST">
    <?php do { ?>
    <input name="approve[]" type="checkbox" id="approve[]" value="<?php echo
    $row_rs['ID']; ?>">
    <select name="select">
    <option value="1">optio n 1</option>
    <option value="2">optio n 2</option>
    <option value="3">optio n 3</option>
    </select>

    <?php } while ($row_rs = mysql_fetch_ass oc($rs)); ?>
    </FORM>


    I want to acheive a loop that inserts a record into a database that enters
    the id and the value they have selected (if the approve input box was
    ticked)

    so far I have this:
    <?
    foreach($approv e as $aID) {
    $insertSQL = "INSERT INTO TABLE('refID',' optionSelected' ) VALUES
    ('$aID','')";




    }

    ?>

    How do I enter the select value within this loop?
    I assumed that it would be just
    $insertSQL = "INSERT INTO TABLE('refID',' optionSelected' ) VALUES
    ('$aID','$selec t')";

    But how does the form know what select item is associated with what ID

    Hope this makes sense

    any help would be grateful

    Craig


  • R. Rajesh Jeba Anbiah

    #2
    Re: Submitting Multiple Form fields in an array / loop

    Craig Keightley wrote:[color=blue]
    > I have the following form
    >
    > <FORM NAME="form1" METHOD="POST">
    > <?php do { ?>
    > <input name="approve[]" type="checkbox" id="approve[]" value="<?php[/color]
    echo[color=blue]
    > $row_rs['ID']; ?>">
    > <select name="select">
    > <option value="1">optio n 1</option>
    > <option value="2">optio n 2</option>
    > <option value="3">optio n 3</option>
    > </select>
    >
    > <?php } while ($row_rs = mysql_fetch_ass oc($rs)); ?>
    > </FORM>
    >
    >
    > I want to acheive a loop that inserts a record into a database that[/color]
    enters[color=blue]
    > the id and the value they have selected (if the approve input box was[/color]
    [color=blue]
    > ticked)
    >
    > so far I have this:
    > <?
    > foreach($approv e as $aID) {
    > $insertSQL = "INSERT INTO TABLE('refID',' optionSelected' ) VALUES
    > ('$aID','')";
    > }
    >
    > ?>
    >
    > How do I enter the select value within this loop?
    > I assumed that it would be just
    > $insertSQL = "INSERT INTO TABLE('refID',' optionSelected' ) VALUES
    > ('$aID','$selec t')";
    >
    > But how does the form know what select item is associated with what[/color]
    ID

    When you render the HTML, try to make the fields associated with
    the IDs like <select name="foo[1]"> where 1 is the ID. Similarly for
    checkbox, etc. So, you make the fields associated with the ID. Then do
    a print_r($_POST) and see, how these values are submitted. You can then
    write the code to extract the values possibly using foreach().

    --
    <?php echo 'Just another PHP saint'; ?>
    Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

    Comment

    • Roy W. Andersen

      #3
      Re: Submitting Multiple Form fields in an array / loop

      Craig Keightley wrote:[color=blue]
      > How do I enter the select value within this loop?
      > I assumed that it would be just
      > $insertSQL = "INSERT INTO TABLE('refID',' optionSelected' ) VALUES
      > ('$aID','$selec t')";[/color]

      Not sure if this is what you're asking, but it sounds to me like you
      want a checkbox associated with a select-list, correct?

      In that case, give them names that you can use to match them to
      eachother. What I'd do is give the checkboxes names like checkbox[1] and
      the select-lists select[1], and checkbox[2] and select[2] and so on.

      Then, when the form is submitted, all checked checkboxes will be
      received as an array where the keys are the same as the array with the
      select-lists, so you can just go through all the select-lists as so:

      foreach ($_POST['select'] as $key=>$value) {
      if ($_POST['checkbox'][$key]) {
      do_sql_stuff;
      }
      }

      This will only do the SQL stuff if the checkbox with the same number as
      the current select-field was checked (an unckecked box doesn't get sent
      when you submit the form, so if a box is unchecked, the value of
      $_POST['checkbox'][number] is false).

      To see what actually gets sent, do a print_r($_POST) and examine the output.

      Also, you should grab the variables using $_POST['varname'] instead of
      just $varname, as that'll make your life much easier the day you realize
      having register_global s off is a good thing ;)


      Roy W. Andersen
      --
      ra at broadpark dot no / http://roy.netgoth.org/

      "Hey! What kind of party is this? There's no booze
      and only one hooker!" - Bender, Futurama

      Comment

      • Craig Keightley

        #4
        Re: Submitting Multiple Form fields in an array / loop

        Brilliant thats exactly what i need
        thanks for the advice

        craig
        "Roy W. Andersen" <roy-news@netgoth.or g> wrote in message
        news:34qeefF4ek 5t9U1@individua l.net...[color=blue]
        > Craig Keightley wrote:[color=green]
        >> How do I enter the select value within this loop?
        >> I assumed that it would be just
        >> $insertSQL = "INSERT INTO TABLE('refID',' optionSelected' ) VALUES
        >> ('$aID','$selec t')";[/color]
        >
        > Not sure if this is what you're asking, but it sounds to me like you want
        > a checkbox associated with a select-list, correct?
        >
        > In that case, give them names that you can use to match them to eachother.
        > What I'd do is give the checkboxes names like checkbox[1] and the
        > select-lists select[1], and checkbox[2] and select[2] and so on.
        >
        > Then, when the form is submitted, all checked checkboxes will be received
        > as an array where the keys are the same as the array with the
        > select-lists, so you can just go through all the select-lists as so:
        >
        > foreach ($_POST['select'] as $key=>$value) {
        > if ($_POST['checkbox'][$key]) {
        > do_sql_stuff;
        > }
        > }
        >
        > This will only do the SQL stuff if the checkbox with the same number as
        > the current select-field was checked (an unckecked box doesn't get sent
        > when you submit the form, so if a box is unchecked, the value of
        > $_POST['checkbox'][number] is false).
        >
        > To see what actually gets sent, do a print_r($_POST) and examine the
        > output.
        >
        > Also, you should grab the variables using $_POST['varname'] instead of
        > just $varname, as that'll make your life much easier the day you realize
        > having register_global s off is a good thing ;)
        >
        >
        > Roy W. Andersen
        > --
        > ra at broadpark dot no / http://roy.netgoth.org/
        >
        > "Hey! What kind of party is this? There's no booze
        > and only one hooker!" - Bender, Futurama[/color]


        Comment

        Working...