I have a dropdown which i want to fill through jquery. Problem that i am facing is that i want to call a class function directly from jquery. eg My class_function PHP class contains get_locations function. how can i call this get_locations function using jquery .post method without introducing a third page?
4 Answers
The way I usually do that is to have an if statement at the top of the php page checking for a special mode.
jquery
$.get(
'page.php?mode=ajaxGetLocations&someVar=' + $('#someVar').val(),
function(data) { $('#myDropDown').html(data); }
);
PHP - near the top of the code
<?php
if (isset($_REQUEST['ajaxGetLocations'])
{
$someVar = $_REQUEST['someVar'];
// Get your data here
// Output your select box here
?>
<select>
...
</select>
<?php
exit; // You don't want to keep processing the page
}
?>
Comments
You can't, because jQuery and PHP run in entirely different environments - PHP on the server, jQuery on the client. jQuery runs after PHP is done.
You want to look into jQuery's Ajax functions to find out how to call a PHP script from Javascript.