Page 1 of 1

pass variable from php to javascript

Posted: Wed Jul 07, 2004 7:43 am
by ddragas
How to pass variable from php to javascript

<?php
$start_date='01/01/2004';
$end_date='12/31/2004';

$price1=20;
$price2=30;
$price3=40;
$price4=30;
$price5=20;

?>
<script language="JavaScript" type="text/javascript">
<!--
var start=new Date('$start_date');
var end=new Date('$end_date');
var seasons=new Array('01/01/2004-05/28/2004','05/29/2004-07/02/2004','07/03/2004-09/03/2004','09/04/2004-10/01/2004','10/02/2004-12/31/2004');
var prices=new Array($price1,$price2,$price3,$price4,$price5);

etc......

</script>

Posted: Wed Jul 07, 2004 7:48 am
by magicrobotmonkey

Code: Select all

<?php 
$start_date='01/01/2004'; 
$end_date='12/31/2004'; 

$price1=20; 
$price2=30; 
$price3=40; 
$price4=30; 
$price5=20; 

?> 
<script language="JavaScript" type="text/javascript"> 
<!-- 
var start=new Date('<? echo $start_date; ?>'); 
var end=new Date('<? echo $end_date; ?>'); 
var seasons=new Array('01/01/2004-05/28/2004','05/29/2004-07/02/2004','07/03/2004-09/03/2004','09/04/2004-10/01/2004','10/02/2004-12/31/2004'); 
var prices=new Array(<? echo $price1.",".$price2.",".$price3.",".$price4.",".$price5;?>); 

etc...... 

</script>


?>

Posted: Wed Jul 07, 2004 7:57 am
by ddragas
Thank you for help, but if I write it in that way, function doesn't work. Function doesn't calculate nothing

Posted: Wed Jul 07, 2004 8:02 am
by feyd
You never said anything about calculating anything, so how was magicrobotmonkey supposed to know? Nor was the snippet you posted actually a function that implies you actually want to calculate something..

Sidenote: Last I checked, there were only 4 seasons.

Posted: Wed Jul 07, 2004 10:23 am
by ddragas
I'm not saying that magicrobotmonkey is guilty that my function is not working now!!!

If you know how try to help me.

here is code of javascript function

Code: Select all

<!-- saved from url=(0022)http://internet.e-mail -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<title>Untitled</title> 
<?php 

$start_date='01/01/2004'; 
$end_date='12/31/2004'; 

$price1=20; 
$price2=30; 
$price3=40; 
$price4=30; 
$price5=20; 

?> 


<script language="JavaScript" type="text/javascript"> 
<!-- 
var start=new Date('$start_date'); 
var end=new ('$end_date'); 
var seasons=new Array('01/01/2004-05/28/2004','05/29/2004-07/02/2004','07/03/2004-09/03/2004','09/04/2004-10/01/2004','10/02/2004-12/31/2004'); 
var prices=new Array($price1,$price2,$price3,$price4,$price5); 
var getDaysOfSeasons=new Array(); 
var priceCount=new Array(); 
function calculate()&#123; 
end = new Date(end); 
diff=(end-start)/86400000 
for (var i=0; i<seasons.length; i++)&#123; 
temp=seasons&#1111;i].split('-'); 
temp&#1111;0]=Date.parse(temp&#1111;0])/86400000; 
temp&#1111;0]=Math.round(temp&#1111;0]); 
temp&#1111;1]=Date.parse(temp&#1111;1])/86400000; 
temp&#1111;1]=Math.round(temp&#1111;1]); 
getDaysOfSeasons&#1111;i]=temp&#1111;0]+'-'+temp&#1111;1]; 
priceCount&#1111;i]=0; 
&#125; 
var dt1_dt = Date.parse(start); 
var dt2_dt = Date.parse(end); 
diff=(dt2_dt-dt1_dt)/86400000; 
diff=Math.round(diff); 
if (diff>0)&#123; 
var dayArray=new Array(); 
begDay=start.getDate(); 
begMonth=start.getMonth()+1; 
begDay=begMonth+'/'+begDay+'/'+start.getFullYear(); 
begDay=Date.parse(begDay)/86400000; 
begDay=Math.round(begDay); 
endDay=end.getDate(); 
endMonth=end.getMonth()+1; 
endDay=endMonth+'/'+endDay+'/'+end.getFullYear(); 
endDay=Date.parse(endDay)/86400000; 
endDay=Math.round(endDay)-1;  
for (var i = begDay; i<=endDay;i++)&#123; 
for (var j = 0; j < getDaysOfSeasons.length;j++)&#123; 
var season=getDaysOfSeasons&#1111;j].split('-'); 
if (i>=season&#1111;0]&&i<=season&#1111;1])&#123; 
priceCount&#1111;j]++; 
&#125; 
&#125; 
&#125; 
&#125; 
var resultStr=''; 
var total=0; 
for (var i = 0; i < priceCount.length;i++)&#123; 
if(priceCount&#1111;i]!=0)&#123; 
total+=priceCount&#1111;i]*prices&#1111;i]; 
resultStr+=priceCount&#1111;i]+' day(s) at '+prices&#1111;i]+'€ = '+priceCount&#1111;i]*prices&#1111;i]+'<br>'; 
&#125; 
&#125; 
resultStr+='Total = '+total+'€'; 
document.getElementById('result').innerHTML=resultStr 
&#125; 
//--> 
</script> 
</head> 
<body onload="calculate()"> 
<span id="result"></span> 
</body> 
</html>

Posted: Thu Jul 08, 2004 2:22 pm
by ddragas
anybody please?

Posted: Thu Jul 08, 2004 3:29 pm
by pickle
One of your problems is that javascript doesn't know what $start_date, $price1, etc are. You need to enter PHP in order to output those variables - just like ~magicrobotmonky's code did.