Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    45
    Rep Power
    2

    Getting checked checkbox values


    Hello,

    I have a list of images that I want to the user to be able to select from then either delete or save the images selected.


    PHP Code:
    //Compare images in array to images in another directory and if they exist do two things:
    //1.)save: create new directory and move files to new directory
    //2.) delete files

    $array array_diff$arr1,$arr2)
    $dir = new RecursiveDirectoryIterator($dir_path); 
    foreach(new 
    RecursiveIteratorIterator($dir) as $file) {

            if (
    in_array(basename($file), $array)) { 
                
    $old_dir $dir_path .$dir '/images/'basename($file);
                
    $new_dir $dir_path .$dir '/temp/'basename($file); 
                               
                if (isset(
    $_POST['submit'])) { 
                     
    $selected_radio $_POST['rad'];

                     if (
    $selected_radio == 'save'){         
                        
    mkdir($dir_path .$dir '/temp/'0700); 
                        
    rename($old_dir,$new_dir);  
               
                      }elseif(
    $selected_radio == 'delete'){
                          
    unlink($old_dir);
                          };
                   };

             }; 
    }; 
    The form:

    <form method="post">

    PHP Code:
    // creates list of images from $array with checkboxes

    foreach($array as $key => $value){
    <
    input type="checkbox"  name="checkboxName[]" value="'.$value.'" />
    }; 


    PHP Code:
    //get checked images and save or delete

    if (isset($_POST['submit'])){ 
         
    $selected_radio $_POST['rad'];
         
    $post_arr $_POST['checkboxName']; 
         
    $checked_arr $array
                                    
        foreach (
    $checked_arr as $checked_key => $checked_value){
          if ((
    $selected_radio == 'save') && array_key_exists($checked_key$post_arr)){                  
               echo 
    'saved!';
         }elseif((
    $selected_radio == 'delete') && array_key_exists($checked_key$post_arr)){
               echo 
    'deleted!';
         };
                      
      };
     }; 
    </form>

    The result is no matter how many images I check it either saves or deletes all of images instead of just the checked ones.

    Can anyone please help and tell me what I did wrong? Thanks in advance.
  2. #2
  3. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,047
    Rep Power
    487
    I don't get your code ... I guess you're after something that presents the user two options per image (* EDIT > I presume that you want the user to be able to choose EITHER but not BOTH) yet you only seem to give them a single checkbox??

    Take this sample code for example ... this loops through an array (defined at runtime although you can of course use whatever data source you like to create it) and for each element/item of the array, presents the user with a radio button that allows them to choose if they want to DELETE or SAVE the item (of course, the script example below doesn't actually do anything with the user's selection other than present what was selected).

    PHP Code:
    <?php
    $array 
    = array("Item1""Item2""Item3""Item4""Item5""Item6");
    echo 
    "<form name='test' method='post'>";
    foreach(
    $array as $key => $value){
        echo 
    "{$value} : DELETE : <input type='radio' name='{$key}_radio' value='{$value}_delete'> SAVE : <input type='radio' name='{$key}_radio' value='{$value}_save'><br />";
    }
    echo 
    "<input type='submit' name='submit' value='submit'>";
    echo 
    "</form>";
    if (isset(
    $_POST['submit'])) {
        echo 
    "<pre>";
        
    print_r($_POST);
        echo 
    "</pre>";
    }
    ?>
    The result of posting/submittal is:-
    Array
    (
    [0_radio] => Item1_delete
    [1_radio] => Item2_save
    [2_radio] => Item3_save
    [3_radio] => Item4_delete
    [4_radio] => Item5_delete
    [5_radio] => Item6_save
    [submit] => submit
    )
    What is the result if you add the line

    print_r($_POST);

    into your code (after the isset($_POST... bit)?
    Last edited by badger_fruit; September 12th, 2012 at 05:18 AM.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    45
    Rep Power
    2
    Originally Posted by badger_fruit
    I don't get your code ... I guess you're after something that presents the user two options per image (* EDIT > I presume that you want the user to be able to choose EITHER but not BOTH) yet you only seem to give them a single checkbox??

    Take this sample code for example ... this loops through an array (defined at runtime although you can of course use whatever data source you like to create it) and for each element/item of the array, presents the user with a radio button that allows them to choose if they want to DELETE or SAVE the item (of course, the script example below doesn't actually do anything with the user's selection other than present what was selected).

    PHP Code:
    <?php
    $array 
    = array("Item1""Item2""Item3""Item4""Item5""Item6");
    echo 
    "<form name='test' method='post'>";
    foreach(
    $array as $key => $value){
        echo 
    "{$value} : DELETE : <input type='radio' name='{$key}_radio' value='{$value}_delete'> SAVE : <input type='radio' name='{$key}_radio' value='{$value}_save'><br />";
    }
    echo 
    "<input type='submit' name='submit' value='submit'>";
    echo 
    "</form>";
    if (isset(
    $_POST['submit'])) {
        echo 
    "<pre>";
        
    print_r($_POST);
        echo 
    "</pre>";
    }
    ?>
    The result of posting/submittal is:-


    What is the result if you add the line

    print_r($_POST);

    into your code (after the isset($_POST... bit)?
    Thank you for your help. What I was trying to do is have 1 checkbox per image then have two radio buttons at the bottom (one for 'save' and the other for 'delete'). You could either delete or save only the checked images on submit. Nothing happens to the unchecked images.

    I hope that makes sense. Is that even possible or should I do it your way?
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Make these radio buttons, with the name "action[$imageId]" and the value of "delete" and "save" respectively.

    Then $_POST['action'] will be an array:

    PHP Code:
    foreach ($_POST['action'] AS $imageId => $action ) {
      if (
    $action == 'save' ) {
         
    //save
      
    } elseif ( $action == 'delete' ) {
        
    //delete
      
    }

    Note that radio buttons can't be "unclicked" once one of them has been clicked, so maybe add a "do nothing" radio button, which is selected by default.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    45
    Rep Power
    2
    Originally Posted by ManiacDan
    Make these radio buttons, with the name "action[$imageId]" and the value of "delete" and "save" respectively.

    Then $_POST['action'] will be an array:

    PHP Code:
    foreach ($_POST['action'] AS $imageId => $action ) {
      if (
    $action == 'save' ) {
         
    //save
      
    } elseif ( $action == 'delete' ) {
        
    //delete
      
    }

    Note that radio buttons can't be "unclicked" once one of them has been clicked, so maybe add a "do nothing" radio button, which is selected by default.
    Hmm. Can't seem to get it to work. It's still saving all the images instead of the checked images. This is what I have following your example.

    <form method="post">
    PHP Code:

    foreach($array as $key => $value){
         <
    input type="checkbox"  name="checkboxName[]" value="'.$value.'" />
    }; 
    PHP Code:
    if (isset($_POST['submit'])){ 

         
    $post_arr $_POST['checkboxName']; 
         
    $checked_arr $array
                                    
        foreach (
    $checked_arr as $checked_key => $checked_value){
                        
          foreach (
    $_POST['action'] as $imageId => $action ) {

        if (
    $action == 'save'  && array_key_exists($checked_key$post_arr)) {
            echo 
    'saved!';
        }elseif ( 
    $action == 'delete' && array_key_exists($checked_key$post_arr) ) {
            echo 
    'deleted!';
            };
           };
        };
        }; 
    <input type="radio" name="action[$imageId]" value="save" />
    save<br />
    <input type="radio" name="action[$imageId]" value="delete" />
    delete<br />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    This is what I have following your example.
    That's nowhere near what I suggested.

    PHP Code:
    $images = array( 
      array(
    'filename' => '1.jpg''id' => 1),
      array(
    'filename' => '2.jpg''id' => 2),
      array(
    'filename' => '3.jpg''id' => 3),
      );
      
    echo 
    "<form method='POST'>";  
    foreach ( 
    $images as $image ) {
      echo 
    "<img src='images/{$image['filename']}' />";
      echo 
    "<br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='delete'> Delete <br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='save'> Save <br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='' selected='selected'> Do Nothing <br />";
    }
    echo 
    '</form>';









    foreach ( 
    $_POST['action'] as $imageId => $action ) {
      if ( 
    $action == 'delete' ) {
        echo 
    "DELETE IMAGE {$imageId}<br />";
      } elseif ( 
    $action == 'save' ) {
        echo 
    "SAVE IMAGE {$imageId}<br />";
      }

    Of course, your $images array is probably different.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    45
    Rep Power
    2
    Originally Posted by ManiacDan
    That's nowhere near what I suggested.

    PHP Code:
    $images = array( 
      array(
    'filename' => '1.jpg''id' => 1),
      array(
    'filename' => '2.jpg''id' => 2),
      array(
    'filename' => '3.jpg''id' => 3),
      );
      
    echo 
    "<form method='POST'>";  
    foreach ( 
    $images as $image ) {
      echo 
    "<img src='images/{$image['filename']}' />";
      echo 
    "<br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='delete'> Delete <br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='save'> Save <br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='' selected='selected'> Do Nothing <br />";
    }
    echo 
    '</form>';









    foreach ( 
    $_POST['action'] as $imageId => $action ) {
      if ( 
    $action == 'delete' ) {
        echo 
    "DELETE IMAGE {$imageId}<br />";
      } elseif ( 
    $action == 'save' ) {
        echo 
    "SAVE IMAGE {$imageId}<br />";
      }

    Of course, your $images array is probably different.
    Sorry, I thought I understood you but apparently I didn't. Thanks for your help. Quick question though.

    My image array is this:

    array(10) { [7]=> string(6) "a1.jpg" [8]=> string(6) "a2.jpg" [9]=> string(6) "a3.jpg" [10]=> string(6) "a4.jpg" [16]=> string(21) "a5.gif" [17]=> string(21) "a6.gif" [18]=> string(21) "a7.gif" [19]=> string(21) "a8.gif" [21]=> string(21) "a9.gif" [22]=> string(21) "a10.gif" }

    Would I then do something like this:

    foreach ( $images as $key => $image ) {
    echo "<img src='images/{$image[$key}' />";
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    If the key is the image ID and the value is the image filename, you would just do foreach ( $images as $imageId => $imageFilename )

    then replace the array accesses in my code with the correct variables from yours.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    45
    Rep Power
    2
    Originally Posted by ManiacDan
    That's nowhere near what I suggested.

    PHP Code:
    $images = array( 
      array(
    'filename' => '1.jpg''id' => 1),
      array(
    'filename' => '2.jpg''id' => 2),
      array(
    'filename' => '3.jpg''id' => 3),
      );
      
    echo 
    "<form method='POST'>";  
    foreach ( 
    $images as $image ) {
      echo 
    "<img src='images/{$image['filename']}' />";
      echo 
    "<br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='delete'> Delete <br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='save'> Save <br />";
      echo 
    "<input type='radio' name='action[{$image['id']}]' value='' selected='selected'> Do Nothing <br />";
    }
    echo 
    '</form>';









    foreach ( 
    $_POST['action'] as $imageId => $action ) {
      if ( 
    $action == 'delete' ) {
        echo 
    "DELETE IMAGE {$imageId}<br />";
      } elseif ( 
    $action == 'save' ) {
        echo 
    "SAVE IMAGE {$imageId}<br />";
      }

    Of course, your $images array is probably different.
    Currently, your code creates 3 radio buttons per image. I'm looking to create 1 checkbox per image and only 2 radio buttons. If an image is not selected does PHP do nothing to it or would I need a 3rd radio button telling it to do nothing to the unchecked images?

    Ex:

    1 checkbox per image

    [] a1.gif
    [] a2.gif
    [] a3.gif
    [] a4.gif
    [] a5.gif

    Select one radio button to save or delete the checked checkboxes
    () save
    () delete
    () do nothing -not sure if I need this-

    Submit form
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Oh, that's not what I thought you meant. I thought you wanted to save or delete each image individually.

    Name the checkboxes "images[]" with a value of $imageId

    Name the radio buttons both "action" and only use values "save" and "delete"

    Then $_POST['images'] is an array of image IDs, and $_POST['action'] is the action.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    45
    Rep Power
    2
    Originally Posted by ManiacDan
    Oh, that's not what I thought you meant. I thought you wanted to save or delete each image individually.

    Name the checkboxes "images[]" with a value of $imageId

    Name the radio buttons both "action" and only use values "save" and "delete"

    Then $_POST['images'] is an array of image IDs, and $_POST['action'] is the action.
    Sorry for the miscommunication and thanks for your patience. That's the idea I was going for originally but my code saves/deletes all the images in the array instead of just the checked ones. I have no idea where I screwed up.

    Here's my original code before I started testing out some of your code with mine:

    <form method="post">

    PHP Code:
    <?php

    //display checkbox next to each image

    echo '<ul>';
    foreach(
    $array as $key => $value){
    echo 
    '<li><input type="checkbox" class="checkbox"  name="checkBox[]" value="'.$value.'" />'.$value.'</li>';
    };
    echo 
    '</ul>';

    // on submit save or delete checked images. do nothing to the unchecked images.

    if (isset($_POST['submit'])){
    $selected_radio=$_POST['rad'];
    $post_arr $_POST['checkboxName'];

    foreach (
    $checked_arr as $checked_key => $checked_value){
    if ((
    $selected_radio == 'save') && array_key_exists($checked_key$post_arr)){
    echo 
    'saved!';
    };

    if ((
    $selected_radio == 'delete') && array_key_exists($checked_key$post_arr)){
    echo 
    'deleted!';
    };
    };
    };

    ?>
    <input type="radio" name="rad" value="save" />
    Save to temp folder<br />
    <input type="radio" name="rad" value="delete" />
    Delete<br />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    This code is very much invalid, it will throw fatal PHP errors and not do anything.

    Other comments:

    PHP Code:
    $selected_radio=$_POST['rad']; 
    You never print these.


    PHP Code:
    $post_arr $_POST['checkboxName']; 
    That's not the name of the post variable. Even if it was...


    PHP Code:
    foreach ($checked_arr as $checked_key => $checked_value){ 
    You made $post_arr and loop through $checked_arr

    PHP Code:
    && array_key_exists($checked_key$post_arr
    This serves no purpose.

    Fixed:
    PHP Code:
    <?php
    if ( isset( $_POST['submit'] ) ) {
      if ( 
    $_POST['rad'] == 'delete' ) {
        echo 
    "DELETE IMAGES" implode(','$_POST['images']) . "<br />";
      } elseif ( 
    $_POST['rad'] == 'save' ) {
        echo 
    "SAVE IMAGES" implode(','$_POST['images']) . "<br />";
      }
    }

    $images = array( 
      
    => '1.jpg',
      
    => '2.jpg',
      
    => '3.jpg',
      );
      
    echo 
    "<form method='POST'>";  
    foreach ( 
    $images as $imageId => $filename ) {
      echo 
    "<input type='radio' name='images[]' value='{$imageId}'>";
      echo 
    "<img src='images/{$filename}' />";
      echo 
    "<P />";
    }

    ?>
    <input type="radio" name="rad" value="save" />
    Save to temp folder<br />
    <input type="radio" name="rad" value="delete" />
    Delete<br />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    45
    Rep Power
    2
    Originally Posted by ManiacDan
    This code is very much invalid, it will throw fatal PHP errors and not do anything.

    Other comments:

    PHP Code:
    $selected_radio=$_POST['rad']; 
    You never print these.


    PHP Code:
    $post_arr $_POST['checkboxName']; 
    That's not the name of the post variable. Even if it was...


    PHP Code:
    foreach ($checked_arr as $checked_key => $checked_value){ 
    You made $post_arr and loop through $checked_arr

    PHP Code:
    && array_key_exists($checked_key$post_arr
    This serves no purpose.

    Fixed:
    PHP Code:
    <?php
    if ( isset( $_POST['submit'] ) ) {
      if ( 
    $_POST['rad'] == 'delete' ) {
        echo 
    "DELETE IMAGES" implode(','$_POST['images']) . "<br />";
      } elseif ( 
    $_POST['rad'] == 'save' ) {
        echo 
    "SAVE IMAGES" implode(','$_POST['images']) . "<br />";
      }
    }

    $images = array( 
      
    => '1.jpg',
      
    => '2.jpg',
      
    => '3.jpg',
      );
      
    echo 
    "<form method='POST'>";  
    foreach ( 
    $images as $imageId => $filename ) {
      echo 
    "<input type='radio' name='images[]' value='{$imageId}'>";
      echo 
    "<img src='images/{$filename}' />";
      echo 
    "<P />";
    }

    ?>
    <input type="radio" name="rad" value="save" />
    Save to temp folder<br />
    <input type="radio" name="rad" value="delete" />
    Delete<br />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    I'm sorry that was sloppy on my end for not cleaning up the code before posting. Unfortunately, it's still saving all the images instead of the checked ones. Actually, now it's just saving images, delete doesn't even work.

    I assume you meant to put checkbox here:

    echo "<input type='radio' name='images[]' value='{$imageId}'>";

    Now, I think the problem is in the actual code for saving and deleting:

    PHP Code:
        $images array_diff($arr1,$arr2// compare different image arrays

        
    $dir newRecursiveDirectoryIterator($dir_path);
        foreach(new 
    RecursiveIteratorIterator($dir) as $file){
          if (
    in_array(basename($file), $images)){

         
    $old_dir $dir_path.$dir.'/images/'.basename($file);
         
    $new_dir $dir_path.$dir.'/temp/'.basename($file);

        if (isset(
    $_POST['submit'])){

        if(
    $_POST['rad'] == 'save'){ // save to temp folder
              
    mkdir($dir_path.$dir.'/temp/',0700);
              
    rename($old_dir,$new_dir);

            }elseif(
    $_POST['rad'] == 'delete'){ // delete
             
    unlink($old_dir);
            };
         };
      };
    }; 
  26. #14
  27. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,047
    Rep Power
    487
    Hmm, this is a good one, perhaps you can use some javascript instead of PHP ... onclick of the checkbox it updates a hidden field with the ID of the image the user selected, separated with a delimiter such as |

    Then you can simply submit the hidden field, which, when parsed by PHP can see which Image IDs you selected and what action to perform.

    If that doesn't make sense, I will try to dig out some sample code which I used for something similar which might help to explain better!

    Regards
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  28. #15
  29. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,047
    Rep Power
    487
    I tried to find the javascript but it was nowhere to be found .. instead, here is a modified bit of code that I made earlier which I think may do the trick ...

    PHP Code:
    <?php
    // Create the array of images that the user needs to process
    $array = array("Item1.gif""Item2.gif""Item3.gif""Item4.gif""Item5.gif""Item6.gif");

    // Create and display the HTML form
    echo "<form name='test' method='post'>";

    // FOREACH item in the $array array, display a checkbox, give it a "shared name" so that when submitted it sends as an array, the values of the array are the values set in the "value=" element.  We can have different values but we MUST have the same name with [] to indicate we need to send ALL selected elements and not just the first or last one
    foreach($array as $key => $value){
        echo 
    "<input type='checkbox' name='cbox[]' value='{$value}'> {$value}<br />";
    }

    echo 
    "<br />";

    // Now we ask the user what shall we do, SAVE or DELETE (there is no need for a "Do nothing", if the user does not wish to process the image, they should not select its checkbox).
    echo "Select action : <br />";
    echo 
    "DELETE : <input type='radio' name='action' value='delete'><br />";
    echo 
    "SAVE : <input type='radio' name='action' value='save'><br />";

    // The submit button.
    echo "<input type='submit' name='submit' value='submit'>";
    echo 
    "</form>";

    // We check to see if the form has been submitted but checking to see if a $_POST variable "submit" isset ...
    if (isset($_POST['submit'])) {

        
    // FOR EACH item in the cbox array, we decide if the user selected DELETE or SAVE and call the appropriate function
        
    foreach ($_POST['cbox'] as $key => $value) {
            if (
    $_POST['action'] == "delete") { del_file($value); }
            if (
    $_POST['action'] == "save") { sav_file($value); }
        }    
    }

    function 
    del_file($fname) {
        echo 
    "Deleting {$fname} ... ";
        
    $old_dir "/srv/www/temp/";
        
    unlink("$old_dir}{$fname}");
        echo 
    " ... done!<br />";
    }

    function 
    sav_file($fname) {
        echo 
    "Saving {$fname} ... ";
        
    $old_dir "/srv/www/temp/";
        
    $old_dir "/srv/www/images/";
        
    rename("{$old_dir}{$fname}","{$new_dir}{$fname}"); 
        echo 
    " ... done!<br />";
    }

    ?>
    Of course, this might not work lol but maybe it will give you a start?
    Have fun
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo