Can't carry over a key parameter from one form to another

Bill Mudry <[email protected]>
Newsgroups gmane.comp.php.windows
Message-ID <[email protected]>
A few weeks ago I was able to get a simple search engine working
in my "TAXA" project and very glad for it. All the pages are dynamically
formed, so the search code has to be able to extract answers from
the 'species' table. At first, the search method was hard wired to 
provide answers
on a common name basis for different woods.

I realized that I should be able to expand this to allow users to first
choose by which column to do the search. I chose to list common names,
botanical names, genus, location and description. This form is working just
fine by itself.

The rest of the code for the search is still working great ---- when 
I hard wire
which column to use. However, I can't seem to get the user chosen parameter
($searchtype) or its corresponding actual column name ($searchfield) to carry
through once a user finally enters a search string and presses the 
submit button.
Mind you, I can see that any output parameters from the first form 
gets wiped out
every time the form cycles back to the same page.

I have tried adding a hidden field to preserve the $searchfield 
value. That didn't
work. I have tried adding a $_GET statement to bring back that value but that
did not work either. I just tried to change the second form action from
action=\"search.php\" to action="<?php echo $_SERVER['PHP_SELF']; ?>"
and that didn't help either.

What is the proper code to carry over $searchfield so it can be used 
by the second form? Because of numerous decode statements I put in and because
hard wiring a column name makes the search engine work just fine, I know that
once that value can be used by the second form, I will have a search function
that will allow users to search by numerous choices. That would be one more
giant leap for my project.

Perhaps one answer would be to merge both forms into one but I am hoping
I can keep then separate so a user doesn't have to choose the method of
search for multiple queries.

I always appreciate the fine help you have all supplied. It has meant 
the difference
between accomplishing my goals or not

Bill Mudry
Mississauga, Ontario

The running code can be seen and tried out at:
         http://www.prowebcanada.com/taxa/search.php

Here is the complete code for that page:
========================================================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!---
This page contains the major search functions for The TAXA Wood Knowledge Base.
It is called from the main menu.
Important parameters include:
         $searchtype - is the method users choose to do a search as 
the English words
     $searchfield - Each method translates into the actual field in 
the 'species' table
                            used to complete the query
         $querystring - is the actual query string submitted by the user.



--->

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TAXA: Search by Common Name</title>
<style type="text/css">
<!--
body {
         font: 100% Verdana, Arial, Helvetica, sans-serif; font-size: 12px;
         background: ivory;
         margin: 0; /* it's good practice to zero the margin and 
padding of the body element to account for differing browser defaults */
         padding: 0;
         text-align: center; /* this centers the container in IE 5* 
browsers. The text is then set to the left aligned default in the 
#container selector */
         color: #000000;
}

*/
.oneColElsCtrHdr #container {
         width: 146em;  /* this width will create a container that 
will fit in an 800px browser window if text is left at browser 
default font sizes */
         background: #FFFFFF;
         margin: 0 auto; /* the auto margins (in conjunction with a 
width) center the page */
         border: 1px solid #000000;
         text-align: left; /* this overrides the text-align: center 
on the body element. */
}
.oneColElsCtrHdr #header {
         background: #DDDDDD;
         padding: 0 10px 0 20px;  /* this padding matches the left 
alignment of the elements in the divs that appear beneath it. If an 
image is used in the #header instead of text, you may want to remove 
the padding. */
}
.oneColElsCtrHdr #header h1 {
         margin: 0; /* zeroing the margin of the last element in the 
#header div will avoid margin collapse - an unexplainable space 
between divs. If the div has a border around it, this is not 
necessary as that also avoids the margin collapse */
         padding: 10px 0; /* using padding instead of margin will 
allow you to keep the element away from the edges of the div */
}
.oneColElsCtrHdr #mainContent {
         padding: 0 20px; /* remember that padding is the space 
inside the div box and margin is the space outside the div box */
         background: #FFFFFF;
}
.oneColElsCtrHdr #footer {
         padding: 0 10px; /* this padding matches the left alignment 
of the elements in the divs that appear above it. */
         background:#DDDDDD;
}
.oneColElsCtrHdr #footer p {
         margin: 0; /* zeroing the margins of the first element in 
the footer will avoid the possibility of margin collapse - a space 
between divs */
         padding: 10px 0; /* padding on this element will create 
space, just as the the margin would have, without the margin collapse issue */
}
p.webnote {margin: 20; font: Verdana, Arial, Helvetica, sans-serif; 
font-size:small;
text-align: left;
};
.tabledata {font: Verdana, Arial, Helvetica, sans-serif; 
font-size:small; text-align: left;};


-->
</style>
<!---------------------------------------------------------------------------------------------------->
<!---------------------------------------  END OF STYLE LISTING 
-------------------------------------->
<!---------------------------------------------------------------------------------------------------->


<meta name="search_engine_author" content="Steve R, 
http://www.designplace.org/">

</head>

<!---
  ------------------------------------  START OF THE BODY  -----------------
  --->


<body >

<hr />


<p class="webnote">
This section is under development and will not work well until it is 
finished. It will
expand the number of choices by which a person can do a search in the 
species table
(such as by species, common name, origin and so on).
</p>

<!---
  This section allows users to choose by what field in the species 
file to search from
--->
<hr />
     <h2 align="center">Search TAXA</h2>


Choose first by which means you wish to search.<br />


<FORM name="choose_search" id="choosesearch" method="GET" 
action="<?php echo $_SERVER['PHP_SELF']; ?>" >

<TABLE align="center" border=0 bgcolor='ivory' cellpadding=10>
     <TR>
         <TD class="tabledata">
             <B>Common names</B>
             <Input type='radio' value='common names' name="searchtype" />
         </TD>
         <TD class="tabledata">
             <B>Botanical names</B>
             <Input type='radio' value='botanical names' name="searchtype" />
         </TD>
         <TD class="tabledata">
             <B>Genus</B>
             <Input type='radio' value='genus' name="searchtype" />
         </TD>

         <TD class="tabledata">
             <B>Location</B>
             <Input type='radio' value='location' name="searchtype" />
         </TD>
         <TD class="tabledata">
             <B>Description</B>
             <Input type='radio' value='description' name="searchtype" />
         </TD>
     </TR>
     <TR>
         <TD align="center">
       <?PHP
         echo "<input type='hidden' name='species_name' 
value='{$species_name}' />";
       ?>
             <Input type='submit' value='Submit Search Method' />
         </TD>
     </TR>
</TABLE>
</FORM>

<!--- ---------------------------------------- End of 
form  ------------------------------------- --->

<?php
   // Get the search variable from URL

   $searchtype = @$_GET['searchtype'] ;
   $searchtype = trim($searchtype); //trim whitespace from the stored variable

If (isset($searchfield)) {
                 echo "Searching by - $searchtype";
         };
//echo "Line 149 - \$searchtype is - $searchtype<br />";

$searchfield = "";
switch ($searchtype) {
         case "common names":
                 $searchfield = "species_commonname";
                 //Echo "<br>common names <br>\n";
                 break;

         case "botanical names":
                 $searchfield = "species_name";
                 //Echo "<br>botanical names - $searchfield 
<br>\n";
                 break;
         case "genus":
                 $searchfield = "genus_name";
                 //Echo "<br>genus - $searchfield <br>\n";
                 break;
         case "location":
                 $searchfield = "location";
                 //Echo "<br>location - $searchfield <br>\n";
                 break;
         case "description":
                 $searchfield = "species_description";
                 //Echo "<br>description - $searchfield 
<br>\n";
                 break;
         default: If (!isset($searchfield)) {
                 echo "Please choose a search type first";
         }

}

//echo "Line 180 - \$searchtype is - $searchtype<br />";

//Echo "Chosen search field (\$searchfield) is $searchfield <br /><br />";

echo "<h3 align='center'>Searching by $searchtype</h3>";



  echo "</div>";
  echo  "<div id='mainContent'>";
echo "Line 190 just before the query form - \$searchtype is - 
$searchtype<br />";
  ?>


<form name=\"form\" action="<?php echo $_SERVER['PHP_SELF']; ?>" 
method=\"get\">
<?php
//echo "<form name=\"form\" action=\"search.php\" method=\"get\">";
  echo "<input type=\"text\" name=\"querystring\" />";
// echo  "<input type='hidden' name=\"species_name\" 
value=\"{$species_name}\" />";
// echo  "<input type=\"hidden\" name=\"search_type\" 
value=\"$searchtype\" />";
// echo  "<input type=\"hidden\" name=\"search_field\" 
value=\"{$searchfield}\" />";
  echo  "<input type=\"submit\" name=\"Submit\" value=\"Search\" />";
echo "</form>";
echo "Line 199 just after the query form - \$searchtype is - 
$searchtype<br />";
  echo "</div>";


$searchtype = @$_GET['searchtype'] ;

$searchtype = trim($searchtype);


$searchfield = @$_GET['searchfield'] ;

$searchfield = trim($searchfield);


$querystring = @$_GET['querystring'] ;

$querystring = trim($querystring);

/*
Development debug statement:
*/
Echo "Line 209 - \$searchfield chosen by user is - $searchfield <br />";


// rows to return
$limit='100';

// check for an empty string and display a message.


if ($querystring == "")
   {
   echo "<p>Please enter a search...</p>";
   exit;
   }


// check for a search parameter
if (!isset($querystring))
   {
   echo "<p>We do not seem to have anything to search for!</p>";
   exit;
   }

//Echo "Line 212 - \$searchfield chosen by user is - $searchfield <br />";


///////////////////////////////////////////////////////////////////
//                   CONNECT TO THE DATABASE
///////////////////////////////////////////////////////////////////

include ("connecttotaxa.php");
$connection = mysql_connect($hostname, $username, $password)
         or die("Unable to connect to database server");

$db = mysql_select_db($dbname, $connection)
         or die("Unable to connect to database");

         // Building optional search fields. Pegged to species common 
names at first

//Echo "Line 253 - \$searchfield chosen by user is - $searchfield <br />";

/*
Debug method: hard wire the searchfield at first to only common name 
searches to allow
*/
//$searchfield="species_commonname";
//Echo "Line 253 - \$searchfield is hard set temporarily to 
$searchfield<br />";
Echo "Line 260 - \$searchfield is chosen as $searchfield<br />";
Echo "Line 261 - \$querystring is user entered as $querystring<br />";

// Build SQL Query
$query = "select * from species where $searchfield like \"%$querystring%\"
   order by $searchfield";

  $numresults=mysql_query($query);
  Echo "On line 268 \$numresults is - $numresults";
  $numrows=mysql_num_rows($numresults);

if ($numrows == 0)
   {
   echo "<h3>Search Results by $searchtype</h3>";
  echo "<p>Sorry, your search: &quot;" .  $querystring . "&quot; 
returned zero results</p>";

  //echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; 
returned zero results</p>";
   }

// next determine if s has been passed to script, if not use 0
   if (empty($s)) {
   $s=0;
   }

// get results
   $query .= " limit $s,$limit";
   $result = mysql_query($query) or die("Couldn't execute query");
  echo "<hr>";

// display what the person searched for
echo "<p>You searched for: &quot;" . $querystring . "&quot;</p>";

// begin to show results set
$count = 1 + $s ;
Echo "<div align = 'center'>";

///////////////////////////////////////////////////////////////////////////
//    Format the data with a double table and display the data
///////////////////////////////////////////////////////////////////////////

echo "<table border=3 bgcolor='EECCAA' width=820>";
echo "<tr>";
echo "<td cellpadding='10'>";

echo "<table border=0 bgcolor='white' width=800>";
echo "<tr>";
echo "<td colspan=4 cellpadding=5 bgcolor=#F0E8E0 align=center>";
echo "<h3 align='center'>Results by Common Name</h3>"; //The data title
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td cellspan=1 cellpadding=5 bgcolor=#FFFFFF class=>";
echo "<h4 align=center>#</h4>";
echo "</td>";
echo "<td cellspan=1 cellpadding=5 bgcolor=#FFFFFF>";
echo "<h4 align=center>Common Name</h4>";
echo "</td>";
echo "<td cellspan=0 cellpadding=5 bgcolor=#FFFFFF>";
echo "<h4 align=center>Botanical Name</h4>";
echo "</td>";
echo "<td cellspan=0 cellpadding=5 bgcolor=#FFFFFF>";
echo "<h4 align=center>Location</h4>";
echo "</td>";
echo "</tr>";

echo "<div id='resultsdata' class='tabledata'>";

// now you can display the results returned
   while ($row= mysql_fetch_array($result))
   {
         extract($row); //At this point, the search data is ready for display
         //echo $row;

echo "<tr>";

   $title1 = $row["species_commonname"] ;

   $title2 = $row["species_name"];

   $title3 = $row["location"];

echo "<td>";
echo "$count";
echo "</td>";

echo "<td nowrap>";
echo "$title1";
echo "</td>";

echo "<td nowrap>";
echo '<a href="displayspecies.php?&species_name='.$species_name.' ">';
echo "$title2";
echo"</a>.<br>\n";

echo "</td>";

echo "<td>";
echo "$title3";
echo "</td>";

echo "</tr>";


//  echo "$count.&nbsp;$title<br />" ;

   $count++ ;
   }

echo "</table>";

echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";

$currPage = (($s/$limit) + 1);
Echo "</div>";
//break before paging
   echo "<br />";

   // next we need to do the links to other results
   if ($s>=1) { // bypass PREV link if s is 0
   $prevs=($s-$limit);
   print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
   Prev 10</a>&nbsp&nbsp;";
   }

// calculate number of pages needing links
   $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a 
remainder from division

   if ($numrows%$limit) {
   // has remainder so add one page
   $pages++;
   }

// check to see if last page
   if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

   // not last page so give NEXT link
   $news=$s+$limit;

   echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
   }

$a = $s + ($limit) ;
   if ($a > $numrows) { $a = $numrows ; }
   $b = $s + 1 ;
   echo "<p>Showing results $b to $a of $numrows</p>";

Echo "<hr>";
$searchtype = "";
$querystring = "";

?>

   </div>
   </div>
</body>
</html>

========================================================
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.