• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

what is best way to pass php variable to jquery besides setting valud to id or attribute

Sorry for what seems to be such a basic question but this small little detail is alluding me. What is the best way to pass a php variable to jquery without setting the value within the DOM structure? Is there another way besides setting a variable so that jquery can use the variable?

An example would be the value of a mysql database count i.e. 5 animals, 2 vegetables, and 1 rock.  <div id="animalCount"><?php echo $theAnimalCount;?></div>

Thanks!
0
kent3800
Asked:
kent3800
2 Solutions
 
amit_gCommented:
<script type="text/javascript">

$(document).ready(function() {
    var AnimalCount = <?php echo $theAnimalCount;?>;
    //Use AnimalCount
});
0
 
hieloCommented:
You can use cookies, but if the user's browser has disabled cookies, your application will not work.
Another alternative would be to use hidden fields:
  <div id="animalCount"><input type='hidden' name='animals' id='animals' value='<?php echo $theAnimalCount;?' /></div>

you can then retrieve the value with:
$("#animals").val();

Lastly, you can initialize some javascript variable:
<script>
var animals=<?php  echo $theAnimalCount;?>;
</script>
0
 
kent3800Author Commented:
Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now