Zeeshan Rang
Zeeshan Rang

Reputation: 19875

Opening a new window using php and displaying data in that

I am having a problem in my php. What I'm trying to do here is, that I have a .html which directs to an external .js and this js file open up a .php The php extracts data from mysql tables and xml and displays the data on the webpage, (in .html form as table rows). Now when a user clicks on any row, I want that php should open up a new window and I want to display some more data from the same mysql and xml on it (the new window)

for($k=0;$k<=$x->length-1;$k++)
{
    for($l=0;$l<=$j-1;$l++)
    {
        if($y->item($k)->nodeValue==$JobNoArr[$l])
        {
            $m++;
            if ($m%2==0)
                {$a="#A5ADEA";}
            else 
                {$a="#D1D1D3";}

            //form_html1+="<tr onclick='show(" + x + ")' bgcolor="+ y +">";
            echo "<tr bgcolor=". $a .">";
            echo "<td>" . $m . "</td>";
            echo "<td>" . ($y->item($k)->nodeValue) . "</td>";
            echo "<td>" . ($TitleArr[$l]) . "</td>";
            echo "<td onclick=show(".$u->item($k)->nodeValue.")><i><font size=2>Click for Abstract</font></i></td>";
            echo "<td>" . $uu . "</td>";
            echo "</tr>";
        }
    }
}

If I use echo "<td onclick=show() " it opens the show function in the .js file but if I use echo "<td onclick=".show()."... " it opens the function in php itself but without the onclick functionality.

I don't know if have been able to clearly explain my problem here, but if you can help me in this, please do so.

Upvotes: 1

Views: 11487

Answers (3)

defines
defines

Reputation: 10534

You don't want to actually "do this in PHP", you're going to do it with JavaScript. PHP is a server-side technology, and what you want to do needs to happen on the client side (browser). There are a few different ways you can go about this; the method I outline below uses very little JavaScript.

First of all, you need to rewrite your onClick as follows:

echo "<td onClick=\"return show('".$u->item($k)->nodeValue."');\"><i><font size=2>Click for Abstract</font></i></td>";

We are going to use the window.open function to actually pop up a new window - this will require that you create a new PHP script that can display the data you want to show, given the specified row ID or some other identifier. In this case, your show function would be something like this:

I've updated the example below to be output from within PHP

echo "
 <script type=\"text/javascript\">
 <![CDATA[
   function show(rowid, arrno) {
     window.open(
       'showAbstract.php?rowid='+rowid+'&rowid2='+arrno,  // the url to the php script
       'somenameforthewindow',                            // some name for the window
       'status=0,width=100,height=100'                    // display options for the window
     );
     return false;
   }
 ]]>
 </script>";

Your PHP page should grab the $_GET['rowid'] parameter, lookup the appropriate data, and format/print it as you would like it to appear.

You could actually embed all your data in JSON and use DHTML and CSS Layers to have a similar effect without additional windows. If you would like, I can post an example of this.

Upvotes: 2

bradpurchase
bradpurchase

Reputation:

Like said above, you'd want to use a javascript modal window to display the content. You can (with most modal windows anyway) use PHP code in the modal window, allowing you to use the same MySQL and XML, but PHP as a server-side technology cannot open a new window without refreshing the page or opening a new page entirely.

If you use jQuery (which I reccommend), I would suggest Facebox as a great, lightbox-style modal window that you can easily use and even customize yourself.

Hope this helps.

Upvotes: 0

Zak
Zak

Reputation: 25205

you can probably use YUI to create a floating dialog that is hidden, and contains the data for the "abstract" . Default to hiding all the abstracts for all the items on the page. Your js show function can just set that YUI element visible when the user clicks on the item. You won't even need to open a new browser window.

Upvotes: 2

Related Questions