Search code examples
phpjqueryhtmlpopupwindow.open

popup window.open href works in html but need proper syntax for PHP


I'm having a situation that I can't solve. I can't get popup window to work properly in my php page - but that same href for popup in html made table - works - link for popup window with dynamic href - works good. So i guess that the included function is OK. No need to quote the function here then. It opens the popup windows with no problems at all in html and parent page stays as it should.

This is entire A in html table

<A class=black href="<?php if (isset($_GET['art_id'])) { $id = intval($_GET['art_id']); } mysql_query("SET CHARACTER SET utf8"); $query=mysql_query("SELECT art_id FROM table WHERE art_id = '" . mysql_real_escape_string($id) . "' ") or die("err"); while($row=mysql_fetch_array($query)) { echo "ab.php?art_id=$row[art_id]"; } ?>" onclick="NewWindow(this.href,'rank','450','230','no','center');return false" onfocus="this.blur()" settings>GO</A></div>

However, when this same dynamic a href when put inside a php generated table *with same function applied, i get double loading. Popup loads allright but parent page also loads the content of the popup window.

This is complete A for php generated table

echo "<a href='ab.php?art_id=$row[art_id]' onclick=NewWindow(this.href,'rank','450','230','no','center') return=false onfocus=this.blur()>".'GO'."</a>";

This one opens allright in popup but also loads ab.php in main, parent window. I treid with target:_blank, _child, tried to enter actual url etc... tried with different \'NewWindow ... settings... Nothing. Now I know that this is related to \'NewWindow etc , with this href quoting in php, but I can't seem to find any reasonable explanation how to do it ; every explanation for popup is for html based ones without echo, without already quoted lines. I know this is all deprecated and there are other way to do it - but i have to do it this way for my project. Can someone help me, please?


Solution

  • You have to put asteriks around the attributes of you output. You can use ', " and you can escape them with \.

    The problem was that you wrote return=false as own ht l attribute but it is part of onclick. (It tells the browser not to open the url in the main window)

    echo '<a href="ab.php?art_id='.$row['art_id'].'" onclick="NewWindow(this.href,\'rank\',\'450\',\'230\',\'no\',\'center\'); return false;" onfocus="this.blur();">GO</a>';
    

    And please don't write PHP code all in one line, as in the working example above. Nobody wants to read this and this is a common place for bugs. Use functions and variables!