简体   繁体   中英

How to call an image alt by an onclick event using php?

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" class="card-img-top "  alt="'.$i.'" onclick="meghiv(this)"> </a>';
           
          $i++;
     }

on the new page

<?php

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

?>

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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM