Search code examples
phphidden-field

how to send hidden details when click on image in while loop


this is my code

while ($row = $result->fetch_assoc())
{
$imgpath=$row['image_name'];
$id=$row['id'];
<input type="hidden" name="Id" value="' . $row['Id'] . '" >

here when i click send ID to next page

<img src="'.$imgpath.'" height="170" width="600" title="album-name" class="img-responsive" alt=" " />
}

can i use form for it?


Solution

  • jQuery AJAX will do the job. You just need to attach an event handler function for the click event to the image. We don'it need the hidden element, we will attach $row['id'] to the id attribute of the <img> element, then on the click, we post it via Ajax.

    Try this :

    //First, make the id attribute of the <img> equal to $id($row['id'])
    <img src="'.$imgpath.'" id="$id" height="170" width="600" title="album-name" class="img-responsive" alt=" " />
    

    Then, add the event handler :

    $('body').on('click','img',function(){
      var id = $(this).attr('id');// Get the id
      $.post( "target.php", {id : id}, function( data ) {//Post the id to the target page
         //Do whatever..
      });
    });