[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


embed javascript in php

Posted on 2008-06-19
Medium Priority
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

Question by:sulentho
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
  • 3
  • 2

Expert Comment

ID: 21827813
try this method
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>');";

Open in new window


Author Comment

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.
LVL 19

Accepted Solution

LordOfPorts earned 1000 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']); 
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

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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.

Author Comment

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

$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'];
} 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];}
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'];
$select_reg_id="select region_id from county where county_id=".$city['county_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')";
$select_county="select county_id from county where region_id=".$change_region;
$que=" and (";
$que.=" or";
$que.=" county_id=".$fet_county['county_id'];
$que.=" or region_id like '%".$change_region."%' or county_id='all' or region_id='all')";
$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";
<table width="976" border="0" align="center" cellpadding="0" cellspacing="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<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>
<table width="976" border="0" align="center" cellpadding="0" cellspacing="0">
<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>
<table width="976" border="0" align="center" cellpadding="4" cellspacing="0">
<td style="padding-bottom:20px;"><div align="right" class="partner_link"><a href="http://www.insidetoday.com/">Back to home page</a></div></td>

Open in new window

LVL 19

Expert Comment

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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

650 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