0

i have content listed in a div and i have a dropdown with various options to order and filter that content. I'm using ajax to filter/order that content and is working but i use other php page with the content i want on the div that has the content, like this

 function order(str){
    $.post("order_products.php",
    {
        q: str,
    },
    function(data, status){
      document.getElementById("txtHint").innerHTML = data;
    });
  }

What i wanted was to instead of putting the code (data) to change in another page just for that, i could put that code inside a class php function that i have.

<?php
class products{
    function list(){
        blablabla
    }

That way i would "save space" and organize everything, considering that i have many other things to order/filter but i don't know to to make the ajax request to that function, or if it's possible without having a page in between and then get the response from the function and put it on the div.

Mar10Bel
  • 41
  • 2
  • 6

1 Answers1

0

You can do this using Laravel by setting up a route to a function that will do the ordering. Please note I've made a lot of assumptions in the following answer as I can't see all your code and have made it quite general, please adjust the code to your project or provide more details of your code if you don't understand the answer fully.

routes.php

Route::post('products/order', [
  'as' => 'products.order',
  'uses' => 'ProductsController@orderProducts'
]);

Your view (assuming you're using blade)

$txtHint = $('#txtHint'); // grab the reference to txtHint dropdown
$.post( '{{ route("products.order") }}', // hit our route
    {
        q: str,
    },
    function(data, status){
      $txtHint.empty(); // clear the dropdown
      // loop through the data and assign each element to the dropdown
      $.each(data, function(value, key) {
        $txtHint.append($("<option></option>")
          .attr("value", value)
          .text(key));
      });
});

ProductsController.php

public function orderProducts()
{
  $orderBy = \Input::get('q');

  return \Products::lists('name', 'id')->orderBy($orderBy);
}

For outside of a framework just change the url to your php file and add in a data attribute for the method you require to be fired from the file.

$.post( 'products.php', // hit our route
        {
            action: 'order',
            q: str,
        },
  ...

Then in products.php you'd do something like this

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'order' : order();break;
        case 'otherFunction' : otherFunction();break;
    }
}

function order()
{
    // order logic here
    // get $_POST['q']
    // do your ordering
    // return data as json
}

See here for similar question: using jquery $.ajax to call a PHP function

Community
  • 1
  • 1
haakym
  • 12,050
  • 12
  • 70
  • 98
  • Thanks for the answer.i used it and made the adjustments necessary and it worked. Only know i noticed that i didn't said this but i also needed to apply this code without using frameworks, meaning without using controllers or routes, what would be more difficult, i suppose...That was what i wanted: the answer using frameworks and without using them, but was my mistake – Mar10Bel Jun 02 '15 at 14:18
  • You're welcome. Glad you got it working. Does this help for outside of a framework? http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function – haakym Jun 02 '15 at 14:27
  • Updated answer with brief explanation of **what** you need to change – haakym Jun 02 '15 at 14:35
  • It was helpfull in a certain way...the problem i face is that i don't know how to say, in other words, "go to this page, this function". I have a php file with only functions...I want the ajax to call that page, that specify function (function that echo what i want to put on the div).i don't know if i'm making my point here. – Mar10Bel Jun 02 '15 at 14:44
  • i used that code posted...In my php file with the functions i had to put the if outside the class and know it return this error:Call to undefined function order() – Mar10Bel Jun 02 '15 at 14:53
  • If you have a class with all functions lets call it `products.php`, just create a separate file for handling ajax requests for example `ajaxhandler.php` then put your if and switch logic to determine the method in the `ajaxhandler.php` file and at the top of the file you can import the `products.php` class with all the functions then you can be free to call any functions from the `products.php` file/class. Does that make sense? – haakym Jun 02 '15 at 14:56
  • you'd need to make an instance of the class also before calling any methods, that's why you got the error before. I'll update my answer. – haakym Jun 02 '15 at 15:01
  • No problem, well done for getting it working. Perhaps you can add your own answer to help others if they have a similar problem. Please vote my answer up if it helped or accept if you think it answered your question fully. Thanks. – haakym Jun 02 '15 at 15:22
  • 1
    I can vote yet.if i could i would but i can't so i shan't. Thanks :D – Mar10Bel Jun 02 '15 at 15:45
  • No worries. All the best with your project. – haakym Jun 02 '15 at 15:50