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.)

Recommended Answers

All 14 Replies

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.

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

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

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

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

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

?>

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

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>

The javascript typically goes between the <head> tags at the top of the page.

Here is a basic db connection class

<?
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;

  }

}

?>

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

include ("db_scripts/dbconnect.php");

$dbops = New MyOps;
$link = $dbops->dbconnect();

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

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>

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>

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

Then create a PHP page named process.php like so:

<?php

$fave_color = $_POST['fave_color'];

?>

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

<?= $fave_color ?>

</body>
</html>

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. :)

OK,

Thanks

There really is almost all the code here in this thread that you would need. You just will need to implement your html and do the query. Besides that, the stuff above will be good for most of your project. As far as your connection goes and that fatal error. You might want to make sure you have properly set up the database. Sometimes the connection isnt "localhost" but is something else so check with your host. There could be about two dozen reasons why you cant connect but once you do just come back to this form because there's lots of good code here.

Good luck

Im having problem with my php and connect to multiple database. I use MySQL. I have index.php, process.php, search.php and dbconnect.php..When I try to run it just do nothing. just 'page error'..I don't know where the mistakes come from..

-----------------------------------------------------
I don't know exactly the common@basic page for process.php, and dbconnect.php..Help me..

Can You evaluate my project 'theses online' coding?

index.php


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>


<body>
<form name="thisForm" method="POST" action="process.php">
<table width="1015" border="0">
<tr>
<td colspan="4"><div align="center"><img src="img/banner1.jpg" width="732" height="102" /></div></td>
</tr>
<tr>
<td width="100"> <b>Home </b> </td>
<td width="357">&nbsp;</td>
<td width="293">&nbsp;</td>
<td width="247">&nbsp;</td>
</tr>
<tr>
<td> <b>Admin</b></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><b>About Us</b> </td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><div align="right">
<label>Words:
<input type="text" name="words" size = "30"/>
</label>
</div></td>
<td><div align="right">
<label>Search Type
<select name="search_type" size="1">
<option value="1" selected="selected">Penyelia</option>
<option value="2">Nama Pelajar</option>
<option value="3">Program</option>
</select>
</label>
</div></td>
<td><label> </label>


<div align="justify">
<input type="button" name="search_type" value="Search" onclick = "validate()"/>
</div></td>
</tr>
</table>
</form>
</body>
</html>


-----------------------------------------------------process.php


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
</head>


<body>
<?php


echo_$POST;
echo_$POST;


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


?>



</body>
</html>
------------------------------------------------dbconnect.php


<?php
class MyOps {


/* Global Variables */


var $conn = "localhost";
var $user = "root";
var $pass = "";
var $db = "tesis";



/*****************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;


}


}


?>


---------------------------------------------------


search.php


<?php


//database connection
include ("db_scripts/dbconnect.php");


$dbops = New MyOps;
$link = $dbops->dbconnect();


//query table name


mysql_select_db("tesis", $link);
$result = mysql_query("SELECT pelajar.penyelia, pelajar.nama, projek.tajuk_projek FROM pelajar, projek
WHERE pelajar.no_matriks= projek.no_matrik");
while($row = mysql_fetch_array($result))


{
echo $row . " " . $row;
echo "<br />";
}


//make a string of table names like this


<select name="search_type">


$list = "<option value='1'>Penyelia</option><option value='2'>Nama Pelajar</option>
<option value='3'>Program</option>";


<option>Penyelia</option>
<option>Nama Pelajar</option>
<option >Program</option>


</select>
?>


<html>
<head>
<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="1015" border="0">
<tr>
<td colspan="4"><div align="center"><img src="img/banner1.jpg" width="732" height="102" /></div>
</td>
</tr>
<tr>
<td width="100"> <b>Home </b> </td>
<td width="357">&nbsp;</td>
<td width="293">&nbsp;</td>
<td width="247">&nbsp;</td>
</tr>
<tr>
<td> <b>Admin</b></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><b>About Us</b> </td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><div align="right">
<label>Words:
<input type="text" name="words" size = "30"/>
</label>
</div></td>
<td><div align="right">
<label>Search Type
<select name="search_type" size="1">
<option value="1" selected="selected">Penyelia</option>
<option value="2">Nama Pelajar</option>
<option value="3">Program</option>
</select>
</label>
</div></td>
<td><label> </label>


<div align="justify">
<input type="button" name="search_type" value="Search" onclick = "validate()"/>
</div></td>
</tr>
</table>


</form>


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


</body>
</html>

Sorry im just learn php and i still have much trouble in understanding coding..For search.php file, i need your advice..What i mean is i want for column scan term it will show "penyelia", "nama pelajar", and "tajuk_projek" and for hit count column i want it count for how many "tajuk_projek" that available in database. My system will look like library online..When we search book, there are list available and hit count too.where should i put coding for hit counter??

this happen when i submit search button

Parse error: parse error in c:\phpdev\www\projecttheses\process.php on line 11

the mistakes in here:echo_$POST;

Im sorry this is my coding for search.php

<?php


//database connection
include ("db_scripts/dbconnect.php");


$dbops = New MyOps;
$link = $dbops->dbconnect();


//query table name


mysql_select_db("tesis", $link);
$result = mysql_query("SELECT pelajar.penyelia, pelajar.nama, projek.tajuk_projek FROM pelajar, projek
WHERE pelajar.no_matriks= projek.no_matrik");
while($row = mysql_fetch_array($result))


echo "<table border='1'>
<tr>
<th>Hit Count</th>
<th>Scan Term</th>
</tr>";while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row . "</td>";
echo "<td>" . $row . "</td>";
echo "</tr>";
}
echo "</table>";


//make a string of table names like this


<select name="search_type">


$list = "<option value='1'>Penyelia</option><option value='2'>Nama Pelajar</option>
<option value='3'>Program</option>";


<option>Penyelia</option>
<option>Nama Pelajar</option>
<option >Program</option>


</select>
?>


<html>
<head>
<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="1015" border="0">
<tr>
<td colspan="4"><div align="center"><img src="img/banner1.jpg" width="732" height="102" /></div>
</td>
</tr>
<tr>
<td width="100"> <b>Home </b> </td>
<td width="357">&nbsp;</td>
<td width="293">&nbsp;</td>
<td width="247">&nbsp;</td>
</tr>
<tr>
<td> <b>Admin</b></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><b>About Us</b> </td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><div align="right">
<label>Words:
<input type="text" name="words" size = "30"/>
</label>
</div></td>
<td><div align="right">
<label>Search Type
<select name="search_type" size="1">
<option value="1" selected="selected">Penyelia</option>
<option value="2">Nama Pelajar</option>
<option value="3">Program</option>
</select>
</label>
</div></td>
<td><label> </label>


<div align="justify">
<input type="button" name="search_type" value="Search" onclick = "validate()"/>
</div></td>
</tr>
</table>


</form>


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


</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.