PHP devloping a post variable name with a database entry

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

    PHP devloping a post variable name with a database entry

    I am developing a regisration page that will present a set of check
    boxes to the viewer. This list of checkboxes is developed from a list
    in a database so the amount and names of the boxes will change. This
    portion works just fine.

    The page content has a user name and password along with several sets
    of these checkboxes. All the form content of this page is presented in
    one form.

    The problem:

    I want to store the values presented in these checkboxes back into the
    database based on what the user selects. The data will be stored in a
    table that has the id for the user and the id for the checkbox item.
    As anyone knows, checkbox's only send a boolean value through POST so
    once the data is sent it is nearly impossible to test for its value
    and work on it since these are dynamically presented.

    What I mean by dynamically presented is that the list is pulled from a
    database. The checkboxes are build as such: <p><input
    type=\"checkbox \" name=\"{$row['name']}\">{$row['name']}</p>. $row is
    the reference to the database result set that has been affected by the
    row_assoc function.

    Since I have no idea what the contents of $row['name'] will be. I
    cannot use isset($_POST['checkboxname']) to test its value as you
    would normally do. As I see it 'checkboxname' is just a string and so
    it seems logical to me that this would work:
    isset($_POST['$row['name']']), but it doesn't.

    My request:

    I need to be able to check to see if the checkbox is check like such:
    isset($_POST['$row['name']']) so I can check the value of
    $_POST['$row['name']']. What is the correct way to run this check if
    it is at all possible? Or is there another way to do this or a more
    effective way to do this?
  • Pedro Graca

    #2
    Re: PHP devloping a post variable name with a database entry

    Bryan Wood wrote:[color=blue]
    > I need to be able to check to see if the checkbox is check like such:
    > isset($_POST['$row['name']']) so I can check the value of
    > $_POST['$row['name']']. What is the correct way to run this check if
    > it is at all possible? Or is there another way to do this or a more
    > effective way to do this?[/color]

    Maybe?
    <?php
    // if (isset($_POST['$row['name']'])) { /* whatever */ }
    if (isset($_POST[$row['name']])) { /* whatever */ }
    // no quotes ___^^__________ ^^______
    ?>


    But ... wouldn't it be better to write HTML more like:

    <?php
    echo '<input type="checkbox" name="cbox[', $row['name'], ']"/>';
    ?>

    and then use that in PHP without the need for SELECTing again from the
    DB?

    <?php
    foreach ($_POST['cbox'] as $k=>$v) {
    echo $k, ' selected<br/>';
    }
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • laidbak

      #3
      Re: PHP devloping a post variable name with a database entry

      >it seems logical to me that this would work:[color=blue]
      >isset($_POST['$row['name']']), but it doesn't.[/color]

      Instead of writing : isset($_POST['$row['name']']),
      Try the following:

      isset($_POST[ $row['name'] ])

      --
      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
      Wil Moore III, MCP Site : www.quicksitedesign.com?em
      Application Developer Site : www.digitallysmooth.com?em
      ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~

      Comment

      • martin

        #4
        Re: PHP devloping a post variable name with a database entry

        stufin24@ewu.ed u (Bryan Wood) wrote in message news:<ed9d4de4. 0401201117.600f 27b8@posting.go ogle.com>...[color=blue]
        > I am developing a regisration page that will present a set of check
        > boxes to the viewer. This list of checkboxes is developed from a list
        > in a database so the amount and names of the boxes will change. This
        > portion works just fine.
        >
        > The page content has a user name and password along with several sets
        > of these checkboxes. All the form content of this page is presented in
        > one form.
        >
        > The problem:
        >
        > I want to store the values presented in these checkboxes back into the
        > database based on what the user selects. The data will be stored in a
        > table that has the id for the user and the id for the checkbox item.
        > As anyone knows, checkbox's only send a boolean value through POST so
        > once the data is sent it is nearly impossible to test for its value
        > and work on it since these are dynamically presented.[/color]

        That may be abstractly true, but if a checkbox is checked then its
        "value" (whatever you set it to be) is submitted with the <form>.
        If it is not checked, then nothing is submitted for that checkbox's
        name. This is old from when I was new, but may give better ideas.

        for (each column in the row to be represented by a checkbox)
        {
        $column_name_or _value = <something>;

        echo("<p><input type=checkbox ");

        echo(" name=checkboxes[$checkbox_cntr] value=$column_n ame_or_value>") ;
        echo(" $column_name_or _value </p>");
        $checkbox_cntr+ +;
        }
        echo("<input type=hidden name=checkbox_c ntr value=$checkbox _cntr>");

        So, the checkboxes will be named $checkboxes[0], $checkboxes[1], etc.
        and $checkboxes can be treated like any array. (Have you seen that
        trick?) And, $checkbox_cntr will equal how many there are, checked
        or unchecked. Or maybe you already know how many checkboxes there
        will be. (I didn't.)
        (back where the submitted <form> is processed):

        for ($cntr=0; $cntr<$checkbox _cntr; $cntr++)
        {
        if ($checkboxes[$cntr] != "") // Valueless checkbox-name means unchecked.
        {
        /* Do whatever with the value of this checkbox */
        /* and the knowledge that it was checked. */
        }
        }

        [color=blue]
        >
        > What I mean by dynamically presented is that the list is pulled from a
        > database. The checkboxes are build as such: <p><input
        > type=\"checkbox \" name=\"{$row['name']}\">{$row['name']}</p>. $row is
        > the reference to the database result set that has been affected by the
        > row_assoc function.
        >
        > Since I have no idea what the contents of $row['name'] will be. I
        > cannot use isset($_POST['checkboxname']) to test its value as you
        > would normally do. As I see it 'checkboxname' is just a string and so
        > it seems logical to me that this would work:
        > isset($_POST['$row['name']']), but it doesn't.
        >
        > My request:
        >
        > I need to be able to check to see if the checkbox is check like such:
        > isset($_POST['$row['name']']) so I can check the value of
        > $_POST['$row['name']']. What is the correct way to run this check if
        > it is at all possible? Or is there another way to do this or a more
        > effective way to do this?[/color]

        Comment

        Working...