0

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?

0

4 Answers 4

4

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
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Have your PHP page upgraded with

if (isset($_GET['get_locations'])) {
    echo $this->get_locations();
    return;
}

and call it from jQuery with

$.ajax({
//..
data: "get_locations=1",
//...
});

Comments

0

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.

Comments

0

Ajax is used to make an HTTP request from the browser, using JavaScript, without leaving the page. so you cant call a class function directly... but you can handle it by allowing the constructor to call this function depending on the parameters you have passed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.