Jump to content

PHP Form Handling Multiple Dynamic Fields *HELP*


monkeytooth

Recommended Posts

Ok think of gmail.. and how they list the mail in the inbox.. OK.

 

So my thing isnt nearly as elaberate, however I do have a ever expanding list of users in my database. I have a generic script that I made that lists them, and lists them with options to preform the needed maintencer on there accounts. And well with the DB getting larger its becoming 1 a burden to repeat each task individually to those I need to preform the task on and 2 very tedious. So I want to revise my script by adding a "Checkbox" next to every name (which I can do).

 

What I am looking for is how do I use the checkboxes, I know how to use them well enough when they are static within a form, but not dynamicly created.

 

How would I work it if I selected out of 100+ people on a list, only 10 people. Say I selected users 10, 20, 30, 45, 73, 88, 91, 92, 97, 103 (for examples sake)

 

How would I on submit of the form get it to recognize that I selected those? (we'll say for examples sake that the Checkbox Input name is "Chkbx_ID" and right after "ID" is added there ID number from the db to make each box unique in name (Which I can change if need be I just thought thats how it was done, but I can't wrap my mind around how to do this)

 

Well hopeful I haven't lost you with all the babble.. Can anyone help?

Link to comment
Share on other sites

For the checkboxes, for it like this, where $user_id is the users unique identification number

<input type="checkbox" name="Chkbx_ID[<?php echo $user_id ?>]" />

 

then on the page it posts to:

<?php
  if(is_array($_POST['Chkbx_ID'])){
    //Users have been selected
    foreach(array_keys($_POST['Chkbx_ID']) as $user_id){
      //Do actions on $user_id here
    }
  }
?>

Link to comment
Share on other sites

The best way to do this is to create the HTML for the checkboxes so that they are an array.  You can then differentiate the selected IDs by one of two means:

 

1) Set the array index to the ID

2) Set the value attribute to the ID

 

The goal is to have your HTML checkboxes look like this:

<input type="checkbox" name="selected[1]" />
<input type="checkbox" name="selected[2]" />
<input type="checkbox" name="selected[3]" />

 

This will cause $_POST['selected'] to be an array where each index in the array is the ID of a database item that was checked.

 

After you've modified your output to be like the above, add this at the top of your processing page and see if you can't figure out the rest:

<?php
  echo '<pre style="text-align: left;">' . print_r( $_POST['selected'], true ) . '</pre>';
?>

 

(edit) rhodesa beat me to it, but since I'd already typed out my response I just let it through.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.