Home > Software engineering >  How to call an image alt by an onclick event using php?
How to call an image alt by an onclick event using php?

Time:12-19

I want to open a new page and transfer data from an array. Using the name of the image seemed like the easiest way so that's what i want to do.

on the page i want to call it

function meghiv($img)
{
  $be=$img.alt;
  echo $be;
  session_start();
  $_SESSION['kod'] = $be;
}
for($j=0;$j<4;$j  )
     {
           echo '<a href="reszletes.php" title=""> <img src="'.$nevek[$i].'.png"   alt="'.$i.'" onclick="meghiv(this)"> </a>';
           
          $i  ;
     }

on the new page

<?php

session_start();
echo $_SESSION['kod'];

?>

CodePudding user response:

I don't know if it answers your question but try using javascript to load the image name into your php file

let images = document.querySelectorAll('.card-img-top'); // returns NodeList
let img_list = [...images]; // converts NodeList to Array
img_list.forEach(div => {
div.addEventListener("click", function(e){
    e.preventDefault()
    let alt = div.getAttribute("alt")
    window.open(`https://link.here/?alt=${alt}`, "_blank");
        })
    });

Then in your php file

 $image_name = $_GET['alt'];
  • Related