faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 13 people (77%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

how to pass an integer value from PHP to JavaScript ???

Apr 6th, 2008 19:44
ha mo, Dave Clark, Jean-Bernard Valentaten, Colin Fraser, peter lukáč,


Colin Fraser wrote:

> I suspect the same rules apply for variables in PHP as they are for
> Javascript, so I am going to take a punt here, and suggest that it is
> actually simple, declare your variable globally then use the global
> variable anywhere you need it. 

> The only problem you may have is if the value in the variable is not 
> an integer, so check the value in the global is an integer before 
you 
> try to use it. 

> Good luck

Again we're seeing the conflict between serverside and clientside 
languages, but this time a solution is easy to find.
Here's an example of how to do it, I'm sure this won't need any 
further 
explanation *g*:

<script language="JavaScript">
<?
  $phpVar = 25;
  echo("var myVar = ".$phpVar.";");
?>
</script>

Jean

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

     ...and, just for complete information, it can also be done this 
way:

<?
  $phpVar = 25;
?>
<script type="text/javascript">
<!--//
var myVar = <%=$phpVar%>;   // for numeric values
var myVar = "<%=$phpVar%>"; // for string values
//-->
</script>

Take care,

Dave Clark
www.DaveClarkConsulting.com

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com