Link to home
Start Free TrialLog in
Avatar of sulentho
sulentho

asked on

create javascript file with php

I have a php script on my server that connects to the database and pulls out banners based on location. We have a third party company that handles these banners but they only accept javascript code and not php so I need to make all the php execute on my server and then write the results to a js file that they can retrieve and take it from there. I am not sure on how to do this so any help will be appreciated.

Right now the code displays the javascript on the page but it should write it to a javascript file that the third party can access and use accordingly.
$db_server = "db_server";
$db_user = "db_user";
$db_pass = "db_pass";
$db_name = "db_name"; 
 
mysql_connect($db_server,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
 
if(isset($_POST['change_region'])) {
	$change_region = $_POST['change_region'];
	$change_county =$_POST['change_county'];
	$change_city = $_POST['change_city'];
	setcookie('change_region',$change_region,time()+60*60*24*30);
	setcookie('change_county',$change_county,time()+60*60*24*30);
	setcookie('change_city',$change_city,time()+60*60*24*30);
} else {
$change_region = $_COOKIE['change_region'];
$change_county =$_COOKIE['change_county'];
$change_city = $_COOKIE['change_city'];
}
 
?>
<link href="http://www.insidetoday.com/css/trulia_style.css" rel="stylesheet" type="text/css" />
 
<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
 
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
 
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<?php
if($change_city) {
$city = getCity((int)$change_city);
$location_Str = $city['city_name'].', NJ';
} elseif($change_region) {
$region = getRegion((int)$change_region);
$location_Str = $region['region_name'];
}
if(($change_region!="")&&($change_region!="0")){
if($city['county_id']){
$select_reg_id="select region_id from county where county_id=".$city['county_id'];
$exe_reg_id=mysql_query($select_reg_id);
$row_reg_id=mysql_fetch_array($exe_reg_id);
$que=" and (county_id=".$city['county_id']." or region_id like '%".$row_reg_id['region_id']."%' or county_id='all' or region_id='all')";
}else{
$select_county="select county_id from county where region_id=".$change_region;
$exe_county=mysql_query($select_county);
$que=" and (";
$i=0;
while($fet_county=mysql_fetch_array($exe_county)){
if($i>0){
$que.=" or";
}
$que.=" county_id=".$fet_county['county_id'];
$i++;
}
$que.=" or region_id like '%".$change_region."%' or county_id='all' or region_id='all')";
}
}else{
$que="";
}
$sel_ban= "select banner_id,banner_image,link_url from bannerimage_header where start_date <= DATE_FORMAT( CURRENT_DATE( ) , '%Y-%m-%d' ) AND end_date >= DATE_FORMAT( CURRENT_DATE( ) , '%Y-%m-%d' ) and pagename='realestate' and banner_type ='header'$que order by rand() limit 0,1";
$exe_ban=mysql_query($sel_ban);
$hed_ban=mysql_fetch_array($exe_ban);
?>
<table width="976" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="32%" valign="bottom" style="height:99px; vertical-align:middle;"><div align="left" style="padding:20px 0 20px 28px;"><a href="http://www.insidetoday.com/"><img src="http://insidetoday.com/images/tp_logo.jpg" alt="" name="LOGO" width="282" height="59" border="0" style="background-color: #6666CC"></a></div></td>
<td width="68%" align="right" valign="bottom" class="gray" style="padding:10px 20px 5px 0;">
 
<!--<a href="http://www.insidetoday.com/ban_click.php?id=<?=$hed_ban['banner_id']?>&url=<?=$hed_ban['link_url']?>" target="_blank"><? if($hed_ban['banner_image']){ ?><img src="http://www.insidetoday.com/bannerimages/<?=$hed_ban['banner_image']?>" alt="ad" width="468" height="60" border="0" /><? } ?></a>-->
 
<script type="text/javascript">
 
document.write('<a href="http://www.insidetoday.com/ban_click.php?id=<?php 
 
echo urlencode($hed_ban['banner_id']); 
 
?>&url=<?php
 
echo urlencode($hed_ban['link_url']);
 
?>" target="_blank"><?php
 
if($hed_ban['banner_image']) { 
 
?><img src="http://www.insidetoday.com/bannerimages/<?php 
 
echo $hed_ban['banner_image']; 
 
?>" alt="ad" width="468" height="60" border="0" /><?php
 
}
 
?></a>');
 
</script>
 
<br /><br /></td>
</tr>
</table>
</td>
</tr>
</table>
<table width="976" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="middle" style="background:url(http://www.insidetoday.com/images/menu_bg.jpg) repeat-x;"><div align="center"><a href="http://insidetoday.com/entertainment" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Entertainment','','http://insidetoday.com/images/enter_roll.jpg',1)"><img src="http://insidetoday.com/images/enter.jpg" alt="Entertainment" name="Entertainment" width="131" height="38" border="0" id="Entertainment" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/events" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('events','','http://insidetoday.com/images/events_roll.gif',1)"><img src="http://insidetoday.com/images/events.gif" alt="Events" name="events" width="88" height="38" border="0" id="events" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/restaurants" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('restaurants','','http://insidetoday.com/images/rest.jpg',1)"><img src="http://insidetoday.com/images/rest_roll.jpg" alt="Restaurants" name="restaurants" width="115" height="38" border="0" id="restaurants" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/recreation" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Recreation','','http://insidetoday.com/images/recreation.jpg',1)"><img src="http://insidetoday.com/images/recreation_roll.jpg" alt="Recreation" name="Recreation" width="112" height="38" border="0" id="Recreation" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/shopping" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Shopping','','http://insidetoday.com/images/shopping.jpg',1)"><img src="http://insidetoday.com/images/shopping_roll.jpg" alt="Shopping" name="Shopping" width="105" height="38" border="0" id="Shopping" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/services" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Services','','http://insidetoday.com/images/service.jpg',1)"><img src="http://insidetoday.com/images/service_roll.jpg" alt="Services" name="Services" width="100" height="38" border="0" id="Services" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/classifieds" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Classifieds','','http://insidetoday.com/images/class.jpg',1)"><img src="http://insidetoday.com/images/class_roll.jpg" alt="Classifieds" name="Classifieds" width="110" height="38" border="0" id="Classifieds" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><img src="http://insidetoday.com/images/real.jpg" alt="Real Estate" name="Real Estate" width="115" height="38" border="0" id="Real Estate" /></div></td>
</tr>
</table>
<table width="976" border="0" align="center" cellpadding="4" cellspacing="0">
<tr>
<td style="padding-bottom:20px;"><div align="right" class="partner_link"><a href="http://www.insidetoday.com/">Back to home page</a></div></td>
</tr>
</table>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JurgenvH
JurgenvH
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sulentho
sulentho

ASKER

I am getting the following errors

Warning: fopen(bannerscript.js) [function.fopen]: failed to open stream: Permission denied in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 107

Warning: fwrite(): supplied argument is not a valid stream resource in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 109

Warning: fwrite(): supplied argument is not a valid stream resource in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 111

Warning: fwrite(): supplied argument is not a valid stream resource in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 113

Warning: fwrite(): supplied argument is not a valid stream resource in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 115

Warning: fwrite(): supplied argument is not a valid stream resource in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 119

Warning: fwrite(): supplied argument is not a valid stream resource in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 123

Warning: fclose(): supplied argument is not a valid stream resource in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 125
<?php
$db_server = "db_server";
$db_user = "db_user";
$db_pass = "db_pass";
$db_name = "db_name"; 
 
mysql_connect($db_server,$db_user,$db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
 
if(isset($_POST['change_region'])) {
	$change_region = $_POST['change_region'];
	$change_county =$_POST['change_county'];
	$change_city = $_POST['change_city'];
	setcookie('change_region',$change_region,time()+60*60*24*30);
	setcookie('change_county',$change_county,time()+60*60*24*30);
	setcookie('change_city',$change_city,time()+60*60*24*30);
} else {
$change_region = $_COOKIE['change_region'];
$change_county =$_COOKIE['change_county'];
$change_city = $_COOKIE['change_city'];
}
 
?>
<link href="http://www.insidetoday.com/css/trulia_style.css" rel="stylesheet" type="text/css" />
 
<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
 
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
 
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
<?php
if($change_city) {
$city = getCity((int)$change_city);
$location_Str = $city['city_name'].', NJ';
} elseif($change_region) {
$region = getRegion((int)$change_region);
$location_Str = $region['region_name'];
}
if(($change_region!="")&&($change_region!="0")){
if($city['county_id']){
$select_reg_id="select region_id from county where county_id=".$city['county_id'];
$exe_reg_id=mysql_query($select_reg_id);
$row_reg_id=mysql_fetch_array($exe_reg_id);
$que=" and (county_id=".$city['county_id']." or region_id like '%".$row_reg_id['region_id']."%' or county_id='all' or region_id='all')";
}else{
$select_county="select county_id from county where region_id=".$change_region;
$exe_county=mysql_query($select_county);
$que=" and (";
$i=0;
while($fet_county=mysql_fetch_array($exe_county)){
if($i>0){
$que.=" or";
}
$que.=" county_id=".$fet_county['county_id'];
$i++;
}
$que.=" or region_id like '%".$change_region."%' or county_id='all' or region_id='all')";
}
}else{
$que="";
}
$sel_ban= "select banner_id,banner_image,link_url from bannerimage_header where start_date <= DATE_FORMAT( CURRENT_DATE( ) , '%Y-%m-%d' ) AND end_date >= DATE_FORMAT( CURRENT_DATE( ) , '%Y-%m-%d' ) and pagename='realestate' and banner_type ='header'$que order by rand() limit 0,1";
$exe_ban=mysql_query($sel_ban);
$hed_ban=mysql_fetch_array($exe_ban);
?>
<table width="976" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="32%" valign="bottom" style="height:99px; vertical-align:middle;"><div align="left" style="padding:20px 0 20px 28px;"><a href="http://www.insidetoday.com/"><img src="http://insidetoday.com/images/tp_logo.jpg" alt="" name="LOGO" width="282" height="59" border="0" style="background-color: #6666CC"></a></div></td>
<td width="68%" align="right" valign="bottom" class="gray" style="padding:10px 20px 5px 0;">
 
<!--<a href="http://www.insidetoday.com/ban_click.php?id=<?=$hed_ban['banner_id']?>&url=<?=$hed_ban['link_url']?>" target="_blank"><? if($hed_ban['banner_image']){ ?><img src="http://www.insidetoday.com/bannerimages/<?=$hed_ban['banner_image']?>" alt="ad" width="468" height="60" border="0" /><? } ?></a>-->
 
<?php
 
$filename = "bannerscript.js";
 
$bf = fopen($filename, 'w');
 
fwrite($bf, 'document.write(\'<a href="http://www.insidetoday.com/ban_click.php?id=');
 
fwrite($bf, urlencode($hed_ban['banner_id'])); 
 
fwrite($bf, '&url='.urlencode($hed_ban['link_url']));
 
fwrite($bf, ' target="_blank">');
 
if($hed_ban['banner_image']) { 
 
        fwrite($bf, '<img src="http://www.insidetoday.com/bannerimages/'.$hed_ban['banner_image'].' alt="ad" width="468" height="60" border="0" />');
 
}
 
fwrite($bf, '</a>\');');
 
fclose($bf);
 
?>
 
<br /><br /></td>
</tr>
</table>
</td>
</tr>
</table>
<table width="976" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td valign="middle" style="background:url(http://www.insidetoday.com/images/menu_bg.jpg) repeat-x;"><div align="center"><a href="http://insidetoday.com/entertainment" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Entertainment','','http://insidetoday.com/images/enter_roll.jpg',1)"><img src="http://insidetoday.com/images/enter.jpg" alt="Entertainment" name="Entertainment" width="131" height="38" border="0" id="Entertainment" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/events" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('events','','http://insidetoday.com/images/events_roll.gif',1)"><img src="http://insidetoday.com/images/events.gif" alt="Events" name="events" width="88" height="38" border="0" id="events" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/restaurants" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('restaurants','','http://insidetoday.com/images/rest.jpg',1)"><img src="http://insidetoday.com/images/rest_roll.jpg" alt="Restaurants" name="restaurants" width="115" height="38" border="0" id="restaurants" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/recreation" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Recreation','','http://insidetoday.com/images/recreation.jpg',1)"><img src="http://insidetoday.com/images/recreation_roll.jpg" alt="Recreation" name="Recreation" width="112" height="38" border="0" id="Recreation" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/shopping" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Shopping','','http://insidetoday.com/images/shopping.jpg',1)"><img src="http://insidetoday.com/images/shopping_roll.jpg" alt="Shopping" name="Shopping" width="105" height="38" border="0" id="Shopping" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/services" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Services','','http://insidetoday.com/images/service.jpg',1)"><img src="http://insidetoday.com/images/service_roll.jpg" alt="Services" name="Services" width="100" height="38" border="0" id="Services" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><a href="http://insidetoday.com/classifieds" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Classifieds','','http://insidetoday.com/images/class.jpg',1)"><img src="http://insidetoday.com/images/class_roll.jpg" alt="Classifieds" name="Classifieds" width="110" height="38" border="0" id="Classifieds" /></a><img src="http://insidetoday.com/images/menu_pipe.jpg" alt="|" width="1" height="31" /><img src="http://insidetoday.com/images/real.jpg" alt="Real Estate" name="Real Estate" width="115" height="38" border="0" id="Real Estate" /></div></td>
</tr>
</table>
<table width="976" border="0" align="center" cellpadding="4" cellspacing="0">
<tr>
<td style="padding-bottom:20px;"><div align="right" class="partner_link"><a href="http://www.insidetoday.com/">Back to home page</a></div></td>
</tr>
</table>

Open in new window

Do you have write permissions on the directory where the script runs?
As long as the fopen command is not permitted, the fwrite and fclose will fail also
Yes I do have permissions to create new files, write to them and change the permissions on subfolders and stuff. Do I need to make the file trulia_header.php a 777 or something. Let me know
You could first try and change the line
$bf = fopen($filename, 'w');

into:
$bf = fopen($filename, 'w+');

or:
$bf = fopen($filename, 'wr');
Hi JurgenvH,
I tried both of the lines of code you gave me but I am getting the same error. Let me know if I need to contact my host to figure out the permissions scenario. Thanks for all the help.
The script is working but the only addition I need to make is right now the output on bannerscript,js needs to be enclosed in a javascript function called bannerscript() like this

function bannerscript()
{
document.write('<a href="http://www.insidetoday.com/ban_click.php?id=89&url=www.insidetoday.com target="_blank"><img src="http://www.insidetoday.com/bannerimages/89_trulia_banner.gif alt="ad" width="468" height="60" border="0" /></a>');
}
Thank you very much for the help but I have figured it out. Thanks and have a great day.
You're welcome!