Page 1 of 1
javascript pop-up window and php
Posted: Mon Oct 07, 2002 6:38 am
by gijs
Hi,
I've a problem with javascript and php links:
What I wanna do:
By means of a hyperlink opening a pop-up window and displaying a php-file (passing the variables i know how to)
Problem: got javascript error
I use following script:
Code: Select all
<HEAD>
<script>
function openpopup(){
var popurl="news/news_display.php"
winpops=window.open(popurl,"","width=500,height=200,")
}
</script>
</HEAD>
And I use the following link:
Code: Select all
<?php echo "<a class=news href=javascript:openpopup()?news_id=" . $newsї'news_id'] .">" . 'More ...' ?></a>
Can someone be so kind to help me with this ?
Thanks in advance,
Gijs
Posted: Mon Oct 07, 2002 7:17 am
by volka
javascript as well as php needs a ; after statements.
i.e. var popurl="news/news_display.php";
what is
...."<a class=news href=javascript:openpopup()?news_id=" ...
supposed to do?
Posted: Mon Oct 07, 2002 7:49 am
by gijs
Hi Volka,
Thanks for your reply:
1) Semi-colon:
I used this script many times without ";" and it works fine.
Just for a test I added it, but it doesn't solve anything.
2)
what is
...."<a class=news href=javascript:openpopup()?news_id=" ...
supposed to do?
In fact the link should call display_news.php in a popup window
display_news.php includes 3 other php-files:
a) header.php : displays the name and short_desc of a the news article
b) display_blob.php: displays the selected news_id, which are all pdf's
c) footer_php: cointains 2 buttons: Close Window and Download
This structure I used before and know that it works.
Any suggestions to guide me in what I do wrong with the pop-up ?
Thanks in advance,
Gijs
Posted: Mon Oct 07, 2002 8:07 am
by twigletmac
I know next to nothing about Javascript but I would assume that you would want to send the news_id to the JS function instead of just trying to append it in the href attribute so you'd have something like:
Code: Select all
<a class="news" href="javascript:openpopup('<?php $newsї'news_id']; ?>')">More ...</a>
Code: Select all
<script>
function openpopup(news_id){
var popurl="news/news_display.php?news_id="+news_id;
winpops=window.open(popurl,"","width=500,height=200,");
}
</script>
Mac
Posted: Mon Oct 07, 2002 8:50 am
by gijs
Thanks for the suggestion Mac,
Remarque first:
When I use a href with target=_blank the whole show works fine.
The $news['news_id'] passes well.
Now to the js problem:
Don't worry about js Mac, I know even much less
I tried your code, but the following happens:
a) the popup opens this time (which is already an improvement to my poor excuse of a handeling things)
b) but the variable doesn't pass, so i get my build-in error message.
Any other suggestions ?
Thanks in advance,
Gijs
Posted: Mon Oct 07, 2002 9:02 am
by twigletmac
I managed to get a variable to pass using my own little test script:
Code: Select all
<script>
function openpopup(test){
var popurl="test.php?test="+test;
winpops=window.open(popurl,"","width=500,height=200,");
}
</script>
<a href="javascript:openpopup('foobar')">More ...</a>
<?php
if (isset($_GETї'test'])) {
echo '$_GETї''test''] = '.$_GETї'test'];
}
?>
Check the source code of the page to make sure that a value is being passes to the openpopup() function.
Mac
Posted: Mon Oct 07, 2002 10:12 am
by gijs
Thanks Mac,
I made a mistake in describing the problem:
news_display.php does NOT contain includes ...
In fact its a php-file containing a frameset which passes thru the $ to the 3 different php-files.
But it don't think that's the problem anymore.
The variable get's passed well to the footer, because the 2 buttons appear.
In news_display_footer.php, I've got following code:
Code: Select all
<?php
if (isset($_GETї'news_id'])) {
?>
<form method="GET" action="news_download.php">
<input type="hidden" name="news_id" value="<?php echo $_GETї'news_id'] ?>">
<td align="right" width="50%"><input type="submit" name="submit" value="Download"></td>
</form>
</tr>
<?php
}
?>
So if the variable wasn't set it wouldn't display the "Download"-button, right ?
But one thing it doesn't is function...
So something is still going wrong somewhere ...
the news_display_header.php doens't show anything and it uses the same $
When using your test-method in news_display_header.php
Code: Select all
// Check if the variable news_id is set
if (isset($_GETї'news_id'])) {
echo '$_GETї''news_id''] = '.$_GETї'news_id'];
it displays:
$_GET['news_id'] =
Do I see this correctly that the $ is passed but not the value of the $ ?
I'm completely in the mist now ... it starts raining cats and dogs in Belgium
Gijs
Posted: Mon Oct 07, 2002 11:24 am
by twigletmac
Basically the problem is not to do with the javascript. Your PHP script is not passing the $news['news_id'] variable. Is this the variable that you want to use because the footer uses $_GET['news_id']?
Mac
Posted: Mon Oct 07, 2002 11:58 pm
by gijs
Thanks Mac,
for your help and ... your patiance.
Concerning your question:
yes, it is $news['news_id'] that is send whit the hyperlink and will be received first in display.php and passed on from there to the 3 other framed php-pages, each time with $_GET['news_id'].
What is puzzling me is, that when the hyperlink is triggered with a simple "href=display.php?news_id...", the variable is passed well thru the whole chaine of files.
But when trying to use a simple javascript in stead, the whole thing collapses.
Btw, this $-thing reminds me of my first steps into the world of php about 3 weeks ago ... and the guidance you gave me to take the first hurdle.
Hmm ... I'm getting romantic at my old age !
Thanks again,
Gijs
EUREKA
Posted: Wed Oct 09, 2002 3:51 pm
by gijs
Hi and thanks for the help,
Solution to the problem (provided by daveyboy and weedpacket):
Code: Select all
<head>
<script language=javascript>
function OpenNews( newsid )
{
var newswin = window.open( 'news/news_display.php?news_id=' + newsid,
'newswin', 'toolbar=no, location=no, directories=no, \
status=no, menubar=no, scrollbars=yes, resizable=yes, \
copyhistory=no,width=600,height=450')
window.blur()
newswin.focus()
}
</script>
</head>
In the HTML:
Code: Select all
<a href="java script:OpenNews( '<?php echo $newsї'news_id'] ?>' )">News</a>
Hope this can be usefull for someone else as well.
Gijs