Jump to content
Simongf

How to prevent submitting search form when search field is empty?

Recommended Posts

When I search item from database works fine,but How to prevent submitting search form when search field is empty? And get Fatal error : call to member function fetch_assoc() on string. Even if there is no item match in the database my function doesn’t return error message which I expect “Your search can not be found”.

<?php

include’lib/Session.php’>

Session::int();

Include’lib/Database.php’;

Include’helpers/Format.php’;

Spl_autoload_register(function ($class){

Include_once”Classes/“.$class.”.php”;});

$db = new Database();

$sr = new Search();

?>

<div class=”search”>

<form name=“form1” method=“post” action=“search.php”>

<input name=“search” type=“text” value=“Search for products”>

<input type=“submit” value=“Search”>

</form>

</div>

header.php

————————————————

<?php include’inc/header.php’;?>

<?php

If($_SERVER[‘REQUEST_METHOD’]== ‘POST’])

{ $search = $_POST[‘search’];

$getsr = $sr->search($search)

}

?>

<div class = “ section “>

<?php

If (isset ($getsr))

{

while($result = $getsr->fetch_assoc())

{

?>

<div class =“ grid “>

<a href=“details.php?proid=<?php echo $result[‘productId’];?><img src=“admin/<?php echo $result[‘image’];?>” alt=“”/></a>

<h2><?php echo $result[‘productName’];?></h2>

<p><?php echo $fm->textShorten($result[‘body’],60);?></p>

<p><?php echo $result[‘price’];?></p>

<div class=“button”><a href=“details.php?proid=<?php echo $tesult[‘productId’];?>”Details</a></div>

</div>

<?php } } ? >

</div>

search.php

———————————————————<?php

$filepath = realpath(dirname(FILE));

Include_once($filepath.’/…/lib/Database.php’);

Include_once($filepath.’/…/helpers/Format.php’);

?>

<?php

Class Search{

Private $db;

Private $fm;

Public function __construct()

{ $this->db = new Database();

$this->fm = new Format();

}

Public function search ($search)

{ $search = $this->fm->validation($search);

$search = mysql_real_escape_string($this->dB->link,$search);

If(empty($search))

{ $searchmsg = “Search field must not be empty”;

return $searchmsg;

}

else {

$query = “ SELECT * FROM tbl_product WHERE productName LIKE ‘% “.$_POST[‘search’]. “ %’ “;

$result = $this->db->select($query);

If ($result!= False){

return $result;

}

else{

$searchmsg = “Search result not found ”;

return $searchmsg;

}

}

}

}

?>

Search.php

 

 

Share this post


Link to post
Share on other sites

Checking $_POST['search'] before calling the search function is one method that springs to mind.

Share this post


Link to post
Share on other sites

Or JavaScript; check the value on submit - call an alert() can return false if it's empty, otherwise proceed as normal.

Edited by maxxd

Share this post


Link to post
Share on other sites

I would dispute your use of "or" in that last post.

Client side checks, javascript or setting the "required" attribute on the field, should be seen as additions for the convenience of the user. Validation should still be done on the server.

Share this post


Link to post
Share on other sites

Absolutely needs to be validated on the server side as well. My wording could have been better - thanks!

Share this post


Link to post
Share on other sites

Thanks for your reply , I’m trying to do what you suggested .my another question is if someone trys to fine some item which is not in the database ,I wrote a function that would respond “No item found” ,but it’s not happening .

Share this post


Link to post
Share on other sites

Difficult to follow the logic flow given the absence of indents, but you seem to be assuming the query will return false if no results are found.

Queries return false when they fail. Not finding results is not a failure (it may in fact be the desired result). Check the number of records returned.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.