Click to See Complete Forum and Search --> : PHP database query & JavaScript display


HighOrbit
02-17-2003, 02:41 PM
Hope somebody can help me out because I am stuck.

I want to have a form populate when a select box on the same page is changed/submited. The select box is populated from a database by php. When the select box is changed/submitted, it will pass its variable to another frame where php will perform a query and return the results via javascript to the first frame. The idea is that the second frame will (eventually) be hidden so it will apear that the page is dynamic, when in fact data is being passed to another php page to run the query.

My problem is that it takes two submits to get the query result and to get javascript to populate the form on the first page. I would like this to work on the first submit. Can anybody give any pointers?

I have included the code for the frameset and both frames.
//*******************************
Here is the frameset:
//*********************************
<html>
<head>
<title>Frame </title>
</head>
<frameset COLS="30%,70%">
<frame SRC="script.php" NAME="left">
<frame SRC="php/phptest.php" NAME="content">
</frameset>
</html>

//*******************************
here is the main frame with the forms. file name is phptest.php
//***************************************
<?php

$USR = 'name';
$PWD = 'pwd';
$DB = 'test';


echo"<form name=cust_select target = left action=/script.php><select name ='cust_name'>";


$link = mysql_connect( 'localhost', $USR, $PWD )
or die ("Could not connect");

mysql_select_db ("test", $link);

$result = mysql_query ("SELECT name FROM customer", $link)
or die ("Invalid query");

while ($data = mysql_fetch_row ($result)){
$name = $data[0];

echo"<option>$name</option>";
}

echo "</select>";

echo "<input type=submit onclick='javascript:parent.left.showInfo(10)'</form>";
?>


<FORM NAME="customer">
<TEXTAREA ROWS="7" COLS="30" NAME="cnum" READONLY WRAP>

</TEXTAREA>
</FORM>

//***********************
here is the query frame, this will be hidden. It pulls the query result and passes it back via javascript to the first frame. file name is script.php
//************************
<?php
$USR = 'root';
$PWD = 'Nasa927!';
$DB = 'test';

$cust_name=$_GET[cust_name];

$link = mysql_connect( 'localhost', $USR, $PWD )
or die ("Could not connect");

mysql_select_db ("test", $link);

$result = mysql_query ("SELECT cust_no FROM customer where name ='$cust_name'", $link)
or die ("Invalid query");

while ($data = mysql_fetch_row ($result)){
$_no = $data[0];
}
echo "$cust_name number is $_no"; //just to see a reult during testing
?>

<SCRIPT LANGUAGE="JAVASCRIPT">
cust_no = new Array
<? echo " cust_no[10] = $_no \n"; ?>
function showInfo(thisNum) {
parent.content.document.customer.cnum.value = cust_no[thisNum]
}
</SCRIPT>