0

i am looking for an example on which implements two depending dropdowns. There are a zillion examples on the internet but they are not working within wordpress plugin. So this is what if have

In the JS File

    $('#am_organisation').change(function(){
        var val =  $(this).val();
         
        $.ajax({
            type: 'POST',
            url: 'reuse/get-contacts.php',
            data:'organisation_id='+val,
            success: function(data){
                $("#am_contact").html(data);
                //$("#loader").hide();
            },
            error: function(xhr, status, error) {
                alert(xhr.responseText);
              }
                    
            });
    });```

for some reason it cannot find the URL php file get-contacts.php. It is located in a subfolder folder from the page file

mypage.php /reuse/get-contacts/php /js/myjavascript.js

I have tried tgo put them all in the same folder, Changed the folder with ./ or ../../ or just / Nothing seems to work.

SO i am in a wordpress plugin. On mypage i have two dropdowns from which the second one should filtered data based on the selection of the first one. How do i managed this in a wordpress plugin.

Thanks for your time, marcel

1
  • 1
    You should never use a standalone PHP file that the browser directly talks to in a WP plugn. If you need to make AJAX requests to WordPress create a REST API endpoint using register_rest_route and you'll get a pretty URL you can make requests to at /wp-json. Having a dedcated PHP file is a massive security and maintenance problem, and very bad practice. – Tom J Nowell Jul 1 '20 at 13:52
-1
  1. Does the get-contacts.php script work ok if you load it directly, e.g. in your browser or from the command line, or in some test javascript?
  2. Maybe you don't have the path correct. Instead of providing a partial path you could provide an absolute path. Is the reuse directory in the root of your web space? If so set url like this:

(Edited for the URL you provided - this is probably how you should set it)

url: '/wp-content/plugins/harriecrm/admin/get-contacts.php',

Or for debugging you could also set the entire path, wherever it is, e.g.:

url: 'https://yoursite.com/somewhere/reuse/get-contacts.php',

Let me know if this helps

2
  • Hi Mozboz, it works with complete URL lvreuls.eu/wp-content/plugins/harriecrm/admin/get-contacts.php. but not with a relative url. i have no clue, other then that is has something to do with the fact that it is within a wordpress plugin – Marcel Vreuls Jul 1 '20 at 13:55
  • If it works with a complete URL, then it makes sense to provide the whole URL, like url: '/wp-content/plugins/harriecrm/admin/get-contacts.php'. Otherwise the URL will be taken relative to the URL of the browser. This is on the client side, so it's nothing to do with where the code comes from on the server side. – mozboz Jul 1 '20 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.