0

I have an html form element with its value set as an array. I want to get one of these values out of the array via jQuery. How is this done? Here is my code. Maybe it can speak for itself if my explanation wasn't clear. Thank you!

    var lat = 0;
    var long = 0;

    //gets the latitude and longitude when a user clicks on any place on the map

    map.on('click', function(e) {
        lat = e.latlng.lat;
        long = e.latlng.lng;

    //store the lat and long in an array

        var latlong = [lat, long];

    //here I am setting the value of my an element in my form as the array of lat and long

        document.getElement(".<?= $this->getName() ?>").set("value", latlong);
        var getLatlong = jQuery(".<?= $this->getName() ?>").val();
        console.log("retrieved: " + getLatlong);

    //it saves and retrieves properly.

    //THE PROBLEM IS HERE
    //THE PROBLEM IS HERE
      var lat = $(".<?= $this->getName() ?>").val()[0];
      var long = $(".<?= $this->getName() ?>").val()[1];
    //THE PROBLEM IS HERE
    //THE PROBLEM IS HERE


    });

This all runs fine but I do not get the reuslt I want. If i try using any of the lines of code below

    //THE PROBLEM IS HERE
      var lat = $(".<?= $this->getName() ?>").val()[0];    //returns 9
      var long = $(".<?= $this->getName() ?>").val()[1];   //returns 4
      var long = $(".<?= $this->getName() ?>").val()[3];   //returns 2
      var long = $(".<?= $this->getName() ?>").val()[4];   //returns 3
    //THE PROBLEM IS HERE

to get either the latitude or the longitude "ie: 94.2323, 12.9932"

The index [0] returns 9,
the index [1] returns 4,
the index [3] returns 2

as if the 94.2323 is the array instead of the lat and long as the array. I want [0] to return 94.2323, and [1] to return 12.9932.

I tried explaining this and formatting this question the best I could. Thank you.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
maxcodes
  • 544
  • 6
  • 15
  • 1
    You are defining the same variables over and over? Don't do that. Use new variable names instead. JavaScript actually runs all `var` statements first, before the rest of the code (within that scope), regardless where in the code block you define them. – M. Eriksson Nov 06 '16 at 00:25

1 Answers1

1

You can use JSON.stringify() to convert Array to JSON format

document.getElement(".<?= $this->getName() ?>")
.set("value", JSON.stringify(latlong));

JSON.parse() to convert JSON formatted String to Array

var getLatlong = JSON.parse(jQuery(".<?= $this->getName() ?>").val());
console.log(getLatlong[0], getLatlong[1]); // do stuff
guest271314
  • 1
  • 15
  • 104
  • 177
  • ok this works in Javascript. Thanks a ton. Though it doesn't completely solve the issue I am trying to solve. How might I write this in PHP? – maxcodes Nov 06 '16 at 00:56
  • $info->latitude = $_REQUEST["mapquestlocation"][0]; $info->longitude = $_REQUEST["mapquestlocation"][1]; – maxcodes Nov 06 '16 at 00:57
  • What does `php` return for `latitude`, `longitude`? – guest271314 Nov 06 '16 at 01:00
  • Those two lines result in "Unexpected token o in JSON at position". Im trying to hit my save button and put those lats and longs in the database. but I am unable to get lat and long out of the elements value attribute. Im pretty new to php so Idk if I am saying this clear enough. – maxcodes Nov 06 '16 at 01:21
  • How are the values sent to `php`? – guest271314 Nov 06 '16 at 01:25
  • If I understand correctly, via a hidden input. document.getElement(".= $this->getName() ?>").set("value", JSON.stringify(latlong)); – maxcodes Nov 06 '16 at 01:28
  • Have you tried using `json_decode` ?http://stackoverflow.com/questions/5164404/json-decode-to-array – guest271314 Nov 06 '16 at 01:37
  • 1
    $info->latitude = json_decode($_REQUEST["mapquestlocation"])[0]; $info->longitude = json_decode($_REQUEST["mapquestlocation"])[1]; – maxcodes Nov 06 '16 at 01:50