Solved

embed javascript in php

Posted on 2008-06-19
6
639 Views
Last Modified: 2008-06-24
I need to echo the following lines of code in javscript which will in turn document.write the html . I am not sure about the string escape characters so will need your help on this. Any help on this will be appreciated. Thanks and have a great day
<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>
 
this is what I have started with
header("Content-type: text/javascript");
echo "document.write('<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" />);";

Open in new window

0
Comment
Question by:sulentho
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:ayus80
ID: 21827813
try this method
<script>
<?
if($hed_ban['banner_image']){
echo "document.write('<a href=\"http://www.insidetoday.com/ban_click.php?id=".$hed_ban['banner_id']."&url=".$hed_ban['link_url'].\"" target=\"_blank\"><img src=\"http://www.insidetoday.com/bannerimages/".$hed_ban['banner_image'].\"" alt=\"ad\" width=\"468\" height=\"60\" border=\"0\" /></a>');";
}
?>
</script>

Open in new window

0
 

Author Comment

by:sulentho
ID: 21827834
I tried the code you sent me and I get these errors

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 106

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /mnt/target02/343061/353817/www.insidetoday.com/web/content/trulia_header.php on line 106

line 106 is the echo line. Please let me know if I am doing something wrong.
0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 250 total points
ID: 21827847
Are you outputting a section of Javascript code within a web page? If yes, try simply:
<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>

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:sulentho
ID: 21827972
Thank you LordofPorts, i will try the code and will award the points as soon as I check it out. Thanks and have a great day.
0
 

Author Comment

by:sulentho
ID: 21856794
Hi LordofPorts, I know I have chosen you answer as the accepted solution but I will need a little more help on this one, which I hope you can help me with.

I am attaching the entire code for the file, all that needs to happen is the entire php code needs to execute on my server and output the results as javascript in a html file and this file I can provide to the third party to run on their server. I hope this makes sense

<?php
 
$db_server = "localhost";
$db_user = "root";
$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>
 
 
<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 19

Expert Comment

by:LordOfPorts
ID: 21891516
sulentho, I am sorry for the delay! I was about to begin working on the issue this weekend but noticed that you opened a new question and received a solution. Is everything working properly now? Please let me know if I may be of assistance.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
000webhost.com default error log 1 28
JavaScript let vs var 5 26
Call a function within the ASP code 4 12
how can i get the string decoded ? 2 14
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

778 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