How to send or assign jquery variable value to php variable?

I wanted to get the img src variable for php when the user clicks on it, so I used the jquery function to get the img src when the user clicks on this image. JQuery to get img src

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

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

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

I'm not sure if Ajax is used correctly, can anyone help with the Ajax function to do this? Thanks.

+4
source share
7 answers

You must make an ajax call when the img button is pressed:

 $(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 the value, and if you used type:GET , you must use $_GET to get the value.

+4
source

try it. hope this helps.

 $("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 "imgSrc" on "somepage.php" like "$ _post [" imgSrc "].

+3
source

As written here , you should do it as follows:

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

and then in php your variable will be in $_POST['param']

+1
source
 $("img").click(function() { var ipath = $(this).attr('src'); $.ajax({ type:'POST', url: 'sample.php', dataType: 'HTML', data : { path: ipath }, success: function(data) { } });//end of ajax })//end of click 

You can get this value in php script as $_POST['path']

+1
source

this should help

 $("img").click(function() { jQuery.post("some.php",{param:$(this).attr('src')},function(data){ console.log(data); },'html'); }); 

in some.php

do a print_r($_POST); to understand how to pull out the necessary information / data

+1
source

try like this -

 $('document').ready(function(){ $("img").click(function() { var ipath = $(this).attr('src'); var dataString = 'imagePath='+ipath; var sendRquest = $.ajax({ type: 'POST', url: 'action.php', data: dataString }); sendRquest.done(function(responseData) { // your code here alert(responseData); }); sendRquest.fail(function(xmlhttprequest,textstatus,responseData) { // your code here alert('failed'); }); sendRquest.always(function(){ // your code here alert('done'); }); }); $("img").click(function() { var ipath = $(this).attr('src'); $('#divid').load('action.php?imagePath='+ipath); //if trigger required $('#divid').load('action.php?imagePath='+ipath, function() { alert('Load was performed.'); }); }); }); 

in action.php

 <?php echo $_POST['imagePath']; // if you are using load function then echo $_GET['imagePath']; ?> 
+1
source

Ajax function

 <script type="text/javascript"> $(function (){ $("#img").click(function() { var src = $(this).attr('src'); //OR// var src = $("#img").attr('src'); $.ajax({ type: "GET", url: "myfile.php", data: {imgsrc: src} }).done(function(data) { alert(data); }); }); }); </script> 

myfile.php

 <?php echo $_GET['imgsrc']; exit; ?> 
+1
source

Source: https://habr.com/ru/post/1483087/


All Articles