I am learning PHP, I decided to implement a simple gallery. Everything works as you would like while the pictures are a bit :), but if there are a hundred of them then troubles arise. On the page, instead of the picture menu, instead of the content, too, an image. When you click on the picture in the menu, the image should change, but without reloading the page itself.

How to implement it using only PHP?

Thoroughly you can not explain, just an approximate direction, then I myself!

  • one
    Javascript We need to start with it - ArchDemon
  • 2
    It is not necessary to use AJAX. You can prepare a special php file in which the image will be generated. When clicking on an image, we execute JS: document.getElementById('изображение').src = '/get_image.php?rid='+Math.random(); - StasHappy
  • one
    @stashappy is a good approach, I would vote for such an answer) - Mi Ke Bu

2 answers 2

Reply @StasHappy:

Create a PHP script on the server that will output the address to the desired image, depending on the GET parameter passed. For example:

 $img = $_GET['image']; echo 'URL картинки'; 

Suppose we call this script get_image.php .

When clicking on an image, execute the Javascript code:

 document.getElementById('изображение').src = '/get_image.php?image='+Math.random(); 

    This can be implemented via ajax , on ajax you can download content without reloading the page. For example: click on the image in the menu, data is transferred via ajax (which data you need to download) to the php handler and the processed data is returned to the page in the specified block without reloading the page.

    • AJAX is completely useless. The @Stas solution is much better because it works on pure HTML + JS. By the way, ajax will not allow to upload a picture from another domain, if it is not allowed by it. - Daniel Shatz
    • @DanielShatz If you know the best implementation of this question, give your answer to this question. (here everyone can give answers to questions). - user185447