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.
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.
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"].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With