0
if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            $.ajax({
                type: "POST",
                url: "/gethamsters",
                data: {
                    x: position.coords.latitude,
                    y: position.coords.longitude
                },
                success: function (data) {
                    console.log(data);
                    hamsters = data;               
                }
            });
        });
    }
    </script>
    @foreach ($hamsters as $hamster)
      <div class="col-sm-6 col-md-4 col-lg-3">
        <div class="thumbnail">
          <center><div>{{ HTML::image($hamster->foto, $hamster->titel, array( 'width' => 200, 'height' => 200 )) }}</div></center>
          <div class="caption">
            <h3><?php echo substr($hamster->titel, 0, 20); ?></h3>
            <p><?php echo substr($hamster->beschrijving, 0, 50); ?></p>
            <center><p><?php echo '<a href=/hamsterdetails/' . $hamster->id . ' class="btn btn-primary" role="button">Meer
info</a><br /> ';?></p></center>
          </div>
        </div>
       </div>
    @endforeach

As you can see I got data from my jquery result and I want it to pass it into the $hamsters variable in my php code. How can I do that?

1
  • 1
    you can't. php is executed on the server, js in your browser. Commented Nov 11, 2014 at 23:18

1 Answer 1

1

Since we need to clearify what is happening where lets step back back to the basics here:

  1. the Client requests GET /hamsters
  2. the Server creates and sends response.
  3. the Client receives response and parses page and runs any javascript on (or linked by) the document - and can request additional resources with AJAX.
  4. the Client POSTs /gethamsters with x, y coordinates via AJAX.
  5. the Server processes POST /gethamsters request and returns a response to client.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.