Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Search Results on Same Page as Search

Status
Not open for further replies.

frazer

Technical User
Oct 1, 2001
6
GB
Hi Guys,

I'm currently querying a database to display contents in drop-down list using the code below:

<?

$connection = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;password&quot;)
or die(&quot;Couldn't make connection.&quot;);

$db = mysql_select_db(&quot;database&quot;, $connection)
or die(&quot;Couldn't select database.&quot;);

$sql = &quot;SELECT DISTINCT code, title FROM modules ORDER BY code ASC&quot;;

$sql_result = mysql_query($sql,$connection)
or die(&quot;Couldn't execute query.&quot;);

while ($row = mysql_fetch_array($sql_result)) {

$code = $row[&quot;code&quot;];
$title = $row[&quot;title&quot;];

if ($curr_code == $code) {

$option_block .= &quot;<OPTION value=\&quot;$code\&quot; selected>$code - $title</OPTION>&quot;;

} else {

$option_block .= &quot;<OPTION value=\&quot;$code\&quot;>$code - $title</OPTION>&quot;;

}
}

?>

<FORM method=post&quot; action=&quot;courses.php&quot;>

<P>Course Code:<br>
<SELECT name=&quot;code&quot;>

<? echo &quot;$option_block&quot;; ?>

</SELECT>

<P><INPUT type=&quot;submit&quot; value=&quot;submit&quot;></p>

</FORM>

where code and title are fields in my database.

The two fields currently reside in the table &quot;modules&quot;.

When the user clicks on the submit button I want another table to be queried (&quot;materials&quot;) and all records where the field &quot;code&quot; is the same as the search criteria to be displayed on the same page (under the drop down list).

I've been trying lots of different ways and fail each time.

Any help in doing this or pointers to exisiting scripts would be greatfully received.

Thanks in advance,

Frazer
 
If you want this all in the same page you should do an if condition -- display the form you have shown (with a value selected if appropriate), then if the field in your form is defined also do the query and display results.

$selectedCode = $code;
$option_block .= &quot;<OPTION value=\&quot;$code\&quot;&quot;;
if ($code == $selectedCode) $option_block .= &quot; selected&quot;;
$option_block .= &quot;>$code - $title</OPTION>&quot;;
...
<select name=&quot;code&quot;>
...


if (strlen($selectedCode)) {
[DO YOUR QUERY]
[DISPLAY RESULTS OF QUERY]
}

Hope that helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top