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


How to send or assign Jquery Variable value to php variable?

我想获得一个img src php变量当用户点击它,所以我使用jquery函数来获取img src当用户点击该图像。下面的jquery用于获取img src

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

现在我尝试这样做来获取PHP变量

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

我不确定是否正确使用Ajax,有人可以帮助Ajax函数来完成这项工作吗?谢谢你。

你应该在点击img时进行ajax调用,例如:

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

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

in some.php use

 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

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

Ajax函数

<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;
?>
$("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']