simeonC Posted July 11, 2013 Share Posted July 11, 2013 I have inserted some JSON data into mysql database. And would like to retrieve it to display it. However it must first pass through a script.. My question is how do I convert a php variable to a javascript variable.. The contents of the php variable is $signature= [{"lx":123,"ly":37,"mx":123,"my":36},{"lx":120,"ly":33,"mx":123,"my":37},{"lx":119,"ly":33,"mx":120,"my":33},{"lx":113,"ly":33,"mx":119,"my":33},{"lx":102,"ly":33,"mx":113,"my":33},{"lx":92,"ly":33,"mx":102,"my":33},{"lx":81,"ly":32,"mx":92,"my":33},{"lx":71,"ly":31,"mx":81,"my":32},{"lx":67,"ly":30,"mx":71,"my":31},{"lx":63,"ly":29,"mx":67,"my":30},{"lx":62,"ly":27,"mx":63,"my":29},{"lx":62,"ly":25,"mx":62,"my":27},{"lx":65,"ly":20,"mx":62,"my":25},{"lx":78,"ly":13,"mx":65,"my":20},{"lx":98,"ly":4,"mx":78,"my":13},{"lx":114,"ly":0,"mx":98,"my":4},{"lx":167,"ly":4,"mx":114,"my":0},{"lx":172,"ly":9,"mx":167,"my":4},{"lx":175,"ly":15,"mx":172,"my":9},{"lx":177,"ly":23,"mx":175,"my":15},{"lx":179,"ly":34,"mx":177,"my":23},{"lx":179,"ly":40,"mx":179,"my":34},{"lx":179,"ly":46,"mx":179,"my":40},{"lx":179,"ly":49,"mx":179,"my":46},{"lx":177,"ly":54,"mx":179,"my":49},{"lx":176,"ly":54,"mx":177,"my":54},{"lx":175,"ly":55,"mx":176,"my":54},{"lx":173,"ly":55,"mx":175,"my":55},{"lx":171,"ly":55,"mx":173,"my":55},{"lx":169,"ly":55,"mx":171,"my":55},{"lx":169,"ly":53,"mx":169,"my":55},{"lx":169,"ly":52,"mx":169,"my":53},{"lx":170,"ly":51,"mx":169,"my":52},{"lx":172,"ly":50,"mx":170,"my":51},{"lx":173,"ly":49,"mx":172,"my":50},{"lx":175,"ly":48,"mx":173,"my":49},{"lx":176,"ly":47,"mx":175,"my":48},{"lx":178,"ly":46,"mx":176,"my":47},{"lx":175,"ly":45,"mx":178,"my":46},{"lx":167,"ly":45,"mx":175,"my":45},{"lx":158,"ly":45,"mx":167,"my":45},{"lx":147,"ly":45,"mx":158,"my":45},{"lx":136,"ly":45,"mx":147,"my":45},{"lx":125,"ly":45,"mx":136,"my":45},{"lx":118,"ly":45,"mx":125,"my":45},{"lx":111,"ly":45,"mx":118,"my":45},{"lx":107,"ly":45,"mx":111,"my":45},{"lx":104,"ly":45,"mx":107,"my":45},{"lx":103,"ly":45,"mx":104,"my":45},{"lx":108,"ly":43,"mx":103,"my":45},{"lx":123,"ly":40,"mx":108,"my":43},{"lx":139,"ly":38,"mx":123,"my":40},{"lx":156,"ly":37,"mx":139,"my":38},{"lx":168,"ly":35,"mx":156,"my":37},{"lx":176,"ly":35,"mx":168,"my":35},{"lx":180,"ly":35,"mx":176,"my":35},{"lx":181,"ly":35,"mx":180,"my":35},{"lx":44,"ly":17,"mx":44,"my":16},{"lx":46,"ly":16,"mx":44,"my":17},{"lx":47,"ly":16,"mx":46,"my":16},{"lx":56,"ly":16,"mx":47,"my":16},{"lx":65,"ly":16,"mx":56,"my":16},{"lx":78,"ly":16,"mx":65,"my":16},{"lx":91,"ly":17,"mx":78,"my":16},{"lx":101,"ly":18,"mx":91,"my":17},{"lx":109,"ly":19,"mx":101,"my":18},{"lx":117,"ly":21,"mx":109,"my":19},{"lx":121,"ly":21,"mx":117,"my":21},{"lx":126,"ly":21,"mx":121,"my":21},{"lx":128,"ly":21,"mx":126,"my":21},{"lx":129,"ly":19,"mx":128,"my":21},{"lx":130,"ly":19,"mx":129,"my":19},{"lx":131,"ly":18,"mx":130,"my":19},{"lx":139,"ly":18,"mx":131,"my":18},{"lx":151,"ly":19,"mx":139,"my":18},{"lx":165,"ly":23,"mx":151,"my":19},{"lx":178,"ly":26,"mx":165,"my":23},{"lx":188,"ly":28,"mx":178,"my":26},{"lx":197,"ly":29,"mx":188,"my":28},{"lx":205,"ly":29,"mx":197,"my":29},{"lx":210,"ly":29,"mx":205,"my":29},{"lx":214,"ly":29,"mx":210,"my":29},{"lx":217,"ly":26,"mx":214,"my":29},{"lx":219,"ly":24,"mx":217,"my":26},{"lx":219,"ly":22,"mx":219,"my":24},{"lx":219,"ly":18,"mx":219,"my":22},{"lx":219,"ly":15,"mx":219,"my":18},{"lx":219,"ly":13,"mx":219,"my":15},{"lx":219,"ly":11,"mx":219,"my":13},{"lx":218,"ly":12,"mx":219,"my":11},{"lx":218,"ly":14,"mx":218,"my":12},{"lx":218,"ly":17,"mx":218,"my":14},{"lx":218,"ly":21,"mx":218,"my":17},{"lx":218,"ly":26,"mx":218,"my":21},{"lx":218,"ly":30,"mx":218,"my":26},{"lx":217,"ly":33,"mx":218,"my":30},{"lx":216,"ly":36,"mx":217,"my":33},{"lx":216,"ly":37,"mx":216,"my":36},{"lx":215,"ly":37,"mx":216,"my":37},{"lx":215,"ly":36,"mx":215,"my":37},{"lx":213,"ly":33,"mx":215,"my":36},{"lx":212,"ly":30,"mx":213,"my":33},{"lx":208,"ly":28,"mx":212,"my":30},{"lx":200,"ly":27,"mx":208,"my":28},{"lx":192,"ly":27,"mx":200,"my":27},{"lx":184,"ly":27,"mx":192,"my":27},{"lx":174,"ly":29,"mx":184,"my":27},{"lx":164,"ly":32,"mx":174,"my":29},{"lx":152,"ly":35,"mx":164,"my":32},{"lx":142,"ly":38,"mx":152,"my":35},{"lx":134,"ly":41,"mx":142,"my":38},{"lx":129,"ly":42,"mx":134,"my":41},{"lx":124,"ly":43,"mx":129,"my":42},{"lx":120,"ly":44,"mx":124,"my":43},{"lx":118,"ly":44,"mx":120,"my":44},{"lx":116,"ly":44,"mx":118,"my":44},{"lx":116,"ly":45,"mx":116,"my":44},{"lx":116,"ly":50,"mx":116,"my":45},{"lx":116,"ly":56,"mx":116,"my":50},{"lx":116,"ly":60,"mx":116,"my":56},{"lx":114,"ly":63,"mx":116,"my":60},{"lx":111,"ly":65,"mx":114,"my":63},{"lx":106,"ly":66,"mx":111,"my":65},{"lx":103,"ly":66,"mx":106,"my":66},{"lx":98,"ly":66,"mx":103,"my":66},{"lx":95,"ly":66,"mx":98,"my":66},{"lx":91,"ly":64,"mx":95,"my":66},{"lx":86,"ly":62,"mx":91,"my":64},{"lx":83,"ly":60,"mx":86,"my":62},{"lx":80,"ly":58,"mx":83,"my":60},{"lx":78,"ly":56,"mx":80,"my":58},{"lx":76,"ly":55,"mx":78,"my":56},{"lx":75,"ly":53,"mx":76,"my":55},{"lx":75,"ly":51,"mx":75,"my":53},{"lx":75,"ly":50,"mx":75,"my":51},{"lx":75,"ly":49,"mx":75,"my":50},{"lx":75,"ly":47,"mx":75,"my":49},{"lx":77,"ly":47,"mx":75,"my":47},{"lx":79,"ly":47,"mx":77,"my":47},{"lx":81,"ly":47,"mx":79,"my":47},{"lx":82,"ly":47,"mx":81,"my":47},{"lx":84,"ly":47,"mx":82,"my":47},{"lx":84,"ly":48,"mx":84,"my":47},{"lx":85,"ly":48,"mx":84,"my":48},{"lx":86,"ly":49,"mx":85,"my":48}] This is how the data will comeout of the database now how do I get it back into javascript Quote Link to comment Share on other sites More sharing options...
Solution trq Posted July 11, 2013 Solution Share Posted July 11, 2013 echo it. var = '<?php echo $signature; ?>'; Quote Link to comment Share on other sites More sharing options...
Irate Posted July 11, 2013 Share Posted July 11, 2013 Did you try using <?php echo json_encode($signature) ?>? Quote Link to comment Share on other sites More sharing options...
jcbones Posted July 11, 2013 Share Posted July 11, 2013 No need to encode it, it is already JSON. Quote Link to comment Share on other sites More sharing options...
web_craftsman Posted July 12, 2013 Share Posted July 12, 2013 <script type="text/javascript"> var signature = JSON.parse('<?php print $signature; ?>'); </script> Quote Link to comment Share on other sites More sharing options...
.josh Posted July 12, 2013 Share Posted July 12, 2013 The way I understand that OP, $signature is actually the js var (it's part of the js he wants to output). So basically just echo out the php variable you store that whole thing in (make sure it's within script tags of course). Example (truncated for brevity): <?php $variableFromDatabase = '$signature= [{"lx":123,"ly":37,"mx":123,"my":36}]'; echo "<script type='text/javascript'>$variableFromDatabase</script>"; ?> Quote Link to comment Share on other sites More sharing options...
simeonC Posted July 12, 2013 Author Share Posted July 12, 2013 Thank you all.. All answers are correct. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.