使用JQuery / Ajax通过post / get传递变量到另一个文件,并改变鼠标坐标

时间:2021-11-17 10:06:17

I am having index.php which will listens mouse coordinates on moving it. I want to send these coordinates to second file (say second.php). For this I have placed post fields inside mousemoving activity. But i am unable to receive these values in second.php file. Help me with this thing. I am attaching my code below :

我有index.php,它会在移动它时监听鼠标坐标。我想将这些坐标发送到第二个文件(比如second.php)。为此,我在postmoving活动中放置了post字段。但我无法在second.php文件中收到这些值。帮帮我这件事。我在下面附上我的代码:

<html>
<head>
<script type="text/javascript" src="jquery-latest.js"></script>
<script type="text/javascript">
jQuery(document).ready(function()
{
   $(document).mousemove(function(e)
   {
      $('#status').html(e.pageX);
      $('#status1').html(e.pageY);

        $.post("second.php",
        {
            x:10,
            y:20
        }, function coord(data)
        {   
            $("#div1").load("second.php");  },"html"

        }


   }); 



})

</script>
<body>

<h2 id='status'>
</h2>

<h2 id="status1">
</h2>
<div id="div1"></div>

</body>
</html>

and the second file normally receives post values and returns it after addition. Am i going on a wrong track or is there an alternate way for doing this. Help me with it.

并且第二个文件通常接收post值并在添加后返回它。我是在走错路还是有另一种方法可以做到这一点。帮助我。

2 个解决方案

#1


2  

I advise you to not do this thing, every time you move the mouse you send a request is very hard to manage think another way to do what you want.
but this code can works for your scope:

我建议你不要做这件事,每次你移动鼠标你发送一个请求是非常难以管理认为另一种方式做你想要的。但此代码适用于您的范围:

<script type="text/javascript">
jQuery(document).ready(function()
{
   $(document).mousemove(function(e)
   {
      $('#status').html(e.pageX);
      $('#status1').html(e.pageY);
      $.ajax({
        type: 'POST',
        url: 'second.php',
        data: { 
            'x': '10', 
            'y': '20' 
        },
        success: function(msg){
            //what you want after request
        }
    });
  });
</script>

#2


0  

It will be hard to manage for you cause of quick value changes, although you can send your data using below code, but I think it will be better to read data from 'second.php' on certain intervals.

尽管您可以使用以下代码发送数据,但很难为您快速更改值,但我认为在某些时间间隔内从'second.php'读取数据会更好。

$.ajax({
            type: "POST",
            async: true,
            url: 'second.php/Your_function_name',
            data: { 'x': anything , 'y': anything},       
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(r) { 
            alert(r.d); // returned value                         
            }
        });

If you decided to read data from second file, make a method in first file that returns an array of your values, then

如果您决定从第二个文件中读取数据,请在第一个文件中创建一个返回值数组的方法,然后

$(document).ready(function() {
GetMousePosition();
}
function GetMousePosition()
{
    $.ajax({
                type: "POST",
                async: true,
                url: 'first.php/GET_MOUSE_VALUES',     
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(r) { 
                MousePositionChanged(r.d[0] , r.d[1]);
                }
            });
     setTimeout(function {GetMousePosition();} , 1000);//Get Values every one second
}

EDIT: In second solution you may lose some values, that can be managed easily by creating an array that saves last (maybe) 5 mouse positions. finally sending the array. That's All

编辑:在第二个解决方案中,您可能会丢失一些值,可以通过创建一个节省最后(可能)5个鼠标位置的数组来轻松管理。最后发送阵列。就这样

#1


2  

I advise you to not do this thing, every time you move the mouse you send a request is very hard to manage think another way to do what you want.
but this code can works for your scope:

我建议你不要做这件事,每次你移动鼠标你发送一个请求是非常难以管理认为另一种方式做你想要的。但此代码适用于您的范围:

<script type="text/javascript">
jQuery(document).ready(function()
{
   $(document).mousemove(function(e)
   {
      $('#status').html(e.pageX);
      $('#status1').html(e.pageY);
      $.ajax({
        type: 'POST',
        url: 'second.php',
        data: { 
            'x': '10', 
            'y': '20' 
        },
        success: function(msg){
            //what you want after request
        }
    });
  });
</script>

#2


0  

It will be hard to manage for you cause of quick value changes, although you can send your data using below code, but I think it will be better to read data from 'second.php' on certain intervals.

尽管您可以使用以下代码发送数据,但很难为您快速更改值,但我认为在某些时间间隔内从'second.php'读取数据会更好。

$.ajax({
            type: "POST",
            async: true,
            url: 'second.php/Your_function_name',
            data: { 'x': anything , 'y': anything},       
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(r) { 
            alert(r.d); // returned value                         
            }
        });

If you decided to read data from second file, make a method in first file that returns an array of your values, then

如果您决定从第二个文件中读取数据,请在第一个文件中创建一个返回值数组的方法,然后

$(document).ready(function() {
GetMousePosition();
}
function GetMousePosition()
{
    $.ajax({
                type: "POST",
                async: true,
                url: 'first.php/GET_MOUSE_VALUES',     
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(r) { 
                MousePositionChanged(r.d[0] , r.d[1]);
                }
            });
     setTimeout(function {GetMousePosition();} , 1000);//Get Values every one second
}

EDIT: In second solution you may lose some values, that can be managed easily by creating an array that saves last (maybe) 5 mouse positions. finally sending the array. That's All

编辑:在第二个解决方案中,您可能会丢失一些值,可以通过创建一个节省最后(可能)5个鼠标位置的数组来轻松管理。最后发送阵列。就这样