PHP Search Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

PHP Search Form

Post by fionaom87 »

Hey i am trying to do a php search form and this error is appearing
Notice: Undefined variable: searching in C:\wamp\www\search.php on line 18
Also it does not seem to realise action="<?=$PHP_SELF?>"> as it does not seem to be coming back to the page again.
Thanks
F :D

Code: Select all

 
<h2>Search</h2> 
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="fname">First Name</option>
<Option VALUE="lname">Last Name</option>
<Option VALUE="info">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
 
 
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form 
if ($searching =="yes") //line 18
{ 
echo "<h2>Results</h2><p>"; 
 
//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 
 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 
 
while($result = mysql_fetch_array( $data )) 
{ 
echo $result['name']; 
echo " "; 
echo $result['address']; 
echo "<br>"; 
echo $result['telephone']; 
echo "<br>"; 
echo "<br>"; 
} 
 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 
 
//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?> 
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: PHP Search Form

Post by mattpointblank »

You have to use $_POST['name_of_form_field'] to access form variables.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: PHP Search Form

Post by fionaom87 »

This is my code now but still doesnt seem to work :banghead:


Code: Select all

 
 
<html>
<body>
<h2>Search</h2> 
<form name="search" method="post" action="<?PHP=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
 
 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form 
 
$name=$_POST['name']; 
$address=$_POST['address']; 
$telephoneno=$_POST['telephoneno']; 
 
 
if ($searching =="yes") 
{ 
echo "<h2>Results</h2><p>"; 
 
//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 
 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 
 
while($result = mysql_fetch_array( $data )) 
{ 
echo $result['name']; 
echo " "; 
echo $result['address']; 
echo "<br>"; 
echo $result['telephoneno']; 
echo "<br>"; 
echo "<br>"; 
} 
 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 
 
//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?> 
</body>
</html>
 
 
 
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: PHP Search Form

Post by mattpointblank »

I mean for this line:

if ($searching =="yes")

You're not defining $searching, so you need to do:

$searching = $_POST['searching'];

before.

Conventionally though, rather than testing for a hidden form field, you should test to see if the submit button was pressed, eg:

Code: Select all

 
if(isset($_POST['search'])) {
 
// do search stuff here
 
}
 
This uses the isset() function, which tests if a variable exists. In this case it's testing that the $_POST array (which contains anything posted to the script from a form) has an element called 'search' (the name of your submit button). If it's been set, it'll do stuff. Make sense?
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: PHP Search Form

Post by fionaom87 »

sorry im slightly confused.
i put in that search variable but when i click it just comes up webpage cannot be found.

Code: Select all

 
 
<html>
<body>
<h2>Search</h2> 
<form name="search" method="post" action="<?PHP=$PHP_SELF?>">
Seach for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
 
 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form 
 
 
 
if(isset($_POST['search'])) 
{ 
echo "<h2>Results</h2><p>"; 
 
//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 
 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 
 
while($result = mysql_fetch_array( $data )) 
{ 
echo $result['name']; 
echo " "; 
echo $result['address']; 
echo "<br>"; 
echo $result['telephoneno']; 
echo "<br>"; 
echo "<br>"; 
} 
 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 
 
//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?> 
</body>
</html>
 
 
 
 
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: PHP Search Form

Post by mattpointblank »

Change

action="<?PHP=$PHP_SELF?>">

to

action="<?php echo $_SERVER['PHP_SELF']; ?>">

you have to actually output/echo these values to make them display.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: PHP Search Form

Post by fionaom87 »

i am now just getting

"You forgot to enter a search term"
even though i have entered a search term.
upper($field)--- must i change this to a field in my database?

Thanks
F :D

Code: Select all

 
 
<html>
<body>
<h2>Search</h2> 
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> 
Seach for: <input type="text" name="find" /> in 
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
 
 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
//This is only displayed if they have submitted the form 
 
 
 
if(isset($_POST['search'])) 
{ 
echo "<h2>Results</h2><p>"; 
 
//If they did not enter a search term we give them an error 
if ($find == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 
 
$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find); 
 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'"); 
 
while($result = mysql_fetch_array( $data )) 
{ 
echo $result['name']; 
echo " "; 
echo $result['address']; 
echo "<br>"; 
echo $result['telephoneno']; 
echo "<br>"; 
echo "<br>"; 
} 
 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 
 
//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find; 
} 
?> 
</body>
</html>
 
 
p3rk5
Forum Commoner
Posts: 34
Joined: Thu Jan 29, 2009 10:19 pm

Re: PHP Search Form

Post by p3rk5 »

You did not define the $find variable before checking if it existed:

Code: Select all

<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
 
 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
$find = strtoupper(strip_tags(trim($find)));
if(isset($_POST['search']))
{
echo "<h2>Results</h2><p>";
 
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
 
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
 
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
 
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
watson516
Forum Contributor
Posts: 198
Joined: Mon Mar 20, 2006 9:19 pm
Location: Hamilton, Ontario

Re: PHP Search Form

Post by watson516 »

$_POST['find']

I might be mistaken but you have to either specify $_POST, $_GET, or $_REQUEST to access the form field variables. Accessing them using just $find I believe has to be set up on the server and is probably discouraged by most people.
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: PHP Search Form

Post by fionaom87 »

hey still doesnt seem to be working for me. When i enter a search term example Fiona for FirstName.... its just comes up you have You forgot to enter a search term
any help would be great :banghead:
F.

Code: Select all

 
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
 
 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
$find = strtoupper(strip_tags(trim($find)));
if(isset($_POST['search']))
{
echo "<h2>Results</h2><p>";
 
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
 
while($result = mysql_fetch_array( $data ))
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
 
$anymatches=mysql_num_rows($data);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
 
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
 
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: PHP Search Form

Post by susrisha »

Code: Select all

 
if(isset($_POST['search']))
{
 
$find= $_POST['find'];
//then your code please
 
 
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: PHP Search Form

Post by fionaom87 »

following errors occured

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\search.php on line 36

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\search.php on line 47

Code: Select all

 
<html>
<body>
<h2>Search</h2>
<form name="search" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Seach for: <input type="text" name="find" /> in
<Select NAME="field">
<Option VALUE="name">First Name</option>
<Option VALUE="address">Address</option>
<Option VALUE="telephoneno">Profile</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
 
 
 
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php include 'config.php'; ?><!-- DB connection -->
<?php
$find = strtoupper(strip_tags(trim($find)));
if(isset($_POST['search']))
{
$find= $_POST['find'];
 
echo "<h2>Results</h2><p>";
 
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
 
$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
 
while($result = mysql_fetch_array( $data )) //line 36
{
echo $result['name'];
echo " ";
echo $result['address'];
echo "<br>";
echo $result['telephoneno'];
echo "<br>";
echo "<br>";
}
 
$anymatches=mysql_num_rows($data);
if ($anymatches == 0) //line 47
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
 
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
</body>
</html>
 
fionaom87
Forum Commoner
Posts: 43
Joined: Mon Feb 02, 2009 10:44 am

Re: PHP Search Form

Post by fionaom87 »

Unfortunatley seem to be still getting same prob any advice would be great!
:banghead: :banghead: :banghead:
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: PHP Search Form

Post by jayshields »

Change this:

Code: Select all

$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'");
to:

Code: Select all

$data = mysql_query("SELECT * FROM customers WHERE upper($field) LIKE'%$find%'") or die(mysql_error());
This is simple MySQL debugging, and has been discussed many times before.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Re: PHP Search Form

Post by Citizen »

var_dump(get_defined_vars()); and mysql_query($sql) or die(mysql_error() . $sql) are your bread and butter for learning the ropes. If you're unsure of the content of your queries or the result of our out put, use them.
Post Reply