• I have successfully passed my php variables to my javascript code, but it’s only grabbing the last variable. I’m assuming it’s my loop that is incorrect but I’ve been struggling with this for over a week now so any help would be greatly appreciated. What I’m trying to accomplish is that when the name of the custom field is clicked, that specific location will drop on the map and replace the old marker with the new. I’m still a newbie with all of this.

    Here is my PHP

    function add_scripts() {
       wp_register_script( 'marker', get_stylesheet_directory_uri() . '/js/marker.js', array('jquery') );
       $posts = get_posts('locations');
       $longs = array();
       $lats = array();
       foreach($posts as $post){
          array_push($lats, rwmb_meta('latitude') );
          array_push($longs, rwmb_meta('longitude') );
       }
       $passedValues = array( 
          'latitude' => $lats, 
          'longitude' => $longs,
       );
       wp_localize_script( 'marker', 'passed_object', $passedValues );
       wp_enqueue_script('marker', get_template_directory_uri() . '/js/marker.js', array('jquery') );
    }
    add_action( 'wp_enqueue_scripts', 'add_scripts' );

    and here is my Java

    function dropMarker(elem) { //function that will add markers on button click
       var latitude = passed_object.latitude[$( elem ).attr('id')];
       var longitude = passed_object.longitude[$( elem ).attr('id')];
       var marker = new google.maps.Marker({
          position: new google.maps.LatLng(latitude, longitude),
          map: map,
          animation: google.maps.Animation.DROP,
       });
       if (oldMarker != undefined) {
          oldMarker.setMap(null);
       }
       oldMarker = marker;
          map.setCenter(marker.getPosition());
       }

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • when the name of the custom field is clicked, that specific location will drop on the map

    I think there’s a disconnect with the name and the value.
    Your PHP is outputting an array of arrays with what? numbers? name and number? or does rwmb_meta return something else?
    And then your JS is looking up $( elem ).attr('id') to use as the subscript. This is what you are saying is the “name of the custom field is clicked”, right?

    You have a loop foreach($posts as $post){ but you don’t use $posts or $post, and they are not global.

    Thread Starter chagene92

    (@chagene92)

    I used metaboxe.io to create a custom post type and the rwmb_meta is how to get the field value of that metabox. They are numbers, but I set the metabox up as a text, would that cause an issue?

    I’m sorry, I guess I should have said when the name of the custom post is clicked, the custom field’s value will set the marker’s new coordinates.

    The important part is the last line of my answer: you are looping, but not telling the other function which one to process.

    Dion

    (@diondesigns)

    How does your rwmb_meta() function know which post it should use to grab the meta data? I suspect you need to pass $post->ID to the function, and use that ID within the function to obtain the correct meta data.

    Thread Starter chagene92

    (@chagene92)

    I am kind of starting to understand what you’re saying Joy. So I need to tell my javascript function which one to process? I guess I assumed it would just use the active one but that also explains my lack of knowledge for php.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘PHP Loop only returns last variable’ is closed to new replies.