Current location: Hot Scripts Forums » Programming Languages » PHP » using image as button to see more images, how can I POST all updates of the variable?


using image as button to see more images, how can I POST all updates of the variable?

Reply
  #1 (permalink)  
Old 06-01-11, 01:40 PM
calx44 calx44 is offline
Newbie Coder
 
Join Date: Apr 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Question using image as button to see more images, how can I POST all updates of the variable?

Hi, I am fairly new to all this and i am just playing around at the minute.
I have the first image out of 2 directories on screen and I would like to be able to have these images act effectively as buttons, so that when you click on one, it brings up another page that displays the other images in that directory.

Here is the first page
PHP Code:

<?php                                                                                                                                                                                                                                                                                                                                                                        

session_start
();

    echo 
"<head><link rel=stylesheet href=../../lensstyles/lensstylesmain.css type=text/css></head>";
    
$user $_SESSION['user'];
    
    echo 
"Welcome to your album viewer " $user " <br>";
    echo 
"Here are the albums you have created on windsurfinglens.org:<br>";
    
    
$dir "../fileupload/uploadedimages/" $user;
    if (
file_exists($dir))
        {
        if (
$opendir opendir($dir))
            {
            echo 
"<table id='album_table'><tr bgcolor='#0000EF'>";
            
            while ((
$file readdir($opendir)) !== FALSE)
                {
                If (
$file != "." && $file != "..")
                    {                
                    echo 
"<td width='200px' height='20'px>";
                    echo 
$file;
                    echo 
"</td>";
                    
                    }
                }
                
            echo 
"</tr><tr bgcolor='#0000EF' height='200px'>";
        
            if (
$opendir opendir($dir))
                {
                while ((
$file readdir($opendir)) !== FALSE)
                    {
                    If (
$file != "." && $file != "..")
                        {
                        if (
$openal opendir($dir "/" $file))
                            {
                            
                            echo 
"<td width='200px' height='200px' bgcolor='#0000EF'>";
                            
$counter 1;                    
                            
                            while ((
$image readdir($openal)) !== FALSE && $counter 2)
                                {                            
                                if (
$image != "." && $image != "..")
                                    {
                            
                                    
$counter ++;    
                                    echo 
"<form action='imageviewer.php' method='post'>";                            
                                    echo 
"<input type='image' src=$dir/$file/$image width='180px'><br>";
                                    echo 
$file;
                                    echo 
"<input type='hidden' name='album' value=$file";
                                    }
                                }
                            echo 
"</td>";
                            }
                        }
                    }
                }        
            echo 
"</tr></table>";
            }
        else
            {
            echo 
"Failed to open directory.";
            }
    
        }
        
    else
        {
        echo 
"There is no directory under your name.";
        }
    

?>
Basically, the problem is that the $file variable changes each time while (($file = readdir($opendir)) !== FALSE) loops around, which is what i need for this page. However when this is sent in the hidden field of the form, it only sends the last value of $file (in this case it is "album 2").
Is there a way that I can get the correct value of $file (eg album 1or album 2) to be POSTed to the next page, because as you can see on the page, both images end up showing.../album2 on the second page. How could i change this, maybe by some how creating a new variable each time? I don't know.
Currently i have set the second page to only show the directory, not the image, to illustrate the problem.

The page is at http://www.windsurfblog.org/windsurf...lbumviewer.php

Sorry for the long message but any help would be appreciated
Regards
Callum
Reply With Quote
  #2 (permalink)  
Old 06-02-11, 03:46 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Is it absolutely necessary to POST the name of the album?

At the moment you're using a form to submit data to the next page, and load a certain folder based on the selected album. An easier way is to pass on the album as a GET parameter. That way you won't need a form, you don't need hidden fields, you only have to create a link. Like this:
HTML Code:
<a href="imageviewer.php?a=$file" title="View all images in $file"><img src="$dir/$file/$image" alt="$file" /></a>
In your imageviewer.php file, do this:
PHP Code:

$album $_GET['a']; 

Now you can get rid of the form, don't have to bother about unique hidden fields, etc... Plus, it has the advantage of permanent linking via, for instance, mysite.com/imageviewer.php?a=album1
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 06-02-11, 08:04 AM
calx44 calx44 is offline
Newbie Coder
 
Join Date: Apr 2011
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
thanks UnrealEd, you are a legend.

The thought of using GET never even crossed my mind. One question though, why doe sthis work? Why does GET pass the correct, different values of $file but POST does not?
Thanks again.
Callum
Reply With Quote
  #4 (permalink)  
Old 06-02-11, 08:43 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Your welcome!

Both POST and GET will send the correct values, the only reason it wasn't working in your code, is because the hidden field is set several times using the same name. A browser (at least the most commonly used ones) parses a form top to bottom, so when fields have the same name, only the last found value will be passed on (it's the same as overwriting an existing variable).

If you would still want to use a form, you would have to create a new form for each album, each having a hidden field named album. This would make the html code more complex (relatively of course), and actually is completely unnecessary.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks

Tags
image, post, update, variable, while loop


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Import images to a new image ? Mythvn PHP 4 12-12-07 06:35 AM
Help with rollover image submit button landboy HTML/XHTML/XML 3 05-30-04 11:17 AM
Put a small icon image into a HTML form button. birdie_nam_nam JavaScript 4 02-25-04 01:24 AM
image submit button problems Pepe PHP 10 12-22-03 08:15 PM


All times are GMT -5. The time now is 08:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.