We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,039 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Pass PHP string to JS

Dear Sirs!

I would like to pass the $row[2] from my PHP code below

<?php
// MySQL
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

// Table
$result = mysql_query("SELECT * FROM  `fillingstations` LIMIT 0 , 30")
or die(mysql_error());  


// Print

while($row = mysql_fetch_array($result)){
echo 'Station name: '. $row[1];
echo 'LatLng: '. $row[2];
echo 'Price: '. $row[3];
}

?>  

to the place of "!!!!Lation!!!!" in my JS code

var beaches = [
  ['Miskolci road / Shell Station', !!!!Lation!!!! 4],
  ['Szabadsag utja / Töltöallomas', 47.559025, 19.281415, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

Can you help me doing that?

Thanks!

Tibor

6
Contributors
10
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
11
Views
Question
Answered
tibormarias
Light Poster
35 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
var beaches = [
  ['Miskolci road / Shell Station', <?php echo $row[2]; ?> , 4],
urtrivedi
Posting Virtuoso
1,714 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

@tibormarias

Since you echo 'LatLng: '. $row[2];

Is this JS code comes before or after or is it on the same page? Never mind I think urtrivedi answer your question.

LastMitch
Industrious Poster
4,165 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

The PHP code is after. Thank untrivedi I'll try it.

tibormarias
Light Poster
35 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

your <script></scipt> tag where your var beaches recides should be inside the while loop of your php and use what urtrivedi said

code739
Posting Whiz in Training
208 posts since May 2012
Reputation Points: 17
Solved Threads: 28
Skill Endorsements: 5

Do you mean by using PHP echo? echo "<script type="text....

This is my whole JS code:

    function initialize() { 
      var myOptions = {
        zoom: 7,
        center: new google.maps.LatLng(46.985749, 19.293220),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      var map = new google.maps.Map(document.getElementById("map_canvas"),
                                    myOptions);

      setMarkers(map, beaches);
    }
    var beaches = [
      ['Miskolci road / Shell Station', <?php echo $row[2];?>, 4],
      ['Szabadsag utja / Töltöallomas', 47.559025, 19.281415, 5],
      ['Cronulla Beach', -34.028249, 151.157507, 3],
      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
      ['Maroubra Beach', -33.950198, 151.259302, 1]
    ];

    function setMarkers(map, locations) {
      var image = new google.maps.MarkerImage('fillingstation.png',
          new google.maps.Size(20, 32),
          new google.maps.Point(0,0),
          new google.maps.Point(0, 32));
      var shadow = new google.maps.MarkerImage('fillingstation.png',
          new google.maps.Size(37, 32),
          new google.maps.Point(0,0),
          new google.maps.Point(0, 32));
      var shape = {
          coord: [1, 1, 1, 20, 18, 20, 18 , 1],
          type: 'poly'
      };
      for (var i = 0; i < locations.length; i++) {
        var beach = locations[i];
        var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            shadow: shadow,
            icon: image,
            shape: shape,
            title: beach[0],
            zIndex: beach[3]
                });
      }

    }
tibormarias
Light Poster
35 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You could get all the data from the DB into a php array and then use json_encode to provide the js variable.

$array = array(array('Szabadsag utja / Töltöallomas',47.559025, 19.281415, 5), array('Cronulla Beach',-34.028249, 151.157507,3));

$js = json_encode($array);

...

<script>
var beaches = <?php echo $js;?>;

</script>

Not tested, but something like that.

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

I 've tried many solutions, and almost all the solutions ended up with no result.. Thank you very much for your idea, but it didn't work neither. I wouldn't have thought this question to be so hard to solve. But I wil play around with json_encode that's not a bad idea.

tibormarias
Light Poster
35 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
<?php

$array = array(array('Szabadsag utja / Töltöallomas',47.559025, 19.281415, 5), array('Cronulla Beach',-34.028249, 151.157507,3));
$js = json_encode($array);
?>


<script>
    var beaches = <?php echo $js;?>;
    alert(beaches[0][0]);
</script>

This works fine for me. Don't know why it doesn't for you.

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

Thank you very much, I found a different syntax error on the page, that caused everything... Have a nice day !

tibor.marias
Newbie Poster
5 posts since Nov 2012
Reputation Points: 0
Solved Threads: 1
Skill Endorsements: 0

OK, mark as solved.

diafol
Keep Smiling
Moderator
10,655 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57
Question Answered as of 5 Months Ago by diafol, urtrivedi, LastMitch and 2 others

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0853 seconds using 2.68MB