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

3
  • set.php will read content? Commented Dec 7, 2012 at 16:36
  • Yes, in set.php I have some actions Commented Dec 7, 2012 at 16:42
  • Maybe if you use Ajax the right way in WordPress... Commented Dec 8, 2012 at 1:29

3 Answers 3

2

As cillosis said, just use :

$.ajax({
  type: "POST",
  url: "set.php"
Sign up to request clarification or add additional context in comments.

5 Comments

then use url: "/wp-content/themes/yourthemename/js_directory/set.php"
Works without / at the left of wp-content, but isn' t nice :| Another method to do this, with less path?
What are you trying to do? To make it work or to make it short?
First work :D and if is possible also short, for js with WP I can use: src="<?php bloginfo('template_url'); ?>/js/file.js"> is there something similar with PHP?
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.
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.

1 Comment

So write full path? full path is very long and I' m not sure about it
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"

5 Comments

I have theme_directory -> js_directory -> file.js and file.php
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.
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 <script>alert('In php file');</script> 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.
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.