Jump to content


Photo

PHP to Javascript


Best Answer trq, 11 July 2013 - 05:14 PM

echo it.

 

var = '<?php echo $signature; ?>';
Go to the full post


  • Please log in to reply
6 replies to this topic

#1 simeonC

simeonC

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 11 July 2013 - 04:59 PM

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



#2 trq

trq

    Advanced Member

  • Administrators
  • 31,025 posts
  • LocationSydney, Australia.

Posted 11 July 2013 - 05:14 PM   Best Answer

echo it.

 

var = '<?php echo $signature; ?>';

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#3 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 11 July 2013 - 05:49 PM

Did you try using <?php echo json_encode($signature) ?>?
Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#4 jcbones

jcbones

    Advanced Member

  • Gurus
  • 2,566 posts
  • LocationNorth Carolina

Posted 11 July 2013 - 06:30 PM

No need to encode it, it is already JSON.



#5 web_craftsman

web_craftsman

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts
  • LocationUkraine

Posted 11 July 2013 - 10:55 PM

<script type="text/javascript">
  
var signature = JSON.parse('<?php print $signature; ?>');

</script>


#6 .josh

.josh

    .josh

  • Staff Alumni
  • 14,830 posts

Posted 11 July 2013 - 11:03 PM

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>";

?>


Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 simeonC

simeonC

    Advanced Member

  • Members
  • PipPipPip
  • 46 posts

Posted 12 July 2013 - 07:40 AM

Thank you all.. All answers are correct.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com