title: How to load markers from MySQL using PHP and ajax?
body: <p>I am new to Leaflet and I was wondering how to load markers from a MySQL database onto a leaflet map. </p>
  
  <p>I don't want to only load the markers when the map is initialised but rather with a function because I would like the user to be able to, for example, select between dates and then load markers from those dates. I have done the following so far but I get an error:</p>
  
  <pre><code>leaflet.js:6 Uncaught TypeError: Cannot read property 'lat' of undefined.
  </code></pre>
  
  <p>I am not sure what I am doing wrong.</p>
  
  <p><strong>jQuery</strong></p>
  
  <pre><code>$( document ).ready(function() {
      var markerpositions;
      var map = L.map('map').setView([-55.7770641,55.6602758], 13);
      L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
          maxZoom: 19,
          attribution: '&amp;copy; &lt;a href="http://www.openstreetmap.org/copyright"&gt;OpenStreetMap&lt;/a&gt;'
      }).addTo(map);
      getlocations();
      var marker = L.marker(markerpositions).addTo(map);
  
  
  function getlocations(){
  var id = "1";
  
      $.ajax({
          type: "POST",
          url: "getlocationstomap.php",
          data: {id:id},
          dataType: 'json',
          cache: false,
      })
      .success(function(response) {
          if(!response.errors &amp;&amp; response.result) {
  
              $.each(response.result, function( index, value) {
                  markerpositions = '['+value[0]+','+value[1]+']';
             });
  
  
          } else {
              $.each(response.errors, function( index, value) {
                  $('input[name*='+index+']').addClass('error').after('&lt;div class="errormessage"&gt;'+value+'&lt;/div&gt;')
              });
          }
      });
  }
  })
  </code></pre>
  
  <p><strong>PHP</strong> (getlocationstomap.php)</p>
  
  <pre><code>&lt;?php
  // assign your post value
  $inputvalues = $_POST;
  
  // assign result vars
  $errors = false;
  $result = false;
  
  $mysqli = new mysqli('localhost', "root", "", "traxi");
  
   /* check connection */
      if (mysqli_connect_errno()) {
          printf("Connect failed: %s\n", mysqli_connect_error());
          exit();
      }
  
      // escape your values
      foreach ($inputvalues as $key =&gt; $value) {
          if(isset($value) &amp;&amp; !empty($value)) {
              $inputvalues[$key] = $mysqli-&gt;real_escape_string( $value );
          } else {
              $errors[$key] = 'The field '.$key.' is empty';
          }
      }
  
  if( !$errors ) {
          // select your query
          $addresult = "
              SELECT `latitude`, `longitude` 
              FROM `positions` 
              WHERE `deviceid` = '" . $inputvalues['id'] . "'
  
           ";
  //$returnResult = array();
           if( $result = $mysqli-&gt;query($addresult) ) {
              // collect results
              while($row = $result-&gt;fetch_all())
              {
                  // assign to new array
                  // make returnResult an array for multiple results
                  $returnResult = $row;
              }
          }
      }
  
      // close connection
      mysqli_close($mysqli);
  
      // print result for ajax request
      echo json_encode(['result' =&gt; $returnResult, 'errors' =&gt; $errors]);
  
      exit;
  ?&gt;
  </code></pre>