WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Ajax calls within Javascript files not working properly (2 posts)

  1. f00bar
    Member
    Posted 4 years ago #

    Hi everyone

    I'm having a little trouble with my ajax calls here.

    The site I'm working on never used to be on wordpress but since I ported it over I have come across a problem regarding the way my contact form works. First of all, there is a script called contact.js which has a function validating the fields in the form, and if successful it should make an ajax call to a file called ajax.php.

    Below is how it was implemented before:

    $.ajax({
      type: "POST",
    url: "ajax.php",
      data:  { trigger: "contactrequest",message: message,name: name,email: email,subject: subject,type: type },
      success: function(msg){
         alert( msg );
       }
     });

    This is using the jQuery method. Unfortunately the file cannot be found by wordpress as there is no absolute path. I don't want to hard-code it as I will be porting the site to a different server when done so there must be a way of somehow making sure it points in the right direction? The ajax.php file is in my theme root folder, and the contact.js which calls it is in a subfolder called "js".

    Any help in this matter is greatly appreciated as this is an important part of the site and needs to be implemented in this way.

    Thank you!
    Michael

  2. s_ha_dum
    Member
    Posted 4 years ago #

    WordPress loads jQuery in noConflict mode, so I'm wondering if that code works at all? You can only use the $ alias in special circumstances.

    Use get_bloginfo('template_url') to construct a path to ajax.php.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.