How to call a function asynchronously in PHP?

How do you call a PHP function from an onchange equals in an HTML form?

  • Answer:

    You would have to do this using an AJAX request. The onchange event would trigger a Javascript function that would in turn send a request to the server, and handle the response. This requires a fair bit of back-end code for handling the AJAX, and I would recommend using an existing library for that purpose. jQuery is the one that I am familiar with, and you could handle that change event like so: <?php if(array_key_exists('mytext', $_POST)){ echo "The text you posted is "{$_POST['mytext']}""; exit; } ?> <script src="(jquery source URL here)" type="text/javascript"></script> <!-- ... --> <input type="text" id="mytext"></input> <div id="result"></div> <script type="text/javascript"> $("#mytext").change(function(){ $.post("myscript.php", {'mytext':$("#mytext").val()}, function(dat){ $("#result").HTML(dat); }); }); </script>

wiki.answers.com Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.