WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [closed] Basic Ajax help (3 posts)

  1. AJ Mallory
    Member
    Posted 1 year ago #

    I'm struggling with using ajax calls in wordpress. I'm pretty sure I'm not fully understanding how it works (I don't know javascript all that well) and I'm hoping some one can give me a couple of pointers. I know my ajax calls were working prior to migrating the code into WordPress so it's really only the wp ajax that is in question. I've tried to trim my code down as much as I could leaving only what's relevant.

    below is the php code from my main plugin file which is setting up the ajax stuff:

    add_action('wp_ajax_ta_site_fill', 'ta_ajax_site_fill');
    add_action('wp_ajax_nopriv_ta_site_fill', 'ta_ajax_site_fill');
    
    function ta_ajax_site_fill(){
    	// qurey DB and build xml data
    	...snip...
    	// Start XML file, create parent node
    	$dom = new DOMDocument("1.0");
    	$node = $dom->createElement("marker");
    	$parnode = $dom->appendChild($node);
    	...snip...
    	echo $dom->saveXML();
    	die();
    }
    
    function ta_map_load_scripts(){
    	$ta_options = get_option('ta_map_options');
    	wp_register_script('ta_gmap_ref', 'http://maps.googleapis.com/maps/api/js?key=' . $ta_options["api_key"] . '&sensor=false',array(),NULL);
    	//wp_register_script('ta_gmap_local_js',plugins_url('ta_site_map.js',__FILE__),array(),NULL);
    	if( !is_admin() ){
    		wp_enqueue_script('ta_gmap_ref',array(),NULL);
    		wp_enqueue_script('ta_gmap_local_js',plugins_url('ta_site_map.js',__FILE__),array(),NULL);
    		wp_localize_script('ta_gmap_local_js','wp_ta_map_vars',array(
    			'local_path'=>plugins_url(__file__),
    			'ajaxurl'=> admin_url('admin-ajax.php')
    			)
    		);
    	}
    }
    
    add_action('wp_enqueue_scripts','ta_map_load_scripts');

    Here's my js I'm using google maps javascript api. And I think this is were I'm having my issue downloadUrl( wp_ta_map_vars.ajaxurl + "&ta_ajax_site_fill", see below for the context if necessary.

    //<![CDATA[
        function gmap_load() {
          var map = new google.maps.Map(document.getElementById("map"), {
            center: --snip--
          });
          var infoWindow = new google.maps.InfoWindow;
          downloadUrl( wp_ta_map_vars.ajaxurl + "&ta_ajax_site_fill", function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < element1.length; i++) {
              var ---snip---;
              var baloonhtml = "<b> <a target=\"_blank\" href="+wp_wwta_map_vars.local_path+"/site_detail_template.php?siteID=" + siteID + ">" + name + "</a> </b> <br/>" + steloc;
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                title: name
              });
              bindInfoWindow(marker, map, infoWindow, baloonhtml);
            }
          });
        }
    
        function bindInfoWindow(marker, map, infoWindow, html) {
          google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
          });
        }
        function downloadUrl(url, callback) {
          var request = window.ActiveXObject ?
              new ActiveXObject('Microsoft.XMLHTTP') :
              new XMLHttpRequest;
          request.onreadystatechange = function() {
            if (request.readyState == 4) {
              request.onreadystatechange = doNothing;
              callback(request, request.status);
            }
          };
          request.open('GET', url, true);
          request.send(null);
        }
        function doNothing() {}
        //]]>

    Thank you taking the time to look at my issue,
    Jason

  2. AJ Mallory
    Member
    Posted 1 year ago #

    So I discovered I had a couple of typos in the line referenced above. Here is the corrected version: downloadUrl( wp_ta_map_vars.ajaxurl + "?ta_site_fill",...

    Now if I manually call the ajax from the browser I'm getting back a "-1" so the call is failing. I'm not sure why? Thoughts?

    Thanks,
    Jason

  3. AJ Mallory
    Member
    Posted 1 year ago #

    Noticed this post was created in the wrong sub form. I've re-posted to hacks and closing this one.

Topic Closed

This topic has been closed to new replies.

About this Topic