Support » Fixing WordPress » Using javascript in WordPress

  • Hi,

    A while back I migrated a whole bunch of HTML pages into pages on a WordPress blog. I eventually solved all the problems (often thanks to people here), but I find I still have one javascript problem.

    At then end of every page I had a form which consisted of a simple opportunity to feedback by selecting one of three radio buttons. A javascript file responded when a button was pressed, called a php file and posted the page name and the feedback (2, 1 or 0 for good, indifferent or bad) to my MYSQL database.

    The structure of the javascript file was simple:

    First, a response thanking the visitor for giving feedback. Second a Try-Catch error trapping routine testing for new XMLHttpRequest(), Third, calling the php file to post to the database.

    However running the same javascript file in WordPress gives me the initial response, but nothing more. I am a total javascript novice, but I presume the second section is no longer working. It is of the form:

     var request = false;
       try {
         request = new XMLHttpRequest();
       }  catch (failed) {
             request = false;
       }
    
       if (!request) {
         return;
    	}

    (The original file also tested for new ActiveXObject but I think that is no longer required.)

    Somehow, despite being a javascript dummy, I managed to get the original file working. Can anyone give me a clue, please, on what is required to make the same type of script work in WordPress?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Seems like you want to call an ajax request to your PHP file which does some operations in database and then return you response back, you need to see this resource https://codex.wordpress.org/AJAX_in_Plugins which helps you achieve same.

    Thanks for replying. I’m trying to avoid totally re-writing this because I’m not very competent. So I first want to see if I can revise what I have. Do you know if there is a reason why what I have won’t work in WordPress?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    It might be easier for us to debug this by looking at the form on the page. Is it possible to link us to that page?

    OK thanks. Here is one of many pages with the same form at the end: http://www.is-there-a-god.info/clues/designfacts/

    The javascript file, which is located in my theme’s footer, used to have this code:

    <script type="text/javascript">
    function hresponse(helpfulResponse) {
    	document.getElementById("feedback").innerHTML = "";
    	document.getElementById("formfeed").innerHTML = "Thank you for your feedback.";
       var request = false;
       try {
         request = new XMLHttpRequest();
       } catch (trymicrosoft) {
         try {
           request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (othermicrosoft) {
           try {
             request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (failed) {
             request = false;
           }  
         }
       }
       if (!request) {
         return;
    	}
       var url = "URL of php file";
       request.open("POST", url, true);
    	var page = document.title;
    	var helpful = 'Result='+helpfulResponse +'&Page='+page;
       request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	request.send(helpful);
    }
    </script>

    But I have since removed the two try .... ActiveXObject lines.

    I’m wondering, hoping that maybe someone has an answer to this question please? Thanks.

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