1

I have all my javascript/jquery code in a file and include it into my page. Without having apache process js files as php what would be the best way to pass data from php to my js script? Right now I have a hidden div on my page that I echo out a value then using jquery I get the text content and then split it to get the variables. Is this a good way of doing it? Or is there a better way?

UPDATE

As mentioned prior, all my js code is in a .js file that I include into the webpage. The only way I could use php in this scenario is tell apache to treat .js files as php so it would recongize php code inside the .js file. I dont want to do this.

HTML

<div id="pagedata" style="display:none;">data1<>data2</div>

JS

var pagedata = $('#pagedata').text();
var break_data = pagedata.split('<>');
John
  • 9,840
  • 26
  • 91
  • 137
  • hello again, json is always a good option. PHP has a function to encode to json, then from javascript you can decode it. If it's just a variable you can always echo it. Like `echo 'var data1 = '.$somephpvariable` – Matt Oct 15 '11 at 06:33
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Blue Jan 29 '18 at 14:02

6 Answers6

2

Why don't you just have php render javascript directly (as JSON), then you have it available to jQuery natively.

<script>

  var data =  {my: 'data', goes: 'here', <? echo render_more_of_my_data(); ?> };

</script>

Edit:

if you don't want to have inline js in your php page, and don't want apache to process js as php, then maybe you can src a php file which contains js.

<script src="my_php_file.php">

and inside that php file have js

var data = <? echo render_my_data(); ?>;

OR

just jQuery to call a php webservice using ajax jQuery.get("php_url.php"), and again in that php script you render some json.

Homan
  • 25,618
  • 22
  • 70
  • 107
0

Instead of getting data from the dive you can directly populate the javascript as:

var pagedata = '<?php echo $somevariable; ?>';
var break_data = pagedata.split('<>');
Shahid Karimi
  • 4,096
  • 17
  • 62
  • 104
  • This wouldnt work if the js code is in a file that is included in the page unless I tell apache to process js files as php, which I dont want. – John Oct 15 '11 at 06:37
  • 1
    Then the best approach is to write a script which writes the js file on the disc each time. – Shahid Karimi Oct 15 '11 at 06:43
0

Instead of mis-using HTML for passing data (which has to be parsed) you should actually echo JavaScript code. For example, PHP could generate:

<script type="text/javascript">
  var data1 = "data1";
  var data1 = "data2";
</script>

As this is declared in a global scope of your page your script can actually access these variables.

The only thing you have to look out for are race conditions. Your script will have to execute after the data has been defined. However, with a standard jquery on-dom-ready callback that is not a problem.

Kissaki
  • 8,810
  • 5
  • 40
  • 42
  • Again I cant use php code in an external js file. I would have to have apache process js files as apache for this to work. If I run your code above it will error out. I dont want to add js inline to my page, I want to keep it as a file that is included into my web page. – John Oct 15 '11 at 06:40
  • I was not talking about echoing it to the external js file but to your HTML page. Hence the explanation about it being global scope. – Kissaki Oct 15 '11 at 06:42
  • I didnt want inline js code in my page. But will look into this option. – John Oct 15 '11 at 06:44
0

Best practice is likely as Matt mentions in the comment above: use JSON. If you're going to use jQuery, you can have a PHP script you call using either $.get() or $.getJSON(). You can find the manual for PHP JSON here: http://php.net/manual/en/book.json.php, and the API reference for the jQuery method $.getJSON() here: http://api.jquery.com/jQuery.getJSON/

HTH.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
0

HTML

<div id="pagedata" style="display:none;">
<? 
$master['data1'] = 'single';
$master['data2'] = array('2a', '2b');
echo json_encode($master); ?>
</div>

json_encode should be already in PHP 5.2. Otherwise look here http://www.json.org/

JS

var master = JSON.parse( $('pagedata').html() );
// master.data1; // single
// master.data2[0]; // 2a
// master.data2[1]; // 2b

To use JSON.parse you'll need json2.js from here https://github.com/douglascrockford/JSON-js.

Moe Sweet
  • 3,683
  • 2
  • 34
  • 46
0

It's a good idea to have JS variables and objects with data, but another approach is to manage them through internal jQuery storage $.data()

The advantages are obvious for non-trivial UI:

  • you can use DOM object as a namespace
  • encapsulation of internal data
  • logic and objects are "isolated"

JS

var pagedata = jQuery.data( document.body, '<? echo json_encode($var); ?>');
var break_data = JSON.parge(pagedata)
Anatoly
  • 15,298
  • 5
  • 53
  • 77