Jump to content

[SOLVED] Search form validation !!


nita

Recommended Posts

Hi.

I would like to do my search form (used to search through my movie collection) a bit more user friendly.

 

I don't know how to do to:

return info that input must be longer then 2 (then back to previous page after lets say 2-3 sec) when search input is no longer the 2 letters / numbers. (min 2)

 

and do do similar think once there is no results at all. (you will find "if" for this in the script).

 


<form method='post' action='$php_self'>
<input type='text' name='search' size=25 maxlength=25><br>
<input type='Submit' name='Submit' value='Submit'>
</form>

$search=$_POST["search"];

$result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' 
OR cast LIKE '%$search%' OR director LIKE '%$search%' 
OR year LIKE '%$search%' OR production LIKE '%$search%'
OR genere LIKE '%$search%' OR language LIKE '%$search%'");

$numrows=mysql_num_rows($result);

if ($numrows == 0)
  	{
  	echo "<h4>No results found !!!</h4>";
  	echo "<p>Sorry, your search returned 0 results</p>";
  	}
else 
{ 
while($row=mysql_fetch_array($result))
{	
   	include "display_rec.php";
}
}

 

Hoping for some solution. Maybe there is some other recommendations too. 

Thank you in advance !!

Nita

 

Link to comment
Share on other sites

THx for suggestion sw0o0sh.

I'm afraid is not working. sure there is no bug there?

And then still i would like to do something redirecting automaticlly after 2 sec. back to previous page

 

i will look up on trim strlen !!

 

Thanks. Nita

 

Link to comment
Share on other sites

what about to make some change in mysql ?

i mean

ft min word len  4  4 

ft max word len  254  254 

ft max word len for sort  20  20 

 

are these on responsible for min / max number of characters??

 

is set on 4 which doesn't make much sense couse im able to get results with just 1 char.

i want this limit to be min 2.

 

 

any advice ... please

nita, thx

 

Link to comment
Share on other sites

i had change part of the code a bit but still not working?

 

 

$minchar = 1;
if (strlen($_POST['search']) <= $minchar)
{
echo "Search must be longer than 2 characters.";
} 
else 
{
while($row=mysql_fetch_array($result))
{	
   	include "display_rec.php";
}
}

 

what's wrong

 

can someone help, please

 

nita

Link to comment
Share on other sites

Hi

Entire code once again to make things clear !

 

<form method='post' action='$php_self'>
<input type='text' name='search' size=25 maxlength=25 minleneght=2><br>
<input type='Submit' name='Submit' value='Submit'>
</form>
</td>

<td>";
$search=$_POST["search"];

$result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' 
OR cast LIKE '%$search%' OR director LIKE '%$search%' 
OR year LIKE '%$search%' OR production LIKE '%$search%'
OR genere LIKE '%$search%' OR language LIKE '%$search%'");

$minchar = 2;
if (strlen($search) < $minchar)
{
echo "Search must be longer than 2 characters.";
} 
else 
{
while($row=mysql_fetch_array($result))
{	
   	include "display_rec.php";
}
}

$numrows=mysql_num_rows($result);
if ($numrows == 0)
  	{
  	echo "<h4></h4>";
  	echo "<p>Sorry, your search returned 0 results</p>";
  	}
else 
{ 
while($row=mysql_fetch_array($result))
{	
   	include "display_rec.php";
}
}

 

So there is a bug somewhere, when i do search with 1 char, it doesn't display warning communicate.

 

help, please

 

Nita

Link to comment
Share on other sites

It seems silly to do the query before you check the search variable is the correct length ???

 

try

<?php 
if (isset($_POST['search'])) {
$search=$_POST["search"];

$minchar = 2;
if (strlen($search) < $minchar)
{
    echo "Search must be longer than 2 characters.";
} 
else 
{
    $result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' 
    OR cast LIKE '%$search%' OR director LIKE '%$search%' 
    OR year LIKE '%$search%' OR production LIKE '%$search%'
    OR genere LIKE '%$search%' OR language LIKE '%$search%'");

    $numrows=mysql_num_rows($result);
    if ($numrows == 0)
  	    {
  	        echo "<p>Sorry, your search returned 0 results</p>";
  	    }
    else 
    { 
        while($row=mysql_fetch_array($result))
        {	
       	        include "display_rec.php";
        }
    }
}
}
?>
<form method='post' action=''>
<input type='text' name='search' size=25 maxlength=25 ><br>
<input type='Submit' name='Submit' value='Submit'>
</form>

Link to comment
Share on other sites

Thanks a lot Barand.

 

Now is working perfectly.

 

I still have question left without solution. How to code automatic redirection after error messages back to previous page. I can manage to do link, but i rather like automatic think more.

 

<META HTTP-EQUIV='REFRESH' CONTENT='1; URL=$PHP_SELF'>  also maybe an option, but it's not to comfortable considering entire application.

 

Is any one can think on some other solution!?

 

Thanks.

Nita.

 

 

 

Link to comment
Share on other sites

One way is with a location header, but you cannot use it if output has already been sent (eg error message)

if (strlen($search) < $minchar)
{
            header("location: prevPage.php?errmsg=Search must be longer than 2 characters");
            exit;
} 

 

Another way is the meta refresh as you have there, displaying the message briefly.

 

Another is redisplay the search form in the same page if there is an error, which would be similar to my code.

Link to comment
Share on other sites

HI, i guess the solution are fine, but i have my header already sent, so i get an error back.

i think i would like to stick to meta too

 

i came up with code: (is working but still is not doing what it meant to do)

-search part of the code-

 

else if(isset($_POST['submit'])||isset($_POST['search'])) // when search button is pressed
{
echo "
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
<LINK REL='StyleSheet' href='movies.css' TYPE='text/css'>
<title>Nita Movies</title>
</head>
<body>
<table width='950'>
<tr>
<td bgcolor='#663300' width='180' border='0' cellspacing='0' cellpadding='0'  valign='top' class='font2' >";
$result = mysql_query("SELECT * FROM category ORDER BY cat") or die(mysql_error());
while($row = mysql_fetch_array( $result )) 
{

echo "<A href='index.php?cat=$row[cat]'>$row[cat]</a><br>";
}
echo "	
<br>
<form method='post' action='$php_self'>
<input type='text' name='search' size=25 maxlength=25 minleneght=2><br>
<input type='Submit' name='Submit' value='Submit'>
</form>
</td>

<td>";
$search=$_POST["search"];

$minchar = 2;
if (strlen($search) < $minchar)
{
    echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
			'http://www.w3.org/TR/html4/loose.dtd'>
			<html>
			<head>
			<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
			<META HTTP-EQUIV='REFRESH' CONTENT='2; URL=$PHP_SELF'>
			<LINK REL='StyleSheet' href='movies.css' TYPE='text/css'>
			<title>Nita Movies</title>
			</head>
			<body>";

	echo "Search must be longer than 2 characters.
	</body>
	</html>
";
} 
else 
{
    $result = mysql_query("SELECT * FROM movies WHERE name LIKE '%$search%' 
    OR cast LIKE '%$search%' OR director LIKE '%$search%' 
    OR year LIKE '%$search%' OR production LIKE '%$search%'
    OR genere LIKE '%$search%' OR language LIKE '%$search%'");

    $numrows=mysql_num_rows($result);
    if ($numrows == 0)
  	    {
  	          echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
			'http://www.w3.org/TR/html4/loose.dtd'>
			<html>
			<head>
			<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
			<META HTTP-EQUIV='REFRESH' CONTENT='2; URL=$PHP_SELF'>
			<LINK REL='StyleSheet' href='movies.css' TYPE='text/css'>
			<title>Nita Movies</title>
			</head>
			<body>";
		echo "<p>Sorry, your search returned 0 results</p>
		</body>
		</html>
";

  	    }
    else 
    { 
        while($row=mysql_fetch_array($result))
        {	
       	        include "display_rec.php";
        }
    }
}
	echo "
</td>
</tr>
</table>
</body>
</html>
";

}

 

im not sure if the meta info is all in the right place?

some help please.

 

Nita,

Link to comment
Share on other sites

OK, so the code above is redirecting automatically after 2 sec.

But is redirecting to index.php (mean root of the application)

i want it to redirect back to the page from which i started to search from.

 

any ideas what to do with this part of the code to achive what i want ??

<META HTTP-EQUIV='REFRESH' CONTENT='2; URL=$PHP_SELF'>

 

chech the script in action at

www.nita-on-line.com/movies/

 

nita thanks a lot in advance.

 

 

 

Link to comment
Share on other sites

Thx Barand

 

will be honest, im not sure where should i check if register_globals are off or on.

Where can i check that.

 

i added line you suggested ... still same result. is redirecting back to the root.

is it a right place to add this line ??

 

<td class='font2'>";

$PHP_SELF = $_SERVER['PHP_SELF']; // additional line !!!!!!!!!!

$search=$_POST["search"];

$minchar = 2;
if (strlen($search) < $minchar)
{
    echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
			'http://www.w3.org/TR/html4/loose.dtd'>
			<html>
			<head>
			<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
			<META HTTP-EQUIV='REFRESH' CONTENT='2; URL=$PHP_SELF'>
			<LINK REL='StyleSheet' href='movies.css' TYPE='text/css'>
			<title>Nita Movies</title>
			</head>
			<body>";

	echo "Search must be longer than 2 characters.
	</body>
	</html>
";
} 

 

looking forward to further advice.

 

Thanks.

 

nita.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.