Search code examples
phpjavascriptclickexecute

How to recognise if user clicked out of text input and run php in that event


How can I use javascript to check, if the user clicks out of my text input class = "foo", and then, if the user has done so, execute a php file.


Solution

  • The easiest way to go about doing this, is to use jQuery.

    First, include jQuery in your <head></head> section:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
    

    Then add in the following Javascript:

    $(document).ready(function() {
      $('input.foo').blur(function(){
        $.ajax('myScript.php');
      });
    });
    

    This will setup the input (who's class is foo) to use ajax to run myScript.php whenever someone clicks out of it, which in development terms is called blurring.