PHP - Mass form action button

Associate
Joined
13 Nov 2003
Posts
1,567
Location
Manchester
Hey all

Not too sure if the title makes sense - never mind - I want have a fairly simply CMS for a site and on the article management page there is a list of articles.

I want to add to the form - so that there is a tick box next to each one - and at the bottom of the list there is a drop down box with the various actions - ie delete, archive etc, and a submit button.

What is the best way of achieving this in PHP? I can do all the db stuff etc its just a case of getting all the values from the form into a useful form.

Any feedback or relevant URLS would be great

Thanks in advance
Aaron
 
Well you'll need to write in PHP a check via an IF statement to see whether the "form tick boxes" were ticked. You must give each tick so form of ID though so you can identify them and do the appropriate action.
 
it sounds like you're trying to do something like gmail's dropdown wotsit:

20080216-dru1sw4d159jkfyrt517ihdg5f.png


so you have a bunch of "things" that you want to be able to perform a series of actions (specified in the dropdown) to. If that's right, then I would do something like the following:

would be laid out like this:

Code:
<input type="checkbox" name="element[]" value="1" />

the value should be the record's primary key in the database, or any other unique identifier for that row. the name element[] will post as an array, so it will look like this:

Code:
array(
  [element] => array(
          [0] => 1
          [1] => 2
  )
)

allowing you to loop through and perform actions easily.

for your dropdown, do something like this:

Code:
<select name="action">
  <option value="">Perform an action...</option>
  <option value="del">Delete</option>
  <option value="read">Mark as Read</option>
</select>

give your option values short codes that you can refer to in the PHP when you've posted. Once the form is submitted, use a switch to see which action to perform on your selected records:

Code:
switch($_POST['action']) {
  case 'del':
    foreach ($_POST['element'] as $e) {
      DeleteByPK($e);
    }
  break;
  case 'read':
    foreach ($_POST['element'] as $e) {
      MarkReadByPK($e);
    }
  break;
  default:
    // do nothing, probably
  break;
}

the above assumes that you have a function called DeleteByPK(), which you pass a primary key, and it runs a mysql query deleting the record, and a function called MarkReadByPK() which does the same, except for marking read, rather than deleting.

hope that helps
 
Back
Top Bottom