#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2

    How to get a variable when it has been parsed


    I am sorry if the title does not make sense, but I did not know what to call it. What I have is a page where admin can select which directory to delete image from and then it is parsed to a different page where another function is being fed the path of the directory so that it can pull image names from database. Then when I go to the other function which is to delete the image info from the database the path is gone, which I know why because the path is empty when I am parsing another form. So my question is how to get the path from the prior form that php is parsing so that I can use it in my delete image function and unlink function. I really dont know how to explain any better than that, sorry. Here is the relevant code:

    remove.func.php

    these are all the functions that I am using

    PHP Code:

    function grab_albums($db){

        
        
    $sql "SELECT * FROM `Group_1`";
                
        
    $q $db->prepare($sql);
                
        echo 
    '<p>Select Album to Delete Images from:<br /><br /><select name="albums"><option value="">Select One...</option>';
                
        if(
    $q->execute()){
                    
                    
            while(
    $row $q->fetch(PDO::FETCH_ASSOC)){
                    
                                            
                
    $a_name $row['a_name'];
                
                
    $path $row['dir_path'];
                        
                            
                echo 
    '<option value="'.$path.'">'.$a_name.'</option>';
                        
                        
            }
                
                
                    echo 
    '</select><br /><br />';
                
        }    


        
    $db null;

    }
        

        


    function 
    remove_image_setup($db$path){
        
        
        
    $sql "SELECT * FROM `Group_2` WHERE dir_path = :path";
                
        
    $q $db->prepare($sql);
                
        echo 
    '<p>Select Image to Remove:<br /><br /><select name="remove_image"><option value="remove_image">Select One...</option>';
                
        if(
    $q->execute(array(':path' => $path))){
                    
                    
            while(
    $row $q->fetch(PDO::FETCH_ASSOC)){                
                                            
                
                
    $image $row['image_name'];
                        
                            
                echo 
    '<option value="'.$image.'">'.$image.'</option>';
                        
                        
            }
                
                
                    echo 
    '</select><br /><br />';
                
                
        }
        
        
        
    $db null;

    }



    function 
    remove_image($file$db){    
                    
            
    $sql "DELETE FROM Group_2 WHERE image_name = :file";
        
            
    $q $db-> prepare($sql);
            
            if(
    $q->execute(array(
                
                
    ':file' => $file
                
                
    ))){
                
                return 
    true;
                
            }else{
                
                return 
    false;
                
            }    
        
        
    print_r($sql->errorInfo());

    select_album.php

    this is where grab albums function grabs the album info out of the database.

    PHP Code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>Conforming XHTML 1.0 Strict Template</title>

    </head>

    <body>
        
        
        <div style="margin:auto; width:500px; text-align:center;">
            
            
            <form method="post" action="remove_image.php">            
                        
                        
                <?php
                
                    
    include 'init.php';
                
                     
    grab_albums($db); 
                 
                 
    ?>
                        
                        
                <input type="submit" name="choose"  value="Choose Album" />
            
                    
            </form>
            
            
        </div>


    </body>
    </html>

    remove_image.php

    this is where the album info is being parsed and feeding the data to the args of the remove_image_setup function and I have another form being parsed in the same file to feed the unlink and remove_images function, but when I parse the form path is lost because it is a different form. I need path to complete the unlink and remove_images function.

    PHP Code:

    <?php

    include 'init.php';

        
    $path $_POST['albums'];
        
        
    $file $_POST['remove_image'];
        
        
    $file_name substr($path14);
        
        
    $image_path 'upload/' .$file_name'/' .$file;
        
        echo 
    $path;
        
        echo 
    $file_name;
        
        if(
    $_POST['remove']){
            
            if (
    unlink($image_path)) {
                
                
                if(
    remove_image($file $db) === TRUE){
                
                
                    echo 
    'You have successfully deleted '.$file.' !';
                
                
                } else {
                
                
                    echo 
    'Something went wrong with deleting '.$file' !';
                
        
                }
        
            
            }
        
            
        }


    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <title>Conforming XHTML 1.0 Strict Template</title>

    <link rel="stylesheet" href="style.css" type="text/css" />

    </head>

    <body>
        
        
        <div class="clean_up_form">
            
            
            <form method="post" action="">            
                        
                        
                <?php remove_image_setup($db$path); ?>
                        
                        
                <input type="submit" name="remove"  value="Choose Album" />
            
                    
            </form>
            
            
        </div>


    </body>
    </html>

    I was trying to use the get method, but could not figure out how to make it work with my script.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,002
    Rep Power
    376
    you probably need to save the path into a session variable so it persists, or use hiddent form element

    q. do you actually have three page process to remove an image? it seems so. your code is everywhere and is not easy to decipher.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    if you wanna pass a form input on, simply include it as a hidden input in the next form.

    You need 'albums' and 'remove_image' from the previous POST, right? Include them as hidden parameters in the 'clean_up_form'.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2
    Yes I do have a three page process just to delete an image and I know that there is an easier and cleaner way of doing it. I choose this way because I could not figure how to do it with javascript. I spent a total of a week trying to figure it by posting on forums, using google, and youtube. I just couldn't figure it out because I have 0 knowledge about javascript. Are you saying that my code is messy? I am open for any suggestions or critiques because how can someone help me if the can't understand my scripts. How do you think that I can make it better? Also, I would have to use session start to create a session variable, correct?
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    A session variable doesn't sound like a good idea, because this would permanently preselect an album, just because you've selected it once. That doesn't make sense from a user's perspective.

    Use a hidden field.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,002
    Rep Power
    376
    well technically with session you would need to do some housekeeping but yeah form field element would be better but more prone to being hijacked..

    also check if the user has the right permission to delete the image
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2
    I GOT IT!!!!! I figured out how to do it with the get method. Finally.

IMN logo majestic logo threadwatch logo seochat tools logo