www.webdeveloper.com
Results 1 to 8 of 8

Thread: GET_variable and javascript

  1. #1
    Join Date
    Nov 2004
    Posts
    6

    GET_variable and javascript

    I'm working with a while-loop that generates links
    The while-loop generates this:

    PHP Code:
    PHP Code:
     echo 
    "<a href=\"javascript:PopupPic('".$bestandsnaam."')\">"
    echo "<img src='".$bestandsnaam."' border='0' height='60'></a>" 

    I would like to set a GET_variable, the variable is an id ,
    but the id is generated together with the links in a while loop.
    Because each link has a unique id I would like the unique id to be appended somewhere in it's appropriate link.

    when the link is clicked a piece of javascript code is executed:
    Code:

    Code:
    <script language="Javascript"> function PopupPic(sPicURL) { window.open( "../../lib/php/popup.inc.php?"+sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200"); } </script>


    I don't want the id appended in this piece of code:
    When the id is appended in this code, the value of the id is the last value that came out the while-loop and that's not what I need.

    Any clue?

  2. #2
    Join Date
    Feb 2005
    Location
    BC, Canada
    Posts
    1,098
    Sorry if this is unrelated, but the variable is actually $_GET, not $GET_.

  3. #3
    Join Date
    Nov 2004
    Posts
    6
    okay excuse me it is $GET, but this isn't the issue.
    Thx anyways

  4. #4
    Join Date
    Jan 2003
    Posts
    411
    How about this?

    <JS>
    var picwindow;

    function openwin(url)
    {
    picwindow = window.open(url,"Window","height=405, width=650, toolbar=no, menubar=no, resizable=yes, scrollbars=yes, left=300, top=100, screenX=0, screenY=100");

    picwindow.focus();

    return false;
    }


    </JS>


    Then as you loop through your links with IDs

    for loop
    {
    //grab DB row if from DB query
    $row = @mysql_fetch_array($result);

    <a href="" onclick = \"return openwin('../dir/page.php?id=$row["ID"]');\">Link1</a>
    }


    Hope this helps, I may have misunderstood what you are trying to do. If so, sorry.

  5. #5
    Join Date
    Nov 2004
    Posts
    6
    thanks a lot!

    The problem is I'm no javascript-wizard, so at first sight I don't know how to convert my code to fit yours.

  6. #6
    Join Date
    Jan 2003
    Posts
    411
    ok,

    the top part

    Code:
    <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
    var picwindow;
    
    function openwin(url)
    {
    picwindow = window.open(url,"Window","height=405, width=650, toolbar=no, menubar=no, resizable=yes, scrollbars=yes, left=300, top=100, screenX=0, screenY=100");
    
    picwindow.focus();
    
    return false;
    }
    
    
    </script>
    is just setting up your function for the popup window. It just tells it what size , position, and whether or not you want menus available. Then when you call your function on the onclick event of your links you can point them all to the same page(your popup) and use your ID in the query string to tell the page what picture or whatever to display.

    So if you have many links from a query, just loop through them and give each link the ID for each row from your query. If you are not using a database and hard coding the links then just assign each a different ID.

    SO the code

    Code:
    <a href="" onclick = \"return openwin('../../lib/php/popup.inc.php?id=$row["ID"]');\">Link1</a>
    
    <a href="" onclick = \"return openwin('../../lib/php/popup.inc.php?id=$row["ID"]');\">Link2</a>
    could be

    Code:
    <a href="" onclick = \"return openwin('../../lib/php/popup.inc.php?id=1');\">Link1</a>
    
    <a href="" onclick = \"return openwin('../../lib/php/popup.inc.php?id=2');\">Link2</a>
    each link will call your function and bring up the popup window and pass it the ID in the query string (after the ? example ?id=2) So on your popup page you look for $_GET["id"] and it will hold either a 1 or a 2.

    Man, I hope I did not just explain everything you already knew

    If so, sorry.

    But you should be able to take my JS function and copy and paste it into your code then where ever your links are set them up to call that function in the onclick event like I showed you
    Last edited by tripwater; 07-05-2005 at 02:33 PM.

  7. #7
    Join Date
    Nov 2004
    Posts
    6
    well i found a solution now:
    like this:
    echo "<a href=\"javascript:PopupPic('".$bestandsnaam."','".$project_id."')\"><img src='".$bestandsnaam."' border='0' height='60'></a>";

    and then in the javascript:

    function PopupPic(sPicURL) {
    window.open( "../../lib/php/popup.inc.php?<?php echo 'project_id='.$project_id.'&';?>"+sPicURL, "",
    "resizable=1,HEIGHT=200,WIDTH=200");
    }

    Thanks a lot though for helping me

  8. #8
    Join Date
    Nov 2004
    Posts
    6
    oops made a mistake but now it's working for 100%

    just changed the javascript to:
    function PopupPic(sPicURL,id) {
    window.open( "../../lib/php/popup.inc.php?id="+id+"&"+sPicURL, "",
    "resizable=1,HEIGHT=200,WIDTH=200");
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles