Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-25-2008, 04:32 PM   PM User | #1
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
echo javascript problem

I have a javascript code that worked great by itself, but I try to echo it with php it gives me this parse error:

unexpected T_STRING, expecting ',' or ';' in /home/surreal/public_html/jsheadtag.php on line 8

Here is the string of code in question:

Code:
function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}
Line 8 is the second line of code.

I keep seeing this exact syntax error pop, Is there something I should be aware of to fix this in the future?
surreal5335 is offline   Reply With Quote
Old 07-25-2008, 04:34 PM   PM User | #2
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
can you post that code in context please?
ohgod is offline   Reply With Quote
Old 07-25-2008, 05:37 PM   PM User | #3
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Please show us the PHP code, I can't help you if I don't know specifically what you're trying to echo.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Old 07-25-2008, 07:05 PM   PM User | #4
JohnDubya
Super Moderator


 
JohnDubya's Avatar
 
Join Date: Nov 2006
Location: Missouri
Posts: 634
Thanks: 12
Thanked 18 Times in 18 Posts
JohnDubya is on a distinguished road
surreal, best thing you could do to avoid PHP echo issues with this chunk of Javascript would be to stop PHP parsing when it gets to this script. Something like this:

PHP Code:
<?php
$javascript 
1;

if (
$javascript == 1) {
?>

function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}

<?php
}
?>
__________________
JDub
http://johnnyzone.com/blog
JohnDubya is offline   Reply With Quote
Old 07-25-2008, 07:08 PM   PM User | #5
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Quote:
Originally Posted by JohnDubya View Post
surreal, best thing you could do to avoid PHP echo issues with this chunk of Javascript would be to stop PHP parsing when it gets to this script. Something like this:

PHP Code:
<?php
$javascript 
1;

if (
$javascript == 1) {
?>

function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}

<?php
}
?>
If you're using a template system, though, that may get a bit hairy.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Old 07-26-2008, 04:49 PM   PM User | #6
scoop_987
New Coder

 
Join Date: Jul 2008
Posts: 91
Thanks: 4
Thanked 9 Times in 9 Posts
scoop_987 is an unknown quantity at this point
Since you are echoing it, you MUST escape the " ' "'s otherwise it will fail

Change your PHP part of the JS that you posted to:

PHP Code:
echo('function rollOn(obj){ 
obj.src=obj.src.replace(/.png/,\'blue.png\'); 

function rollOff(obj){ 
obj.src=obj.src.replace(/blue.png/,\'.png\'); 
}'
); 

Last edited by JohnDubya; 07-26-2008 at 04:51 PM.. Reason: Rude remark removed
scoop_987 is offline   Reply With Quote
Old 07-26-2008, 07:27 PM   PM User | #7
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
well I tried the parenthesis, it didnt seem to help

I am still getting an error on the same string but now its not giving me any specifics.

Here is the code I am trying to work in its entirty

Code:
<?php
echo'

<script language="javascript" type="text/javascript">


function rollOn(obj){
obj.src=obj.src.replace(/.png/,'blue.png');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,'.png');
}



var currentSpan = 1;
function showMe(n) {
document.getElementById("ex_"+currentSpan).style.display = "none";
document.getElementById("ex_"+n).style.display = "block";
currentSpan = n;
}

var currentDiv = 1;

function showcontent(n) {

// Hide the current side content that is being shown
document.getElementById("SC_"+currentDiv).style.display = "none";

// Show the new side content
document.getElementById("SC_"+n).style.display = "block";

// Set the current open side content
currentDiv = n;
}


function bkgrnd() {
var bg = [];
var n=0;

var base = "http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE%20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";


var rnd = Math.floor(Math.random() * bg.length) ;

return '<BODY BACKGROUND="' +bg[rnd]+ '"' +' BGCOLOR="#FFFFFF">'
}

</script>';
?>
I am going to be using this code as a seperate file and including it into another php document which is echoing html. I am using php bc of its ability to include external files.

I appreciate the help
surreal5335 is offline   Reply With Quote
Old 07-26-2008, 07:31 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
There is no PHP used within that function, so you can completely remove the php indicators and the echo. If you really want to use it, look into heredoc syntax; it should reduce the complication on you're escapes. I'd just remove the <?php from it.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 07-27-2008, 04:47 PM   PM User | #9
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
Ok I have diagnosed the problem, it lies with with single quote conflicts seeing as the echo is done with single quotes. I made a change to that line and made it all double quotes and that fixed that... there is just one last line I am not sure how to change, it has single and double quotes in it.

Code:
return '<BODY BACKGROUND="' +bg[rnd]+ '"' +' BGCOLOR="#FFFFFF">'
Its found on the bottom of the page of code in my last post.

Any suggestions on how to get the single quotes out?
surreal5335 is offline   Reply With Quote
Old 07-27-2008, 06:11 PM   PM User | #10
scoop_987
New Coder

 
Join Date: Jul 2008
Posts: 91
Thanks: 4
Thanked 9 Times in 9 Posts
scoop_987 is an unknown quantity at this point
PHP Code:
<?php
echo'

<script language="javascript" type="text/javascript">


function rollOn(obj){
obj.src=obj.src.replace(/.png/,\'blue.png\');
}
function rollOff(obj){
obj.src=obj.src.replace(/blue.png/,\'.png\');
}



var currentSpan = 1;
function showMe(n) {
document.getElementById("ex_"+currentSpan).style.display = "none";
document.getElementById("ex_"+n).style.display = "block";
currentSpan = n;
}

var currentDiv = 1;

function showcontent(n) {

// Hide the current side content that is being shown
document.getElementById("SC_"+currentDiv).style.display = "none";

// Show the new side content
document.getElementById("SC_"+n).style.display = "block";

// Set the current open side content
currentDiv = n;
}


function bkgrnd() {
var bg = [];
var n=0;

var base = "http:\/\/i95.photobucket.com/albums/l136/surreal5335/RE%20site/house"

bg[n] = base + "6.png";
bg[n++] = base + "1.png";
bg[n++] = base + "3.png";
bg[n++] = base + "4.png";
bg[n++] = base + "5.png";


var rnd = Math.floor(Math.random() * bg.length) ;

return \'<BODY BACKGROUND="\' +bg[rnd]+ \'"\' +\' BGCOLOR="#FFFFFF">\'
}

</script>'
;
?>
Should be fixed now
scoop_987 is offline   Reply With Quote
Old 07-28-2008, 12:24 AM   PM User | #11
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
I see what you going for, I tried it but it seems that when I try to view the site it makes a change to the code to look like this:

Code:
return&nbsp;\'<BODY&nbsp;BACKGROUND="\'&nbsp;+bg[rnd]+&nbsp;\'"\'&nbsp;+\'&nbsp;BGCOLOR="#FFFFFF">\'
Firebug says its a syntax error

I keep reseting it but it keeps reverting to this messed up everytime.

Any reason why this would be?

Last edited by surreal5335; 07-28-2008 at 12:27 AM.. Reason: fixed another problem
surreal5335 is offline   Reply With Quote
Old 07-28-2008, 01:08 AM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
If firebug says its a syntax error, its now within you're JS code. If you can view it through the source, PHP has done its job.
Code:
return \'<BODY BACKGROUND="\\\' +bg[rnd]+ \\\'" BGCOLOR="#FFFFFF">\'
What you're JS sees is \' + ..., you need to escape you're escapes to make this work with inline echos.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 07-28-2008, 04:03 PM   PM User | #13
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
Thanks a lot that worked great! One question is there a way to control the repeat and positioning of the background image with javascript?
surreal5335 is offline   Reply With Quote
Old 07-28-2008, 08:50 PM   PM User | #14
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, use the style object. Since you've just got the tag, you can write it in instead:
Code:
return \'<body style="background: #fff url(\\\' + bg[rnd] + \\\') no-repeat fixed center center">\'
This uses CSS which is far more flexible than embedded tag attributes. The above is a shortened background tag which combines background-color, background-image, background-repeat, background-attachment, background-position-x and background-position-y.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:12 AM.


Advertisement
Log in to turn off these ads.