0

This question is asked before but it is not working for me. I am using wordpress and I want to provide the location of the url "myfile.php" to my plugin directory but i don't know is it possible or not. Here is my javascript code:

    xmlhttp.open("GET","myfile.php?q="+str,true);
    xmlhttp.send();
  }
}

Manually it is working fine by providing complete location but is it possible to apply function to get myfile.php by using something like plugin_dir_path( __FILE__ ) Any help is highly appreciated

kate elly
  • 67
  • 9
  • 1
    A directory path to a file is unrelated to the URL. Javascript is client-side, it's requesting the PHP from the outside and needs a URL, not a path. – miken32 Dec 12 '20 at 20:23
  • Does this help? https://stackoverflow.com/questions/14863828/relative-path-for-open-method-of-a-xmlhttprequest-object or https://stackoverflow.com/questions/25329976/ajax-and-php-get-files-and-folders-list-using-recursiveiteratoriterator-as-neede or https://stackoverflow.com/questions/25329976/ajax-and-php-get-files-and-folders-list-using-recursiveiteratoriterator-as-neede or https://stackoverflow.com/questions/24727689/ajax-connection-with-php-not-working-at-subdirectory or https://stackoverflow.com/questions/39692014/ajax-post-to-a-php-file-in-a-directory-not-working – react_or_angluar Dec 12 '20 at 22:01

2 Answers2

1

Thanksfor all of your contrubution. Finally i get the solution by applying php function in it like this:

xmlhttp.open("GET","<?php
$ffi= plugin_dir_url( __FILE__ ) . '/myfile.php';
    echo $ffi;?>?q=" +str,true);
    xmlhttp.send();
  }
kate elly
  • 67
  • 9
0

I got the same result like this:

let pluginPath = "../wp-content/plugins/YOUR_PLUGIN_FOLDER_NAME/";

    xmlhttp.open("GET",pluginPath + "myfile.php?q="+str,true);
    xmlhttp.send();
  }
}