User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 375,174 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,214 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 19171 | Replies: 14
Reply
Join Date: Jul 2005
Posts: 11
Reputation: lawfour is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
lawfour lawfour is offline Offline
Newbie Poster

php drop down menu to search multiple sql tables

  #1  
Jul 27th, 2005
I am about to pull what little hair I have let trying to figure if this is possible. On my website I have a drop down menu with different tables in the drop down part.

What I am trying to do is have a user choose the table to get info from then in the search box type in keywords of what to search for.

Can this be done if so HOW? :eek:

Thanks

my website has the menu I am talking about but of course it does not work. www.jci.tju.edu/~lwilliam/ Need to find a script or somewhere that shows me how to do it. (The tables are not related, some may have a feild or two that are the same.)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Kansas City, Missouri, USA
Posts: 344
Reputation: Troy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: php drop down menu to search multiple sql tables

  #2  
Jul 27th, 2005
The values in the dropdown are actual table names in your database? And you want the user to enter a search term, pick a database table, click submit, then see results from that table?

What database are you using? MySQL? Postgres? MSSQL? Oracle?

This is a simple thing to do unless one or more of the following statements are NOT true. Please reply to let us know where you need help.

1. I know how to submit an HTML FORM to a PHP page and work with the $_POST, $_GET, $_REQUEST objects.

2. I know how to connect to a database, query a database, and work with the results with PHP.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote  
Join Date: Jul 2005
Posts: 11
Reputation: lawfour is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
lawfour lawfour is offline Offline
Newbie Poster

Re: php drop down menu to search multiple sql tables

  #3  
Jul 27th, 2005
I am using MySQL

Q1. I know how to submit an HTML FORM to a PHP page and work with the $_POST, $_GET, $_REQUEST objects.

A1. I know how to submit an HTML form using PHP but I never used $_POST, $_GET, $_REQUEST at least not in php.

I use this to submit forms on my site:
mail("me@emailaddress.com", "KCC Abraham Gas Order", "Air Gas Link: weblink.php \n\n Investigator: $x_Investigator \n\n Requested by: $x_Requested_By \n\n Customer Number:",
"From: larry@{$_SERVER['SERVER_NAME']}\r\n" );

Q2. I know how to connect to a database, query a database, and work with the results with PHP.

A2. I use PHPMAKER to create my php/sql pages at the moment I am good at editing just about anything.

So I don't have a clue on where to start. This is as far as I can go

My Table Names

<select name="select">
<option>Airgas Tickets</option>
<option>Products</option>
<option >Pending Repairs</option>
<option selected>Common Equip.</option>
<option >Lab Equip.</option>
<option >Vendors</option>
<option >RTP's</option>

</select>

I know sad but I am trying.

Thanks

LW
Reply With Quote  
Join Date: Jul 2004
Location: Arizona State University
Posts: 56
Reputation: bwest is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
bwest's Avatar
bwest bwest is offline Offline
Junior Poster in Training

Re: php drop down menu to search multiple sql tables

  #4  
Jul 27th, 2005
Hi Lawfour

Ok the first thing is I think your ready to move on to hand-writing your php instead of using a tool to do it for you. The people in this form are great and we can answer almost any question you have. -And while writing by hand might take a while at first you will eventually generate many methods and algorithms that you will re-use over and over so things will eventually become less frustrating.

Ok, here is how you will need to do form submission for now on. The mail() method simply wont work.

Make a form like this

[HTML]<form name="thisForm" method="POST" action="process.php">
<p><input type="text" name="my_textbox" size="20"></p>
<p><select size="1" name="my_dropdown">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
</select></p>
<p><input type="buttom" value="Submit" name="btm_submit" onclick="validate()"></p>
</form>[/HTML]

Notice that the submit button is not a type="submit". We are going to call a javascript function called validate first before we potentially send off bad info to the php page. the function looks like this

function validate() {

fm = document.thisForm

//use validation here to make sure the user entered
//the information correctly

fm.submit()

}


Notice that the form has an action="process.php". That means that there will be $_POST variables available to you in that php file. When the user hits the submit button and the javascript sends the form, the browser will re-direct to process.php

Here's what that page might look like

[PHP]<?php

echo $_POST['my_textbox'];
echo $_POST['my_dropdown'];

//The name of these post variables is the same as the name
//of the elements that were in the form

?>[/PHP]

Now, Let me know if your have grasped all that and if you respond (in otherwords if you have looked back on this thread which some people dont even do). Then perhaps Troy and I can help you with the database and dynamic form part

On a side note. If you would have used method="GET" in the form, you would use $_GET[] in the php

-B
Reply With Quote  
Join Date: Jul 2005
Posts: 11
Reputation: lawfour is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
lawfour lawfour is offline Offline
Newbie Poster

Re: php drop down menu to search multiple sql tables

  #5  
Jul 27th, 2005
Ok, I hope I got the code right so far, if it is, now I need to connect to database.

I created the process.php file with this info:

<?php

echo $_POST['my_textbox'];
echo $_POST['my_dropdown'];

//The name of these post variables is the same as the name
//of the elements that were in the form

?>


This is the search (live) htt://www.lawfour/esr/search.php

I added the JS right under the form post info.

<form name="thisForm" method="POST" action="process.php">


<SCRIPT LANGUAGE="javascript">

function validate() {

fm = document.thisForm

//use validation here to make sure the user entered
//the information correctly

fm.submit()

}

</SCRIPT>

<table width="30%" border="0" cellspacing="0" cellpadding="0">
<tr>


<td width="7%" class="verdana_login">Search:</td>
<td width="8%">
&nbsp;<input type="text" name="my_textbox" size="10">&nbsp;
</td>
<td width="12%">
<select name="my_dropdown">
<option>Airgas Tickets</option>
<option>Products</option>
<option >Pending Repairs</option>
<option selected>Common Equip.</option>
<option >Lab Equip.</option>
<option >Vendors</option>
<option >RTP's</option>

</select>
</td>

<td width="13%" align="center">&nbsp;<INPUT type=image name="btm_submit" src="http://www.lawfour.com/esr/images/go.gif" border="0" width="35" height="22" onclick="validate()"></td>



</table>
Reply With Quote  
Join Date: Jul 2004
Location: Arizona State University
Posts: 56
Reputation: bwest is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
bwest's Avatar
bwest bwest is offline Offline
Junior Poster in Training

Re: php drop down menu to search multiple sql tables

  #6  
Jul 27th, 2005
The javascript typically goes between the <head> tags at the top of the page.

Here is a basic db connection class

[PHP]<?
class MyOps {

/* Global Variables */

var $conn = "localhost";
var $user = "db_username";
var $pass = "password";
var $db = "database_name";


/*****************dbconnect()***************************
* dbconnect() will remotely connect to a database *
* with a set of specified arguments. *
* *
* @conn = IP Address of the remote/local database *
* @user = DB Username *
* @pass = DB Password *
* @db = DB Name *
* *
*******************************************************/

function dbconnect() {

$link = mysql_connect($this->conn, $this->user, $this->pass, $this->db) or die("Could not connect : " . mysql_error());

$db_select = mysql_select_db($this->db) or die("error");

return $link;

}

}

?>[/PHP]


Then you import this file and use it like this in other php files that connect to the database

[PHP]
include ("db_scripts/dbconnect.php");

$dbops = New MyOps;
$link = $dbops->dbconnect();[/PHP]

Your overall page will look something like this

==============================start page
<?

//database connection

//query table names

//make a string of table names like this
$list = "<option value='a'>a</option><option value='b'>b</option>";

?>

<html>

(javascript validation)

<form>
<select><?php echo $list; ?></select>
</form>

</html>
==============================end page
Reply With Quote  
Join Date: Jul 2005
Posts: 11
Reputation: lawfour is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
lawfour lawfour is offline Offline
Newbie Poster

Re: php drop down menu to search multiple sql tables

  #7  
Jul 28th, 2005
Having trouble connecting, not sure if i am setting up the code right. Below is what I have for search.php.

And I am not sure where this code goes, is it a separate page that give the results of the search?:

<html>

(javascript validation)

<form>
<select><?php echo $list; ?></select>
</form>

</html>

Also, when I do get the search engine working does that mean that I have to change my current index.html to index.php because I want to put the search on the top of my website at http://www.jci.tju.edu/~lwilliam/

With the current code I get this error:

Fatal error: Cannot instantiate non-existent class: myops in /hsphere/local/home/lwilliam/lawfour.com/esr/search.php on line 27





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SEARCH</title>
<SCRIPT LANGUAGE="javascript">

function validate() {

fm = document.thisForm

//use validation here to make sure the user entered
//the information correctly

fm.submit()

}

</SCRIPT>
</head>

<body>
<?

include ("http://www.lawfour.com/esr/dbconnect.php");

$dbops = New MyOps;
$link = $dbops->dbconnect();
$list = "<option value='CommonEquip'>Common Equip.</option><option value='CommonEquip'>Lab Equip.</option>
<option value='Company'>Vendor</option><option value='Equipment Info'>Pending Repairs</option>
<option value='Parts'>Products</option><option value='Parts'>Parts</option><option value='RTP'>RTP</option>
<option value='Ticket'>Airgas Tickets</option>";

?>

<form name="thisForm" method="POST" action="process.php">




<table width="30%" border="0" cellspacing="0" cellpadding="0">
<tr>


<td width="7%" class="verdana_login">Search:</td>
<td width="8%">
&nbsp;<input type="text" name="my_textbox" size="10">&nbsp;
</td>
<td width="12%">
<select name="my_dropdown">
<option>Airgas Tickets</option>
<option>Products</option>
<option >Pending Repairs</option>
<option selected>Common Equip.</option>
<option >Lab Equip.</option>
<option >Vendors</option>
<option >RTP's</option>

</select>
</td>

<td width="13%" align="center">&nbsp;<INPUT type=image name="btm_submit" src="http://www.lawfour.com/esr/images/go.gif" border="0" width="35" height="22" onclick="validate()"></td>



</table></form>
</body>
</html>
Reply With Quote  
Join Date: Jul 2005
Posts: 11
Reputation: lawfour is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
lawfour lawfour is offline Offline
Newbie Poster

Re: php drop down menu to search multiple sql tables

  #8  
Jul 28th, 2005
I did some editing and now all i get is what i enter in the search field. the process.php just displays what i typed into it.

www.lawfour.com/esr/search.php

Thanks for all you help I really appreciate it.

Here is the new CODE.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SEARCH</title>
<SCRIPT LANGUAGE="javascript">

function validate() {

fm = document.thisForm

//use validation here to make sure the user entered
//the information correctly

fm.submit()

}

</SCRIPT>
</head>

<body>

(javascript validation)


<form name="thisForm" method="POST" action="process.php">
<table width="30%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="7%" class="verdana_login">Search:</td>
<td width="8%">
&nbsp;<input type="text" name="my_textbox" size="10">&nbsp;
</td>
<td width="12%">
<form>

<?php echo $list; ?>

</form>
<select name="my_dropdown">
<?
include ("http://www.lawfour.com/esr/dbconnect.php");
$dbops ="New MyOps";
$link ="$dbops->dbconnect()";
$list =" <option value='CommonEquip'>Common Equip.</option><option value='CommonEquip'>Lab Equip.</option>
<option value='Company'>Vendor</option><option value='Equipment Info'>Pending Repairs</option>
<option value='Parts'>Products</option><option value='Parts'>Parts</option><option value='RTP'>RTP</option>
<option value='Ticket'>Airgas Tickets</option> ";
?>
<option>Airgas Tickets</option>
<option>Products</option>
<option >Pending Repairs</option>
<option selected>Common Equip.</option>
<option >Lab Equip.</option>
<option >Vendors</option>
<option >RTP's</option>

</select>


</td>

<td width="13%" align="center">&nbsp;<INPUT type=image name="btm_submit" src="http://www.lawfour.com/esr/images/go.gif" border="0" width="35" height="22" onclick="validate()"></td>



</table></form>
</body>
</html>
Last edited by lawfour : Jul 28th, 2005 at 11:41 am. Reason: add new code
Reply With Quote  
Join Date: Jun 2005
Location: Kansas City, Missouri, USA
Posts: 344
Reputation: Troy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: php drop down menu to search multiple sql tables

  #9  
Jul 28th, 2005
lawfour, bwest is not trying to write your app for you. Instead, he's trying to teach you good programming techniques as well as giving you very good code examples specific to your task.

I think you need to take a step back, and start with some of the basics bwest is trying to show you. Start with some very minimal code. Don't try to tackle 5 concepts at one time.

For example, create a simple HTML page with nothing but a very simple form with a single text element like so:
[HTML]
<html>
<head>
<title>My Page</title>
</head>
<body>
<form method="POST" action="process.php">
<input name="fave_color" type="text" />
<input type="submit" />
</form>
</body>
</html>
[/HTML]

Then create a PHP page named process.php like so:
[PHP]
<?php

$fave_color = $_POST['fave_color'];

?>

<html>
<head>
<title>Another Page</title>
</head>
<body>

<?= $fave_color ?>

</body>
</html>
[/PHP]
Once you've mastered how to receive form variables into a PHP script and work with it, you can move on to database connections.

bwest gave you very good code examples with comments. Although it's good code practice to use an include for a common database connection, it's not easy for a beginner to understand includes. It's easier to see all the code inline. Check out code example in this thread if you are having trouble getting connected.
http://www.daniweb.com/techtalkforums/thread25821.html

You don't need to post all your code--each time you have a problem, you should be able to narrow it down to a single line of code or at least a small subset of code.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote  
Join Date: Jul 2005
Posts: 11
Reputation: lawfour is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
lawfour lawfour is offline Offline
Newbie Poster

Re: php drop down menu to search multiple sql tables

  #10  
Jul 28th, 2005
OK,

Thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:10 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC