0

SOLVED! I used only javascript to the task. Now it reads the text from the php and use the coordinates.

        var texto = HttpReq.responseText;
        var lats,lons;
        var pos = texto.search(' ');
        lats = texto.substring(0,pos-1);
        lons = texto.substring(pos+1);

Thank you all! You are wonderful!


My site has a main page, where after the user selects a city, I wanna show it on a google map. The coordinates of the cities are in a table, so I'm using php in a separate .php file, accessed through AJAX. In the separate .php, I want to call a JS function from the main page to add the point to the map (the JS map object was on the main page, I also tried to put all the JS in a .js file, but the function still isn't called at all (I'm using FireBug to breakpoint it, but the function is never called).

The result from AJAX's GET is this:

Latitude: -21.15361<BR>Longitude: -41.567501<BR><script src="functions.js" type="text/javascript">addPoint(-21.15361,-41.567501);</script>

The Latitude and Longitude appears on screen, but the method addPoint ain't called. Am I missing something stupid here? Thanks in advance.

-- EDIT -- OK, here's what I'm trying to do (that's inside getcoords.php, the file called thru ajax):

$stmt = $dbh->prepare("select id_mun,lat,lon from municipio where id_mun = '".$_GET['mun']."'");
if ($stmt->execute()) {
    echo '<script src="functions.js" type="text/javascript"></script>';
    while ($row = $stmt->fetch(PDO::FETCH_BOTH)) {
        echo "Latitude: $row[1]<BR>";
        echo "Longitude: $row[2]<BR>";
        echo '<script type="text/javascript">';
        echo 'alert("testando");';
        echo "addPoint($row[1],$row[2]);";
        echo "</script>";
    }
}

and the result that goes to the innerHtml from my div (and is not executed):

<script src="functions.js" type="text/javascript"></script>Latitude: -15.779722<BR>Longitude: -47.929722<BR><script type="text/javascript">alert("testando");addPoint(-15.779722,-47.929722);</script>

Thanks again.

2
  • 1
    can you show us your addPoint function? Kinda difficult to see the problem with any real code. Commented Dec 7, 2011 at 21:28
  • There's nothing really special with it. I've put an 'alert' inside it, and it doesn't show. Even the 'alert' in the script that calls this function ain't called either. function addPoint(lat,lon) { var point = new GLatLng(lat,lon); map.addOverlay(new GMarker(point)); } Commented Dec 8, 2011 at 11:39

2 Answers 2

1

Any code inside <script></script> will not be executed if the src attribute is present. You'll need two script blocks instead of one:

<script src="functions.js" type="text/javascript"></script><script type="text/javascript">addPoint(-21.15361,-41.567501);</script>

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but it won't work either. I'm showing more code above.
0

Stuff in <script> is only executed while page is loading. You need to call the code from within your AJAX response handler, not just inject it into the page.

3 Comments

That means I need to use jQuery for that? Or is there any other possibility? And what about the other <script>, why the 'alert' ain't working?
No need to use jQuery, but it would make it easier. If I were you, I would wrap the code in a function, add it to page source and then call it at the end of AJAX response handler.
Milan, there is a tradeoff between doing it in a way that will run faster and run in most computers, versus doing it easier. I usually prefer Delphi (now I'm giving Lazarus a try) and C++ than anything on rails. In Delphi I rather write my own solutions than download "components". I agree with mr. micosyen that simplicity is a rule -- though I'm not down to assembler yet... :) I'll look at jQuery. And that's a good idea. Will try it now. Thank you.

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.