Page 1 of 1

Problem with forms!

Posted: Sun Jul 06, 2008 8:40 pm
by dajawu
I am trying to code a php form that is dynamic. I don't know how many items there will be ahead of time. My plan is the script reads items from a database then displays them with two checkboxes. One for accepting the item, one for deleting it. Now I can get one checkbox to work but not the second one.
What I did was store all the ID numbers in a Session variable and named the Checkbox by the same ID number. In processing I called up each item in that Session variable and read the checkbox that way. The problem is the second checkbox can't be named the same ID number so I tried something like this:

<input type="checkbox" name=D$ID>

then reading it with

$_POST[d$id];

but that doesn't work, any ideas how I can tackle this?

Re: Problem with forms!

Posted: Sun Jul 06, 2008 8:55 pm
by s.dot
Will multiple checkboxes be available? If so you should name them as arrays so they'll be posted as an array.

Code: Select all

echo '<input type="checkbox" name="name1[' . $yourIdHere . ']">
<input type="checkbox" name="name2[' . $yourIdHere . ']">';

Re: Problem with forms!

Posted: Sun Jul 06, 2008 9:36 pm
by dajawu
I am having trouble reading that variable, how should it go?

$_POST['name1[$tmp]'] doesn't seem to work

Re: Problem with forms!

Posted: Sun Jul 06, 2008 10:29 pm
by s.dot
You should read it like this (as it will be an array)

Code: Select all

if (!empty($_POST['name1']))
{
    foreach ($_POST['name1'] AS $key => $value)
    {
        echo $key . ': ' . $value . '<br />';
    }
}

Re: Problem with forms!

Posted: Sun Jul 06, 2008 11:43 pm
by Bill H
A couple of things that jumps out at me.
One is that PHP vars are case-sensitive. So name=D$ID is not going to be recognized by $_POST[d$id].
Also, dollar signs are reserved to ondicate a variable, and cannot be used within a variable name.

So "name=did" will be recognized by $_POST['did'] (note the single quotes in the "$_POST" iteration).