Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable via post/get to another file using JQuery/Ajax with changing mouse coordinates

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.

like image 211
user2524618 Avatar asked Dec 05 '25 05:12

user2524618


1 Answers

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>
like image 141
Alessandro Minoccheri Avatar answered Dec 07 '25 19:12

Alessandro Minoccheri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!