Print "You didn't enter a Search term" or somethin

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

Post Reply
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Print "You didn't enter a Search term" or somethin

Post by Maluendaster »

This is the Code, the problem is when someone doesn't enter a search text, it shows an error, what i want is when someone didn't enter a search term that the page print something like "You didn't enter a search term" :

Code: Select all

<?include("header.php");?>
	
<?

$query = $_GET['query'];
$tim = file("prodtable.db");
$sizecat = count($tim);
////////////////////////////////////
for($i=-1;$i<$sizecat;$i++){
$deti = explode("#",$tim[$i]);

if(stristr($deti[2],$query)){
echo "<table border='0' align='center' cellpadding='2' cellspacing='2'><tr><td bgcolor='#EEEEEE'><IMG SRC='prodimg/$deti[5]'> <BR><B>Nombre:</B> $deti[2] <BR><B>Precio:</B> $deti[3]$curency<BR> <a href='prodshow.php?id=$i'><b style='color:#000000; background-color:#AAAAAA'>[ detalles ]</b></a> </td><td bgcolor='#EEEEEE' width='100%'><B>About:</B> $deti[4] </td></tr></table>"; 
}
}

?>
	
	</TD>
</TR>
</TABLE>

<? include("footer.php"); ?>
THANK YOU
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$query = $_GET['query']; 

if (empty($query)) {
  echo 'Enter search term';
}
else {
   // do everything else 
}
empty(), you might want to validate the string if your inputting $query directly into the query. Search for SQL injection.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

if (isset($_GET['query']) && empty($_GET['query'])) {
  echo 'Enter search term';
}
else {
   // do everything else
}
This is where you need to figure out if they actually have even submitted the form with no data or they haven't submitted it at all. I'd use a combination of isset() and empty().
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

d11wtq wrote:

Code: Select all

if (isset($_GET['query']) && empty($_GET['query'])) {
  echo 'Enter search term';
}
else {
   // do everything else
}
This is where you need to figure out if they actually have even submitted the form with no data or they haven't submitted it at all. I'd use a combination of isset() and empty().
That's pointless because empty() checks for the existence of the variables just as isset() does, except it checks to make sure the variable is not empty as well.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Jcart wrote:
d11wtq wrote:

Code: Select all

if (isset($_GET['query']) && empty($_GET['query'])) {
  echo 'Enter search term';
}
else {
   // do everything else
}
This is where you need to figure out if they actually have even submitted the form with no data or they haven't submitted it at all. I'd use a combination of isset() and empty().
That's pointless because empty() checks for the existence of the variables just as isset() does, except it checks to make sure the variable is not empty as well.
Yes, think about it ;)

You open google.com for the first time. Now if it told you that you haven't entered a search query would that seem confusing? If you then proceeded to hit submit without entering any text and it said you've not entered a query qould it make sense?

The isset() checks that the user actually submitted the form, then the empty() checks if any data was sent by the user. Just using empty() doesn't allow you that knowledge since empty() matches:

array()
''
false
0
null
(unset)

:)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Yup your right, I'm used to using post and checking $_SERVER['REQUEST_METHOD'] .. stumped me on that one.. :oops:
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

thank you for everything! it worked both ways, but I used isset too...
Post Reply