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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16784936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:42:07  来源:igfitidea点击:

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

phpjavascriptajaxjquery

提问by sun

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 src 获取到 php 变量,因此我使用 jquery 函数在用户单击该图像时获取 img src。下面的 jquery 用于获取 img src

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

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

现在我尝试了这样的方法来获取 php 变量的 ipath 值

$.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.

我不确定是否正确使用 Ajax 任何人都可以帮助使用 Ajax 功能来完成这项工作吗?谢谢你。

回答by Robert

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

例如,当单击 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 code

html代码

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

in some.phpuse

some.php 中使用

 echo $_POST['param'];

to get value and if you used type:GETyou should use then $_GETto obtain value.

获得价值,如果你使用过,type:GET你应该使用 then$_GET来获得价值。

回答by Hasina

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"].

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

回答by Dr. Dan

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

您可以在 php 脚本中获取此值 $_POST['path']

回答by Just-lewis

this should help

这应该有帮助

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

in some.php

在一些.php

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

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

回答by HADI

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

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

回答by webGautam

Ajax Function

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;
?>

回答by k102

As written hereyou 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']

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