[SOLVED]Javascript and php help

JavaScript and client side scripting.

Moderator: General Moderators

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

[SOLVED]Javascript and php help

Post by suz_1234 »

Hi,

I have trying to embed JavaScript in my php pages. I have a product.php page which has product info and the product thumbnail image. I would like to include a JS script, so that when a user click on the thumbnail and new page appears with the bigger image.

Can someone help me with this. I have never done JS before, but it would be nice if someone can guide me to the right path or maybe show me some part of a code to help me with this.

So far, I have the product.php and backend is mysql. So maybe JS page should dynamically go to the database to get that image for that particular click...
My product.php has this line, which tries to open the JS page but when i click on the image it does not do anything.

Code: Select all

echo '<a href="javascript:;" onClick="MM_openBrWindow("imagePage.php", "", width=""'.$row&#1111;'itemWidthLarge']."" height="" .$row&#1111;'itemHeight']. "">" ;
my imagePage.php has code for mysql database connection and the look and feel of the page. Something like this:

Code: Select all

<table width="90%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td colspan="2"> 
      <?php
		if($result = mysql_query("SELECT * FROM $table WHERE catID=6"))&#123;
		  		while( $row = mysql_fetch_array($result))&#123;
				
				echo '<p align="center">';
				echo "<img src ="/images/products/royal/" . $row&#1111;'itemURL'] .".jpg" width="" .$row&#1111;'itemWidthLarge']. "" height="".$row&#1111;'itemHeightLarge']."" align="middle">";;
?>
    </td>
  </tr>
  <tr>
    <td colspan="2"> 
      <?php
				echo '<strong><em><font color="#0066FF" size="4" face="Arial, Helvetica, sans-serif">';
				echo $row&#1111;'itemName'] .'<br></strong></font></em>';
?>
    </td>
  </tr>
Thank you for all your help
Last edited by suz_1234 on Fri Nov 05, 2004 1:10 am, edited 1 time in total.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

this is the javascript that i use for this purpose

Code: Select all

<?php
$row.="<a href="javaScript:displayLargeImage(".$results[$x]['logoName'].")">\n";
?>
creates the php code from my db the $results bit.

the javascript is

Code: Select all

function displayLargeImage(nme)
&#123;
  url="./images/"+nme;
  window.open(url, nme)
&#125;
you can then add all the other bits you want for window hieght, width, blah, blah, blah that you want.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript and php

Post by suz_1234 »

Question??

In you php code, where does $x come from?? And where would you pass the url for the displayLargeImage() function...

Also I am trying to include the name of the product and Shopping Cart in the new window.. is that possible and easier to do?? I use Paypal shopping cart so I already have their code and the cart gets the items from the database all of that works...

Thanks for helping,
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

sorry I use my on db class the outputs the results that way so $x would be the $x in the for loop.

Yes you should be able to pass what ever info you want to the new window, especially if the new window is a php page you can just concatinate all the variables you need onto the end then on the new window use the $_GET['varName'] to get at it.
ie
in your js

Code: Select all

url="largeImage.php?sameVar="+document.getElementById("someField").value+"&secondVar="+document.getElementById("someField2").value;
the logo name is the name of the file that I want to open in the new window. so that of course has to get put into the string to be passed to the new window as well.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make sure to use the javascript escape function on those values.

this doesn't appear to have anything real to do with databases, so this thread has been moved to client side.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript, php - open new window on click help

Post by suz_1234 »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,

Sorry I was not able to follow the previous posts. I am very new to Javascript, but I tried following my Deitel book  Didnt have any other book handy... also I ready one of the Javascript links on this form

Here's what I got so far:

My javascriptexample.php, this has the thumbnail pictures and when the user click on it, it should open up js.php

Code: Select all

echo '&lt;a href="javascript:;" target= "newImage" onClick="window.open("http://www.shalemdistributors.com/web/gifts/javascript.php"), "", width=""'.$row&#1111;'itemWidthLarge']."" height="" .$row&#1111;'itemHeight']. ""&gt;" ;

echo "&lt;img src ="/images/products/royal/" . $row&#1111;'itemURL'] .".jpg" width="" .$row&#1111;'itemWidth']. "" height="".$row&#1111;'itemHeight']."" align="middle"&gt;";

&lt;iframe id="newImage"  
            name="newImage"  
	style="width:0px; height:0px; border: 0px"  
	src="blank.html"&gt;
&lt;/iframe&gt;
Do you think i need the <iframe> stuff

Here's my js.php

Code: Select all

&lt;head&gt;
		&lt;script type = "text/javascript"&gt;
			&lt;!--
			
			document.writeln("&lt;table width="90%" border="0" cellspacing="0" cellpadding="0"&gt;");
			document.writeln("&lt;tr&gt; &lt;td colspan="2"&gt; ");
			document.writeln(findImage());			
			
			function findImage(){
				&lt;?php
					if($result = mysql_query("SELECT * FROM $table WHERE catID=6")){
		  				while( $row = mysql_fetch_array($result)){
				
							echo '&lt;p align="center"&gt;';
							echo "&lt;img src ="/images/products/royal/" . $row&#1111;'itemURL'] .".jpg" width="" .$row&#1111;'itemWidthLarge']. "" height="".$row&#1111;'itemHeightLarge']."" align="middle"&gt;";
							document.writeln("&lt;/td&gt; &lt;/tr&gt;");
							document.writeln("&lt;tr&gt; &lt;td colspan="2"&gt;");
							echo '&lt;strong&gt;&lt;em&gt;&lt;font color="#0066FF" size="4" face="Arial, Helvetica, sans-serif"&gt;';
							echo $row&#1111;'itemName'] .'&lt;br&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/em&gt;';
							document.writeln("&lt;/td&gt; &lt;/tr&gt;");
							document.writeln("&lt;tr&gt; &lt;td colspan="2"&gt;");
							&lt;!-- Shopping cart goes in here
							document.writeln("&lt;/td&gt; &lt;/tr&gt;");
							document.writeln("&lt;tr&gt; &lt;td width="72%"&gt;");
							document.writeln("shalemdistributors.com");
							document.writeln("&lt;/td&gt;");
							document.writeln("&lt;td width="28%"&gt; &lt;div align="left"&gt;");
							document.writeln("&lt;a href="javascript:window.close();"&gt;");
							document.writeln("Close This Window");
							document.writeln("&lt;/a&gt; &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;");
						}
					}
					mysql_close();
					
					document.writeln("&lt;/table&gt;");
				?&gt;			
			}
		
		&lt;/script&gt;
	&lt;/head&gt;
When I do this and upload it on my server, the javascriptexample.php has whole bunch of error which are cause by the <a href=> </a> on the thumbnails.. any ideas? I know this might be an immature way to do javascript, but this is my first attempt.

Thanks for helping


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

line 1 in the posted javascriptexample.php has errors in the html/javascript syntax. Switch to using singlequotes (escaped) in the javascript code of the onclick attribute. The call to window.open is missing arguments.. (closed the function call prematurely.)

js.php has a lot of mixing between nonexistant functions (in php) and echoing html into a javascript function.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript, php - open new window on click help

Post by suz_1234 »

Thanks for the reply,

I fixed the quotes and it does not give me any errors in my javascriptexample.php

What do I do with js.php?

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

Post by feyd »

you need to echo the "document.writeln(......." calls. ... or the way you are calling the function, you are supposed to return a string from it.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript, php - open new window on click help

Post by suz_1234 »

refer to the post below
Last edited by suz_1234 on Tue Oct 19, 2004 2:04 am, edited 1 time in total.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript, php - open new window on click help

Post by suz_1234 »

Alright here's an updated version of js.php

Code: Select all

<?php
<script type = "text/javascript">
			<!--
			document.write("<?php if(isset($content)) echo $content; ?>");
			document.write("<?php echo '<table width="90%" border="0" cellspacing="0" cellpadding="0">' ?>");
			document.writeln("<?php echo "<tr> <td colspan="2">" ?>");
		<?php
					if($result = mysql_query("SELECT * FROM $table WHERE catID=6")){
		  				while( $row = mysql_fetch_array($result)){
				
							document.writeln("<?php echo "<p align="center">'?>');
							document.writeln("<?php echo '<img src ="/images/products/royal/" . $row['itemURL'] .".jpg" width="" .$row['itemWidthLarge']. "" height="".$row['itemHeightLarge']."" align="middle">'?>");
							document.writeln("<?php echo "</td> </tr>" ?>");
							document.writeln("<?php echo '<tr> <td colspan="2">'?>");
							document.writeln("<?php echo '<strong><em><font color="#0066FF" size="4" face="Arial, Helvetica, sans-serif">' ?>");
							document.writeln("<?php echo $row['itemName'] .'<br></strong></font></em>' ?>");
							document.writeln("<?php echo '</td> </tr>' ?>");
							document.writeln("<?php echo "<tr> <td colspan="2">" ?>");
							<!-- Shopping cart goes in here
							document.writeln("<?php echo "</td> </tr>" ?>");
							document.writeln("<?php echo "<tr> <td width="72%">" ?>");
							document.writeln("<?php echo 'shalemdistributors.com' ?>");
							document.writeln("<?php echo "</td>" ?>");
							document.writeln("<?php echo "<td width="28%"> <div align="left">" ?>");
							//document.writeln("<a href="javascript:window.close();">");
							document.writeln("<?php echo "Close This Window" ?>");
							document.writeln("<?php echo "</div></td></tr>" ?>");
							
						}
					}
					mysql_close();
					
					document.writeln("<?php echo'</table>' ?>");
				?>			
			
		
		</script>
?>
When I click on the thumbnail links, it does not do anything... can u tell me what are the possibilities to make it work..

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

Post by feyd »

you got it turned around.. you need to have php echo the html you want to print out in the browser through document.writeln.
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript, php - open new window on click help

Post by suz_1234 »

I am sorry.. I am sooo confused... can I please see an example of the same sort...

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

Post by feyd »

Code: Select all

<script language="Javascript">

function example()
{
  var buf = '';
  <?php
    echo 'buf += "blah blah blah blah";';
    echo 'buf += "blah blah blah blah";';
    echo 'buf += "blah blah blah blah";';
    echo 'buf += "blah blah blah blah";';
    echo 'buf += "blah blah blah blah";';
  ?>
  return buf;
}

document.writeln(example());
suz_1234
Forum Newbie
Posts: 24
Joined: Sat Sep 04, 2004 11:22 pm

Javascript and php book

Post by suz_1234 »

Hi,

Can you tell me what is a good book that has helped you with JS and Php??

Thank you so much for helping,
Post Reply