Drupal
I know badly. Tell me, please, how can I get data from the database in javascript
? The script is connected in the .info
file of the theme, and the data that goes out should be displayed on the page as javascript
code. How to do it?
- I basically understand that it is best to write a module, etc. but now it's too late to fix anything, so any method will do - heleg
- The question is not specific and not clear. Try to reformulate it, pointing out that it is you who cannot do it and what you need to get in the end. Plus the information that you know badly drupal is superfluous. - Anton Mukhin
- OK. data from the database can be transferred to the js application as follows: <? php // get data from the database and write them to the variable $ data = get_db_data (); $ data = json_encode ($ data); ?> <head> <script> // here we receive the data in javascript var data = eval ('(' + <? php echo $ data?> + ')'); </ script> <script src = "myscript.js"> </ script> </ head> how to do all this on drupal, provided that myscript.js is transferred to the template via the .info theme file? - heleg
- So the variable "var data" will be available in myscript.js! Moreover, it will be filled with what your php script will add to it. And here it’s not at all in your CMS. - Anton Mukhin
- exactly what will be. but how can you get into the header of the drupal template (after all, myscript.js connects there), and even extract the data from the database? I would nafigachil request to the database directly in the template, but it does not allow the execution of the db_query function there. The task in a nutshell is to transfer the data to the script connected in the header - heleg
|
1 answer
<a href="#" class="klik">Klik</a> <?php $query = db_select('node'); $query->addExpression('COUNT(*)'); $count = $query->execute()->fetchField(); print '<span id="message">Всего записей'.$count.' штук ;-) </span>'; ?> <script type="text/javascript"> jQuery(document).ready(function($){ $('.klik').show(); $('#message').hide(); $('.klik').click(function(event) { event.preventDefault(); $('#message').slideToggle(); }); }); </script>
|