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(){
end = new Date(end);
diff=(end-start)/86400000
for (var i=0; i<seasons.length; i++){
temp=seasonsїi].split('-');
tempї0]=Date.parse(tempї0])/86400000;
tempї0]=Math.round(tempї0]);
tempї1]=Date.parse(tempї1])/86400000;
tempї1]=Math.round(tempї1]);
getDaysOfSeasonsїi]=tempї0]+'-'+tempї1];
priceCountїi]=0;
}
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){
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++){
for (var j = 0; j < getDaysOfSeasons.length;j++){
var season=getDaysOfSeasonsїj].split('-');
if (i>=seasonї0]&&i<=seasonї1]){
priceCountїj]++;
}
}
}
}
var resultStr='';
var total=0;
for (var i = 0; i < priceCount.length;i++){
if(priceCountїi]!=0){
total+=priceCountїi]*pricesїi];
resultStr+=priceCountїi]+' day(s) at '+pricesїi]+'€ = '+priceCountїi]*pricesїi]+'<br>';
}
}
resultStr+='Total = '+total+'€';
document.getElementById('result').innerHTML=resultStr
}
//-->
</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.