By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,604 Members | 858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,604 IT Pros & Developers. It's quick & easy.

PHP devloping a post variable name with a database entry

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bryan Wood wrote:
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?


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 =--
Jul 17 '05 #2

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


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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~

Jul 17 '05 #3

P: n/a
st******@ewu.edu (Bryan Wood) wrote in message news:<ed**************************@posting.google. com>...
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.
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_name_or_value>");
echo(" $column_name_or_value </p>");
$checkbox_cntr++;
}
echo("<input type=hidden name=checkbox_cntr 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. */
}
}


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?

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.