Mitro
Mitro

Reputation: 1260

Use a .PHP file from a .js in WordPress

I'm trying to read a php file from a js code in Wordpress.

$.ajax({
  type: "POST",
  url: "/set.php"

The problem is that the php file isn' t find in the directory, so at the end of js I have the alert of error.

How can I do to find the php file? Someone has a trick?

EDIT: i' ve just solved writing the full path as advised by your, but I' d like to write a "mini-path" without writing the full path. Some advised?

EDIT2: I' ve solved writing full path

Upvotes: 1

Views: 261

Answers (3)

Jeremy Harris
Jeremy Harris

Reputation: 24579

When you use the path "/set.php" it looks for that file at the root directory. If it is one directory up from you JS file, then what you probably meant to do is "../set.php" instead. If you can describe your folder structure a little better we can help you out more.

[EDIT]

When the PHP file is in the same folder as the JS file, you can make the AJAX call like this:

$.ajax({
  type: "POST",
  url: "set.php"

Upvotes: 1

barakadam
barakadam

Reputation: 2259

As cillosis said, just use :

$.ajax({
  type: "POST",
  url: "set.php"

Upvotes: 2

url: "/set.php" means that file have to be in the root directory of your site. if it's located in your templates folder, then you have to write full path from root instead of one slash.

Upvotes: 1

Related Questions