Mikey
Mikey

Reputation: 365

Finding the URL of a PHP file when send an Ajax post (Wordpress)

I am developing a wordpress plugin and am having some trouble with the ajax side of things. What my plugin does is add a form and then validate the inputted data and store the values in a database.

My ajax post is as follows :

                $.ajax({

                type:"POST",
                url: "process.php",
                data: datastring,
                success: function() {

                    $('#formwrapper').html("div id='message'></div>");
                    $('#message').html("<h2>Contact form submitted!</h2>")
                    .append("<p>We will be in touch soon.</p>").hide().fadeIn(1500, function() {
                        $('#message').append("<img id='checkmark' src='images/check.png' />");
                    });
                }
            });

The above code is in a javascript file in a folder called 'js' which is in the root directory of my plugin. The problem is when the data is submitted the ajax post cannot find "process.php". It is stored in the root directory of my plugin folder. I am wondering if there is not maybe a function I am missing which will allow me to find the url of the php file relative to the javascript file without having to set an absolute url path everytime ?

Upvotes: 0

Views: 3256

Answers (3)

Max Langerak
Max Langerak

Reputation: 1207

When you want to access a file in a parent folder. You can use the ../ file to go up one directory. So to solve this, call:

../process.php

Upvotes: 0

Jai
Jai

Reputation: 74738

change this:

url: "process.php",

to this:

url: "../plugin/process.php",

As a side note you have missed a < in the ajax success here:

$('#formwrapper').html("<div id='message'></div>");
 //---------------------^-----------------------------here you missed '<'

Upvotes: 0

Paul L.
Paul L.

Reputation: 50

is your folder structure like this?

/
/js/file.js
/plugin/process.php

Because then you could just say

url: "../plugin/process.php",

or

url: "/plugin/process.php",

Upvotes: 0

Related Questions