Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Popup, mysql query, pass variable back to form

    I have a form, and I want to have a button that someone can click which will open a popup window. That window will run a query on a database and return a listing of the categories field in my table (mysql) and then have it so the person can click on one, then the window will close and put the clicked one into the text box.
    Steps -
    1) Click on button
    2) Display list of all categories from MySQL database
    3) Click on one
    4) Window closes
    5) Item clicked is passed into the textbox
    -
    Here is the MySQL Query I have
    Code:
    SELECT DISTINCT category FROM rp_rares ORDER BY category ASC
    The text box snippet, the form passes this to another script which stores it into the DB
    Code:
    Category:<br/>
    <input type="text" name="category" size="35" value="' . $row['category'] . '"><p></p>
    Thanks!
    P.S. If you can get a dropdown box that displays that query you would be awesome!
    I've also got some code for the passing here.
    Last edited by arcdigital; 06-07-2007 at 11:44 PM.

  • #2
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    Greetings arcdigital,

    Sure this could be done. It's more of a javascript trick than anything.
    It sounds like you already know how to list out all of your categories from the database (if l'm wrong please reply and we'll work from here.)

    But you'll want to create 2 files.
    example.html (or whatever, the page with your form):

    and place the following:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>main page...</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
    <!-- Begin
    function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=500,height=500,left = 390,top = 134');");
    }
    // End -->
    </script>
    </head>
    <body>
    
    <form name="form1" action="javascript:void();">
    Category:<br/>
    <input type="text" name="category" id="element1" value="" />
    <br />
    <input type="button" value="Open the Popup Window" onclick="javascript:popUp('popup.html')" />
    </form>
    
    </body>
    </html>
    Then create a popup.html and paste the following:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>main page...</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
    <!-- Begin
    function moveData(info) {
        opener.document.form1.category.value = info;
        self.close();
        return false;
    }
    // End -->
    </script>
    </head>
    <body>
    <a href="javascript:void();" onclick="moveData('value 1');">value 1</a>
    <br />
    <a href="javascript:void();" onclick="moveData('value 2');">value 2</a>
    <br />
    <a href="javascript:void();" onclick="moveData('value 3');">value 3</a>
    </body>
    </html>
    The bread and butter happens here:
    window.opener.form1.category.value=info;
    found in the moveData(info) function of popup.html

    Here's an online working example for you to test:
    http://www.coastalwebinnovations.com...p/example.html

    Best regards,
    Last edited by Coastal Web; 06-08-2007 at 01:30 AM.

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks! Actually NO IDEA how to list the query.. I just know that is the query I need to use (It'll display what I want) I had a working javascript pass thing that I did here
    Code:
    http://arcnetworks.net/djsec1/rare/onlyaddcat.html
    If you wanna PM me your AIM or MSN we can chat there. I'm going to PM you my script (its in PHP and we'll go from there)

  • #4
    Regular Coder Coastal Web's Avatar
    Join Date
    Oct 2004
    Posts
    225
    Thanks
    12
    Thanked 3 Times in 3 Posts
    I see..
    try this in the popup file:

    Read the comments to understand what's going on and where.
    You'll need to edit with your database log in details...

    PHP Code:
    <?php
    //first we establish a connection to the database...

    /*connect to database*/ 
    $Host "localhost"
    $dbname "DB_NAME"
    $User "DB_USERNAME"
    $Password "DB_PASSWORD"
    $link mysql_connect($Host$User$Password); 
    mysql_db_query($dbname$sql$link); 

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>popup...</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript">
    <!-- Begin
    function moveData(info) {
        opener.document.form1.category.value = info;
        self.close();
        return false;
    }
    // End -->
    </script>
    </head>
    <body>

    <?php

    //next we'll select each category form the database once....
    $sql "SELECT DISTINCT `category` FROM `rp_rares` ORDER BY category ASC";
    $result mysql_query($sql) or die('Queryproblem: '.mysql_error ());
    while(
    $row mysql_fetch_array($result)){

    //output a link for this category...
    echo <<<EoC
    <a href="javascript:void();" onclick="moveData('$row[category]');">$row[category]</a><br />
    EoC;
    }

    //close sql connection
    @mysql_close();
    ?>

    </body>
    </html>
    Last edited by Coastal Web; 06-08-2007 at 01:30 AM.


  •  

    Posting Permissions

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