Jump to content

Search Form with Dropdown Category and Textbox


pruoccojr

Recommended Posts

MySQL Database Version 5.0

 

Hello, 

 

I currently have a search form for my database with 4 input fields and 4 submit buttons which display results for Number, Customer, Contact, and Keyword:

// Make connection to Database
	require("globals.php"); //this has all our configuration options.
	require_login(); //function called to make sure the user is logged in.
	$db = new DB(); //set up a new database connection
	$has_results = false;
	if(isset($_POST['submit'])) {
		//form posted.
		$where = array();
		$form = array();
		foreach($_POST as $key=>$value) {
			$form[$key] = $db->clean_input($value);
		}
		if(isset($form['bynumber']) && $form['bynumber']!="") {
			$where[] = " JobNumber = ".$form['bynumber'];
		}
		if(isset($form['bycustomer']) && $form['bycustomer']!="") {
			$where[] = "Customer like '%".$form['bycustomer']."%' ";
		}
		if(isset($form['bycontact']) && $form['bycontact']!="") {
			$where[] = "Contact like '%".$form['bycontact']."%' ";
		} 
		if(isset($form['bykeyword']) && $form['bykeyword']!="") {
			$where[] = "JobInstruct like '%".$form['bykeyword']."%' ";
		}
		$sql = "SELECT * FROM OrderInfo";
		if(count($where)>0) {
			$sql .= " WHERE ".implode(" OR ",$where);
		}
		$sql .= " ORDER BY JobNumber DESC";
		$rs = $db->query($sql);
		
		if($db->num_rows($rs)>0) {
			$has_results = true;
		}
	}
	include(INCLUDE_PATH."/header.php"); //global header file
<form autocomplete="off" action="search.php" method="post">
	<h2>Number:</h2>
	<input type="text" class="search-field" name="bynumber" value="<?=@$form['bynumber']?>">
	<input type="submit" name="submit" class="submitbutton" value=" ">
	
	<h2>Customer:</h2>
	<input type="text" class="search-field" name="bycustomer" value="<?=@$form['bycustomer']?>">
	<input type="submit" name="submit" class="submitbutton" style="margin:0;" value=" ">
	<div style="clear:both; padding-bottom:18px;"></div>

	<h2>Contact:</h2>
	<input type="text" class="search-field" name="bycontact" value="<?=@$form['bycontact']?>">
	<input type="submit" name="submit" class="submitbutton" value=" ">
	
	<h2>Keyword:</h2>
	<input type="text" class="search-field" name="bykeyword" value="<?=@$form['bykeyword']?>">
	<input type="submit" name="submit" class="submitbutton" style="margin:0;" value=" ">
	<div style="clear:both; padding-bottom:18px;"></div>
</form>

What I would like to do is minimize the form by having 1 dropdown menu with the options being Number, Customer, Contact, and Keyword, and 1 textbox which would search for words within the chosen option. My issue is that this code (which is only part of a larger project) was written by a freelancer that we can no longer get in contact with and I don't really know much about writing PHP. 

 

I'm not asking anybody to do the work for me so I can take it and run, but I would be very grateful if somebody can walk me through the process so that I can learn for myself - assuming it's something simple enough to teach. 

 

Any help at all is greatly appreciated. Thank you

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.