0

I am trying to get the pasted contents of a form text field to trigger an AJAX action which connects to a php script to deal with the data and then sends the relevant response.

The php script is working perfectly so I know the problem isn't there.

The jQuery function also works perfectly, by alerting whatever is pasted into the text field if I do this

$(document).ready(function(){

    $('input').on('paste', function () {

    var capture = this;

      setTimeout(function () {

          var url = $(capture).val();

          alert (url);

      }, 100);  

   });  
});

But when I try to add the AJAX business it all fails.

I'm obviously doing it wrong but I don't know the correct way to do it. The way I'm calling AJAX is the same method I use in a whole bunch of other scripts and it works fine in those so it must be to do with where it's being called.

I've tried (left off the doc ready to save clutter) :

$('input').on('paste', function () {

var capture = this;

    setTimeout(function () {

        var url = $(capture).val();

    }, 100);

        var data = {
                    'action': 'myAction',
                    'security': $( '#my-nonce' ).val(),
                    'url' : url,
                    'check' : check
                   };

        $.post(myajax.ajaxurl, data, function(response) {

                    alert(response);

        }           
});

And also tried :

$('input').on('paste', function () {

var capture = this;

    setTimeout(function () {

        var url = $(capture).val();

        var data = {
                    'action': 'myAction',
                    'security': $( '#my-nonce' ).val(),
                    'url' : url,
                    'check' : check
                   };

        $.post(myajax.ajaxurl, data, function(response) {

               alert(response);

        } 

    }, 100);

});

And I've also tried setting the url var directly in the data var :

var data = {
            'action': 'myAction',
            'security': $( '#my-nonce' ).val(),
            'url' : $(capture).val(),
            'check' : check
           };

But nothing is working.

I suspect this is to do with the setTimeout function. Is there a specific way of firing an AJAX call when also using setTimeout?

Just for clarity. I'm trying to trigger an AJAX call on paste event.

Grant
  • 1,297
  • 2
  • 16
  • 39
  • 1
    _Nothing is working._ Can you be a little more specific? Any errors in the console? – kecer Feb 02 '16 at 23:29
  • Where does `check` come from? And `myajax`? – BurningLights Feb 02 '16 at 23:30
  • check is part of nonce security being handled in the PHP script (it works). myajax is the localization path to my admin-ajax.php (wordpress). – Grant Feb 02 '16 at 23:35
  • @kecer no errors in the console. By nothing, I mean that nothing is happening. The AJAX call isn't firing the PHP script. – Grant Feb 02 '16 at 23:39
  • I recommend using debugger, set breakpoints and go step by step, verify everything is working as expected. – kecer Feb 02 '16 at 23:42
  • also include a failure callback for your ajax so you can figure what if it's ajax that went wrong or if ajax just never get called. – Ting Sun Feb 02 '16 at 23:53

1 Answers1

0

try to increase the time, I mean setTimeout(function(){... ,1000} try experimenting with different times.

If you get the answer then your post call is eating up the time.

Ronak
  • 1
  • 1
  • I wouldn't say that will work. Check [this answer](http://stackoverflow.com/a/1503425/3497425) and also comments. According to that, even 0ms delay should be enough – kecer Feb 02 '16 at 23:41
  • Yes the function already grabs the paste data with no problem set at 100. Am I putting the AJAX data in the right place? That's where it seems I'm going wrong. If I try putting the AJAX after the setTimeout function it doesn't work though. – Grant Feb 02 '16 at 23:47
  • Try removing setTimeout function. And see if your post gives any answer – Ronak Feb 02 '16 at 23:58
  • Removing the timeout doesn't give the script time to get the data, just returns a blank var – Grant Feb 03 '16 at 00:02
  • Maybe it's not possible to trigger AJAX actions on paste? – Grant Feb 03 '16 at 00:03
  • Ok this is really weird. I can't get ANY AJAX working on this page at all which means that there is an error elsewhere preventing me from performing any calls. – Grant Feb 03 '16 at 01:08
  • Found it, was a misplaced semi-colon. Nearly 5 hours that wasted.. lol – Grant Feb 03 '16 at 03:11