[SOLVED]Javascript and php help

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I have no such book. I have books on Javascript and advanced PHP seperately. Writing Javascript in PHP isn't rocket science. It's still just typing text to the browser. :P
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

I usually write my javascript seperately and use php to do a script src=js page.
If I really need php to write my javascript I still write the javascript seperately first just to make sure it is working correctly then I very, very carefully put my php code and tags and \" and other such niceties into my js code.
It can be a bit of a headache but you will get there, just keep things simple first then work up from there.

phpScott
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript error

Post by suz_1234 »

Hi,

I am still having trouble with javascript :cry:

Here's my code where i am trying to open an image in a new window..

Code: Select all

<?php
<script>

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
</script>


echo '<a href="javascript:;" onClick="MM_openBrWindow('"/images/products/royal/".$row['itemURL'].".jpg"','','"width="".$row['itemWidthLarge']. "",height="".$row['itemHeightLarge']."""')">';


?>
I am getting an error saying "Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' "

Can somebody please help me with this error.. I tried to find it but couldnt...
The error is on that link.

Thanks
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript pop-up window help

Post by suz_1234 »

Hi..

I was using the Javascript code by phpScott and it sort of works... now it opens a new window, but it does not load the image... at the end of the url it says 'undefined' ex. http://www.xyz/image/undefined

I kinda changed phpScott's code to suite my style...
This is the function that I used from Scott's

Code: Select all

function displayLargeImage(nme) 
{ 
  url="./images/products/royal/"+nme+".jpg/"; 
  window.open(url, nme) 
}

Code: Select all

$x.="<a href="javaScript:displayLargeImage(".$results[$row]['itemURL'].")">\n";

echo $x;
Can someone please tell me why it says undefined, instead of getting part of the image name from 'itemURL'?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

2nd last message - you have a single quote in there that needs escaping.


last message - you may need to add single quotes around the image url you are adding. I'm not a wiz at js - I've been spending a while catching up after leaving it in the dust a few years back but a few changes will also help in the function I think - try declaring the url variable first:

Code: Select all

<?php
function displayLargeImage(nme) 
{ 
  var url;
  url = "./images/products/royal/" + nme + ".jpg/"; 
  window.open(url, nme);
}

?>
In your actual link from PHP:

Code: Select all

<?php

$x.="<a href="javaScript:displayLargeImage('".$results[$row]['itemURL']."')">\n";

?>
Adding those extra single quotes just might do the trick...
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

this function here is just a little wrong, probably my fault try this.

Code: Select all

function displayLargeImage(nme) 
&#123; 
  url="./images/products/royal/"+nme+".jpg/"; 
  window.open(url, 'newPage') 
&#125;
the window.open synatax requires at least the url of the window you are opening and a proper window name. You can do all sorts of other stuff, just look it up on the web. the window name needs to be quotes if it is not a js variable.

try alerting the url before you do the window.open to make sure it looks alright.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript pop-up window help

Post by suz_1234 »

This is frustrating... I cant believe Javascript can become this hard for me..

This pop-up window still doesnt work

I tried the second last help i received adding single quotes, but it takes me to an unrestricted page

The last post (phpScott).. didnt help.. its still giving me undefined.jpg

This is really getting on my nerves... is there any other way of doing this???

Also if this helps... when i look at the source of the thumbnail page... after every thumbnail javascript: displayLargeImage() repeats..
For example, on the first image the line is printed once
on the second thumbnail that line is repeated twice.. and so on
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

call this page somethink like showpic.php

Code: Select all

<?php
  if(isset($_GET['picName']))
	{
	  $src="<img src="".$_GET['picName']."" alt="whatever you want">
	}
	else
	{
	  $src="<img src="default.gif" alt="default pic">
	}
?>?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php if(isset($src)) echo $src; ?>
</body>
</html>

?>
then in you js that makes the new window do this.

Code: Select all

function displayLargeImage(nme)
&#123;
  url="./images/products/royal/showpic.php?picName="+nme+".jpg/";
  window.open(url, 'newPage')
&#125;
Hopefully that will solve your problem
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript pop-up window help

Post by suz_1234 »

I am so sorry Scott.. u've been a great help... it still doesnt work..

Here's the link that i am working on, its not a perfect looking page...

http://www.shalemdistributors.com/web/g ... xample.php

Thanks
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Progress !!!!

Post by suz_1234 »

Now I am somewhat happy..

I took the $result out in my <a href>.. i didnt need that because i have a while statement...

So now here's the next problem... it url points to the right address, but give me a page saying 'File not found'... does this have something to do with .jpg in the displayLargeImage() function in my js???

the link is in the previous post if anyone of you to see what I am trying to explain :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Rather than answer your specific problem I thought I'd just give you some hints for debugging Javascript.

1) Use a Firefox/Mozilla Browser and activate the Javascript Console (under Tools Menu).

2) When you have a problem combining PHP and Javascript save the document source (created with PHP) to a file and load it on it's own using the browser. Debugging the javascript on the page should allow you to see what is going wrong in your PHP, especially when it comes to quotes.

3) Where possible have a separate javascript file containing all the necessary functions and load it using HTML. This way only functions calls should need to be called from the PHP.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Ok to answer your actual question...

Code: Select all

http://www.shalemdistributors.com/images/products/royal/imagePage.php
does not exist. Nothing to do with Javascript.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

get rid of the final slashs in the javascript

Code: Select all

function displayLargeImage(nme)
&#123;
  url="./images/products/royal/showpic.php?picName="+nme+".jpg";
  window.open(url, 'newPage')
&#125;
and see if that works.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript and php help

Post by suz_1234 »

Thanks to both of you (previous two post).. finally I can take a deep breath .. it works Javascript Rocks!!!

Thanks guys, you have been great and to everyone to tried to help me.
God Bless you all.
Post Reply