Click to See Complete Forum and Search --> : HTML Form - PHP Help


wolfkat
02-24-2003, 06:04 PM
I have got my PHP page to display info from a database (YAY!!!) and am impressed with the intuitive features I have found so far in PHP. I then tried to get a seach page together (simple one, just asking for a book ID or Name) and have the action.php file return the right information. But when I press the Submit button on my form, none of my $variables contain the search criteria entered in the form. I have included both the form and the php file code so if anyone more experienced and with less bloodshot eyes than me wants to take a crack at it, I would appreciate it. Again, thanks from a PHP/MYsql/General Linux newbie.

(PS, I am using a PHP/MySql book for help, but am still failing)


Ok, here is my form page:

<Html>
<Head>
<Title> RANW Member Search
</title>
</head>
<body>

<h1> Book Listing Search Page </h1>

<form action="action.php" method="post">
<select name="searchtype">
<option value="id">ID
<option value="name">Name
</select>
<br>
Enter Search Term:<br>
<input name="searchterm" type=text>
<br>
<input type=submit value="Search">
</form>


</body>
</html>

And here is my action.php page:

<Html>
<Head>
<Title = "Test">
</title>
</head>
<body>
<?php

trim($searchterm);
print $searchterm."<br>";
if(!$searchtype || !$searchterm)
{
echo "You Have Not Entered a Search Item. Please click BACK and try again";

exit;
}

$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);

$db = mysql_pconnect("localhost", "userid", "somepass") or die("Not Connected: ".mysql_error());

mysql_select_db("IDX");

$query = "select * from Books where ".$searchtype." like '%".$searchterm."%'";

print $query;

$result = mysql_query($query) or die("Invalid Query: ".mysql_error());

$num_results = mysql_num_rows($result);

echo "<p> Number of Books Found: ".$num_results."</p>";

for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<p><strong>".($i+1).". Title: ";
echo htmlspecialchars( stripslashes($row["name"]));
echo "</strong><br>ID: ";
echo htmlspecialchars( stripslashes($row["id"]));
echo "</p>";
}
?>
PHP index page
</body>
</html>

mrBen
02-25-2003, 06:03 AM
Try naming your select options:


<form action="action.php" method="post">
<select name="searchtype">
<option value="id" name="id">ID
<option value="name" name="name">Name
</select>

And see if it makes a difference.

(Also you should probably add </option> tags)

theN
02-25-2003, 06:50 AM
Hi

Are you working with PHP's REGISTER_GLOBALS=on?.

If you aren't, action.php won't acknowledge the existence of "searchtype" or "searchterm".

regards
theN

wolfkat
02-25-2003, 10:38 AM
That did it. Thanks for the info. I appreciate the help.

theN
02-26-2003, 03:46 AM
Hi

Glad you got it working :). But, what worked?

the 'name's for FORM elements or REGISTER_GLOBALS=on?

PHP manual advises REGISTER_GLOBALS be set to OFF. Read the manual for the rationale.

regards
theN
ps: a little more detail, than a terse "That did it", would be helpful. It will not only bring closure, but also help the next guy asking the same questions.

wolfkat
02-26-2003, 10:25 AM
the Global Registers = off was the problem. Now I have to figure out the best way to get the scripts to work with them off. :-)

iDxMan
02-26-2003, 09:11 PM
Use the new superglobals.

ie:

$searchtype will be $_REQUEST['searchtype']

(also look at $_GET $_POST and $_SESSION)

-r