How to display image name when clicked PHP

i have a text box with a button saying find image, you click it and all the images in the images folder show up, what can i do so when i click one of those images its name is put into the text box.

so say i click image of a flower, flower.gif would appear in the text box.


There are two ways you can do this. Ideally you’d do it BOTH ways so users with JavaScript (vast majority) get a much faster response, and users without still get the functionality.

The PHP way requires a page reload.
You’ll need to make each image of the results a link back to the current page with it’s file name in the request.

The markup your PHP needs to output might look a bit like this:

<a href='myscript.php?file=flower.gif'><img src='flower.gif' alt='Flower'></a> 
<a href='myscript.php?file=robot.png'><img src='robot.png' alt='Robot'></a> 

Your script will then use $_GET[‘file’] (if it exists) to populate the text box.

For the JS method you’ll need to add a listener to clicks on those links, prevent the link from following, and extract the file name from the image src or a href and set that value in the text box.

Are you already using a JS library on your site?

hi thanks for the reply, im using pure php at the moment.

is this same method possible if the name of the image is unknown, as they are given a random name based on the time when uploaded.

Thanks again

Yes, the sample HTML I gave above is generated by PHP. So when the user clicks to search/list the files, that list is produced by PHP, probably using [fphp]scandir[/fphp] function.

While you are looping the files in the directory you’ll be outputting that HTML. The file names are read from the file system when the page is requested.