Problem with forms!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Problem with forms!

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Problem with forms!

Post 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 . ']">';
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Problem with forms!

Post by dajawu »

I am having trouble reading that variable, how should it go?

$_POST['name1[$tmp]'] doesn't seem to work
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Problem with forms!

Post 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 />';
    }
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Problem with forms!

Post 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).
Post Reply