Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Jquery变量值发送或分配给php变量?_Php_Javascript_Ajax_Jquery - Fatal编程技术网

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

如何将Jquery变量值发送或分配给php变量?,php,javascript,ajax,jquery,Php,Javascript,Ajax,Jquery,我想在用户单击时获取一个img src to php变量,所以我使用jquery函数在用户单击该图像时获取img src $("img").click(function() { var ipath = $(this).attr('src'); }) 现在,我尝试了这样的方法来获取php变量的ipath值 $.ajax({ type:'POST', url: 'sample.php', dataType: 'HTML', data: dataString, success: func

我想在用户单击时获取一个img src to php变量,所以我使用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,有人能帮助使用Ajax函数来完成这项工作吗? 谢谢。

如前所述,您应按以下步骤操作:

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

然后在php中,您的变量将位于
$\u POST['param']

单击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代码

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

要获取值,如果使用了
键入:get
,则应使用
$\u 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”上以“$\u post[“imgSrc”]的形式获取“imgSrc”

您可以在php脚本中以
$\u POST['path']

的形式获取该值,这应该会有所帮助

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

执行
print\r($\u 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.');
        });
    });
});
in 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>

$(函数(){
$(“#img”)。单击(函数(){
var src=$(this.attr('src');
//或者//var src=$(“#img”).attr('src');
$.ajax({
键入:“获取”,
url:“myfile.php”,
数据:{imgsrc:src}
}).完成(功能(数据){
警报(数据);
});
});
});
myfile.php

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


使用此脚本时会出现什么错误?没有错误..我不知道如何将此值获取到示例中可用的php。php我应该在使用此脚本之前使用表单操作和方法吗?您不必这样做,因为没有提交表单,只需单击操作:)我已使用html代码进行编辑。使用#myimg时,它只影响id=“myimg”的项“如果您在$(“img”)上使用,它将影响您单击的每个图像。我正在使用$(img)单独。当我检查sample.php文件时单击它后,其显示的错误错误抑制被忽略,请注意:未定义索引:param,因为您只将其称为some.php,而没有GET params,如果您调用它
some.php?param=asdfasdf
,则不会有任何通知。如果您使用$\u POST,那么不要打开some.php,只需查看js中发出警报的消息。如果javascript中没有错误,您的ajax请求将正常工作。这些都是最基本的。另外,
$(Img)
是不正确的,应该是
$('Img')
谢谢Robert。它说数据已保存,现在我如何访问sample中保存的数据。phpalert附带,,,etc首先查看您发送的内容。try-alert(ipath);在发送到ajaxthis displays array()之前,您可能使用了print而不是print\r,您应该在这些行周围收到一些信息。Array([param]=>)成功回调函数正在工作,但在sample.php中我收到错误通知:未定义索引:paramUse firebug+firephp调试sample中的此错误。php通知:未定义索引:imgsrc似乎您没有正确更改ajax函数!我建议您复制并粘贴,请注意页面名称是myfile.php而不是sample.php!试试这个,如果它仍然不工作,让我知道我会尽我最大的努力来解决它@rajIn在我提供的url中只显示sample.php,尽管它在sample中显示错误。php@raj我刚刚复制了我的工作脚本,这是绝对有效的!起初我也收到了同样的错误消息,后来我发现我的错误是在POST中获取imgsrc而不是在php文件中获取!请检查您的方法类型(GET/POST)和url:)我在单击图像时得到imgsrc,但不是在sample.php文件中
<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>
<?php
echo $_GET['imgsrc']; exit;
?>