3

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 :

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

3
  • 2
    That's a horrible, horrible idea. Everytime the mouse moves one pixel you're sending an ajax request! Commented Jul 27, 2013 at 9:33
  • Actually, you're sending two ajax requests. Commented Jul 27, 2013 at 9:34
  • You have syntax problem; use ) instead } after "html" Commented Jul 27, 2013 at 9:39

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.