...

View Full Version : Popup, mysql query, pass variable back to form



arcdigital
06-07-2007, 10:35 PM
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


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


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.

Coastal Web
06-08-2007, 12:41 AM
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:

<!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:

<!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/test/popup/example.html

Best regards,

arcdigital
06-08-2007, 12:44 AM
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


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)

Coastal Web
06-08-2007, 01:12 AM
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
//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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum