如何将Jquery变量值发送或分配给php变量?

当用户点击它时,我想得到一个img src到php变量,所以当用户点击该图像时我使用jquery函数来获取img src.Below jquery用于获取img src

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

现在我尝试这样的东西来获取php变量的ipath值

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

我不确定正确使用Ajax任何人都可以帮助Ajaxfunction完成这项工作吗? 谢谢。

单击img时应该进行ajax调用,例如:

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

HTML代码

 image 

some.php中使用

  echo $_POST['param']; 

获取值,如果你使用type:GET你应该使用$_GET来获取值。

请试试这个。 希望它会有所帮助。

 $("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"); } }); }); 

尝试将“somepage.php”上的“imgSrc”作为“$ _post [”imgSrc“]获取。

如此处所写,您应该按如下方式进行:

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

然后在php中你的变量将在$_POST['param']

这应该有所帮助

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

在some.php中

做一个print_r($_POST); 了解如何提取所需的信息/数据

试试这样 –

 $('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.'); }); }); }); 

在action.php

  

Ajax函数

  

myfile.php

  
 $("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 

你可以在PHP脚本中获得这个值为$_POST['path']