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!
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
<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;
}
?>
<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>
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?
<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>
<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>
<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>
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.
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
F.
<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>
<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>
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.