Support » Developing with WordPress » Javascript in WordPress

  • Resolved websista

    (@websista)


    I’m running this javascript in WordPress. It can’t find the page in this line of code. The page it is trying to open is in the same folder that the plugin’s main page that loads the javascript is located but the WordPress system is preventing the following line from finding or opening the file (gets a 404). I’m stumped. xmlhttp.open(“GET”,”getsubcat1.php?q=”+myarr[1],true);
    So it can’t find “getsubcat1.php” which is in the same directory as the plugin main page.
    Any ideas?
    —————————————-
    The line “window.alert(“showsubcat1 func status is NOT 200 , 1st this.readyState, then this.status” + this.readyState + ” ” + this.status );” is being tripped showing a 404 because the “if” statement ahead of it ( if (this.readyState==4 && this.status==200) {) fails when it tries to open -> xmlhttp.open(“GET”,”../getsubcat1.php?q=”+myarr[1],true);

    function showSubCat1(str, wp_plugin_path) {
    var myarr = str.split(“:”);

    sessionStorage.setItem(‘catid’, myarr[1]);
    if (str==””) {
    document.getElementById(“txtHint1″).innerHTML=””;
    return;
    }
    window.alert(“showsubcat1 func window.location.pathname ” + window.location.pathname );

    if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
    }
    xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
    window.alert(“showsubcat1 func status is 200 ” + window.location.pathname );
    document.getElementById(“txtHint1”).innerHTML=this.responseText;
    document.getElementById(“txtHint2”).innerHTML=still_more_cats;

    }
    else
    {
    window.alert(“showsubcat1 func status is NOT 200 , 1st this.readyState, then this.status” + this.readyState + ” ” + this.status );
    window.alert(“showsubcat1 func wp_plugin_path ” + “/plugins/wordpress_plugin-master/getsubcat1.php?q=”+myarr[1] );

    }
    }
    document.getElementById(“category_id”).value = myarr[1];
    document.getElementById(“category_name”).value = myarr[2];
    xmlhttp.open(“GET”,”../getsubcat1.php?q=”+myarr[1],true);
    xmlhttp.send();
    }

    • This topic was modified 2 years, 1 month ago by websista.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I can’t explain what’s wrong, but in general, relative paths in WP is generally a bad idea. Build a proper URL and a think you’ll be a lot better off.

    Thread Starter websista

    (@websista)

    I found the solution – a path correction. I was having difficulty figuring out whether the problem had to do with AJAX, JavaScript, WordPress or the htaccess.

    We created a simple test file and put it in the root directory and changed the code to reflect that like this: xmlhttp.open(“GET”,”/getsubcat1.php?q=”+myarr[1],true);
    AND IT WORKED!

    We still weren’t sure if WordPress had been interfering so we started to move the file down the directory tree towards the plugin directory (where it was originally) and it worked in all of those too. First we moved it into wp-content. Then into plugins. Finally back into the plugin folder.

    So the path one needs to use with the javascript open function in WordPress is to start with the wp-content folder – something like this xmlhttp.open(“GET”,”/wp-content/plugins/mannanetwork/getsubcat1.php?q=”+myarr[1],true);

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Javascript in WordPress’ is closed to new replies.