0

i need a simple if else check whether any data is inserted to search or not.

So i set the condition ----

if the input data is less than 1 than it will show that the user should input some data to search. otherwise it will search the result.

But somehow this simple thing is not working for me.

Do anyone knows how to solve this problem. Thanks in advanced.

$query = $_POST['Search']; $min_length = 1; if (isset($query) && !empty($query)) { if (strlen($query) > $min_length){ // Check whether if there is atleast 1 character to chearch // method to search ..... echo($result); } else { echo "Sorry you have to put some data to search"; } 

this one always return the if statement it never go to the else statement.

i have also tried like so ---

 $query = $_POST['Search']; $min_length = 1; if (isset($query) && ! empty($query) ) { if(isset($query) > $min_length){ // method to search ..... echo($result); } else { echo "Sorry you have to put some data to search"; } 

But this one always return the else statement.

Can anyone knows how to fix this problem.

6
  • explain not working - the 2nd code block problem is obvious (isset return truw or false, so its never greater than 1), but what happens when you try the 1st code block Commented Aug 17, 2015 at 9:29
  • isset($query) && !empty($query) && strlen($query) > $min_length - can be done with !empty($query) only. Commented Aug 17, 2015 at 9:31
  • Is query actually retrieving a value from $_POST['name']? Check with var_dump($query); Commented Aug 17, 2015 at 9:33
  • @b0s3 i have tried "!empty($query)" but it is searching when there is no data as well. Commented Aug 17, 2015 at 9:34
  • What? That can't happen. If $query is empty then it will never get inside the block with !empty($query) if there are no mistakes. Commented Aug 17, 2015 at 9:37

2 Answers 2

4

There are some issues with the checks. First, check whether the POST actually contains a field 'Search':

$query = isset($_POST['Search']) ? $_POST['Search'] : ""; 

(edit: changed the field name from name to Search)

then, just check for it's length:

$min_length = 1; if (strlen($query) >= $min_length) { // do search here // and echo the result } else { echo "Sorry you have to put some data to search"; } 
Sign up to request clarification or add additional context in comments.

Comments

1

You missed on } and here is working code for you:

$query = $_POST['name']; $min_length = 1; if (isset($query)) { if (strlen($query) > $min_length) { // Check whether if there is atleast 1 character to chearch // method to search ..... echo "result"; } else { echo "Sorry you have to put some data to search"; } }else { echo "name not set."; } 

2 Comments

No idea, i expect this is the correct answer. Though it could be condensed into one check if(isset($_POST['name']) && strlen($_POST['name']) > $min_length){//query}else{//error}
with this code you will still get an undefined index: name error generated when $_POST['name'] does not exist. Its better to do the isset on the $_POST array value before trying to move it to $query. Hence the downvote. Others should reconsider their upvote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.