1

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

Mitro
  • 1,230
  • 8
  • 32
  • 61

3 Answers3

2

As cillosis said, just use :

$.ajax({
  type: "POST",
  url: "set.php"
barakadam
  • 2,179
  • 1
  • 16
  • 15
  • 1
    then use url: "/wp-content/themes/yourthemename/js_directory/set.php" – barakadam Dec 07 '12 at 17:58
  • Works without / at the left of wp-content, but isn' t nice :| Another method to do this, with less path? – Mitro Dec 07 '12 at 18:00
  • What are you trying to do? To make it work or to make it short? – barakadam Dec 07 '12 at 18:01
  • First work :D and if is possible also short, for js with WP I can use: src="/js/file.js"> is there something similar with PHP? – Mitro Dec 07 '12 at 18:06
  • That is PHP you just quoted as an example. Do you mean is there something similar with JS? Answer: no : because bloginfo() is not PHP, it's WordPress, it's a function that gets data from the server database, whereas JS is client side. – barakadam Dec 07 '12 at 22:55
1

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"
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • I have theme_directory -> js_directory -> file.js and file.php – Mitro Dec 07 '12 at 16:40
  • file.php is in the same folder as file.js? If so, then just use *"set.php"* with no forward slash and it will find it. – Jeremy Harris Dec 07 '12 at 16:41
  • Yes are in the same folder, i remove the slash but I still have the problem, I put in the set.php file an alert to be sure that if the file is executed. Do you think that could be a good idea for tests? However it doesn' t work yet. – Mitro Dec 07 '12 at 16:49
  • 1
    No, that is not a good idea for testing. Use the developer tools in Google Chrome on the Network tab to watch the AJAX request being made. Look at the request and response headers to see if you are getting errors or not. – Jeremy Harris Dec 07 '12 at 16:52
  • Thank you man :D WordPress also without slash go in the root directory, I didn' t know that. so search the set.php in namesite.com/wp/set.php – Mitro Dec 07 '12 at 16:57
1

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.