Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send or assign Jquery Variable value to php variable? [duplicate]

I wanted to get a img src to php variable when a user clicks it so i used jquery function to get img src when user clicks that image.Below jquery is for fetching img src

$("img").click(function() {
    var ipath = $(this).attr('src');
})

now i tried something like this to get the ipath value to php variable

$.ajax({ type:'POST', url: 'sample.php',
 dataType: 'HTML', data: dataString,
 success: function(data)
{
 } 
});
});

I'm not sure about using Ajax correctly can anyone help with Ajax function to get this done? Thank you.

like image 740
sun Avatar asked Dec 16 '22 10:12

sun


2 Answers

You should make ajax call when img is clicked for example:

$(function (){
   $("#myimg").click(function() {
      $.ajax({
        type: "POST",
        url: "some.php",
        data: { param: $(this).attr('src'); }
      }).done(function( msg ) {
             alert( "Data Saved: " + msg );
     });
  });
}

html code

<img src="http://yourimage.jpg" alt="image" id="myimg" />

in some.php use

 echo $_POST['param'];

to get value and if you used type:GET you should use then $_GET to obtain value.

like image 112
Robert Avatar answered May 09 '23 07:05

Robert


please try this. hope it will help.

$("img").click(function() {
   var imgSrc = $(this).attr('src');

    jQuery.ajax({
            type: 'post',                    
            url:'somepage.php',            
            data:{"imgSrc" : imgSrc},
            dataType:'json',                
            success: function(rs)
            {
                alert("success");
            }
        });  
});

try to fetch "imgSrc" on "somepage.php" as "$_post["imgSrc"].

like image 23
Hasina Avatar answered May 09 '23 09:05

Hasina