Solved

create javascript file with php

Posted on 2008-06-26
9
331 Views
Last Modified: 2010-07-27
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

0
Comment
Question by:sulentho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 2

Accepted Solution

by:
JurgenvH earned 500 total points
ID: 21878582
if you replace your php-built script with code below you will find a bannerscript.js in the same folder as script above is located after running.
Change $filename to change the name and/or location of the generated script.
fopen command generates a file (parameter 'w' gives write priviliges)
fwrite command writes a line in the generated file
fclose command closes the file after finishing.

Good luck!
<?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);
 
?>

Open in new window

0
 

Author Comment

by:sulentho
ID: 21878649
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

0
 
LVL 2

Expert Comment

by:JurgenvH
ID: 21878747
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
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:sulentho
ID: 21878776
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
0
 
LVL 2

Expert Comment

by:JurgenvH
ID: 21878918
You could first try and change the line
$bf = fopen($filename, 'w');

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

or:
$bf = fopen($filename, 'wr');
0
 

Author Comment

by:sulentho
ID: 21879069
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.
0
 

Author Comment

by:sulentho
ID: 21879967
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>');
}
0
 

Author Comment

by:sulentho
ID: 21879982
Thank you very much for the help but I have figured it out. Thanks and have a great day.
0
 
LVL 2

Expert Comment

by:JurgenvH
ID: 21881424
You're welcome!
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question