Page 1 of 2
[SOLVED]Javascript and php help
Posted: Fri Oct 15, 2004 1:20 am
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ї'itemWidthLarge']."" height="" .$rowї'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")){
while( $row = mysql_fetch_array($result)){
echo '<p align="center">';
echo "<img src ="/images/products/royal/" . $rowї'itemURL'] .".jpg" width="" .$rowї'itemWidthLarge']. "" height="".$rowї'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ї'itemName'] .'<br></strong></font></em>';
?>
</td>
</tr>
Thank you for all your help
Posted: Fri Oct 15, 2004 3:18 am
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)
{
url="./images/"+nme;
window.open(url, nme)
}
you can then add all the other bits you want for window hieght, width, blah, blah, blah that you want.
Javascript and php
Posted: Fri Oct 15, 2004 11:14 am
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,
Posted: Fri Oct 15, 2004 11:21 am
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.
Posted: Sun Oct 17, 2004 4:41 pm
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.
Javascript, php - open new window on click help
Posted: Mon Oct 18, 2004 11:05 pm
by suz_1234
feyd | Help us, help you. Please use 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 '<a href="javascript:;" target= "newImage" onClick="window.open("http://www.shalemdistributors.com/web/gifts/javascript.php"), "", width=""'.$rowї'itemWidthLarge']."" height="" .$rowї'itemHeight']. "">" ;
echo "<img src ="/images/products/royal/" . $rowї'itemURL'] .".jpg" width="" .$rowї'itemWidth']. "" height="".$rowї'itemHeight']."" align="middle">";
<iframe id="newImage"
name="newImage"
style="width:0px; height:0px; border: 0px"
src="blank.html">
</iframe>
Do you think i need the <iframe> stuff
Here's my js.php
Code: Select all
<head>
<script type = "text/javascript">
<!--
document.writeln("<table width="90%" border="0" cellspacing="0" cellpadding="0">");
document.writeln("<tr> <td colspan="2"> ");
document.writeln(findImage());
function findImage(){
<?php
if($result = mysql_query("SELECT * FROM $table WHERE catID=6")){
while( $row = mysql_fetch_array($result)){
echo '<p align="center">';
echo "<img src ="/images/products/royal/" . $rowї'itemURL'] .".jpg" width="" .$rowї'itemWidthLarge']. "" height="".$rowї'itemHeightLarge']."" align="middle">";
document.writeln("</td> </tr>");
document.writeln("<tr> <td colspan="2">");
echo '<strong><em><font color="#0066FF" size="4" face="Arial, Helvetica, sans-serif">';
echo $rowї'itemName'] .'<br></strong></font></em>';
document.writeln("</td> </tr>");
document.writeln("<tr> <td colspan="2">");
<!-- Shopping cart goes in here
document.writeln("</td> </tr>");
document.writeln("<tr> <td width="72%">");
document.writeln("shalemdistributors.com");
document.writeln("</td>");
document.writeln("<td width="28%"> <div align="left">");
document.writeln("<a href="javascript:window.close();">");
document.writeln("Close This Window");
document.writeln("</a> </div></td></tr>");
}
}
mysql_close();
document.writeln("</table>");
?>
}
</script>
</head>
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
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Mon Oct 18, 2004 11:16 pm
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.
Javascript, php - open new window on click help
Posted: Mon Oct 18, 2004 11:35 pm
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
Posted: Mon Oct 18, 2004 11:41 pm
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.
Javascript, php - open new window on click help
Posted: Tue Oct 19, 2004 1:45 am
by suz_1234
refer to the post below
Javascript, php - open new window on click help
Posted: Tue Oct 19, 2004 1:47 am
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,
Posted: Tue Oct 19, 2004 2:27 am
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.
Javascript, php - open new window on click help
Posted: Tue Oct 19, 2004 2:34 am
by suz_1234
I am sorry.. I am sooo confused... can I please see an example of the same sort...
Thanks much,
Posted: Tue Oct 19, 2004 2:38 am
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());
Javascript and php book
Posted: Tue Oct 19, 2004 10:44 am
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,