• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search Criteria Ignored, Only Redirects to Else Statement

#11
(06-13-2019, 01:17 PM)jreklund Wrote: You haven't provided us with any code for your view (where the search submit button are), so I can't tell you why it ain't working. If you would be so kind to post it and a form that's working.

If a type="submit" dosen't have a name="" field it will not show up. If you provided it with name="submit" it will populate $this->input->post('submit') with whatever your value="" are.

eg.
Code:
<input type="submit" name="submit" value="Submit!" />

Will give you: Submit!

Code:
<input type="submit" value="Submit!" />

Will give you: null (not a string, just null)

And therefor return false in an if statement. So I guess you don't have a name="" on that particular button, but everything else.

You need to close your your parenthesis.
Code:
if ($this->input->method() === 'post')

Ok, let us look at that then. We were focused solely on the Controller and had not mentioned anything about the searchform's View until now. So, pardon my confusion...

Here are the bits of code you asked for:

Modified Search Function from Recipe Controller:
PHP Code:
function search(){
 
  if($this->input->method() === 'post')
 
   $search $this->input->post('query');
 
  $results $this->recipe_model->search($search);
 
     $data = array(
 
     'title' => "Shirley's Recipes : Search Results",
 
     'columns' => array('toc''recipe_search'),
 
     'recipes' => $results,
 
  );
 
  $this->load->view('includes/template'$data);
 
  }
 
  else{
 
   redirect('recipe');
 
  }
 } 

Searchfom View PHP Code:
PHP Code:
<div id="searchform" class="box noprint">
 
   <?php
     
echo form_open('recipe/search', array('class' => 'form'));
 
    echo form_input(array('name' =>'query','onfocus' => "this.value = '';"'onblur' => "if(this.value == ''){this.value ='Recipe Search'}"'value' => 'Recipe Search'));
 
    echo form_submit('submit''''class="button"');
 
    echo form_close();
 
   ?>
   </div> 

Searchform Browser Output for the Form:
Code:
<div id="searchform" class="box noprint">
<form action="http://shirleysrecipes.100webspace.net/recipe/search" class="form" method="post" accept-charset="utf-8">
 <input type="text" name="query" value="Recipe Search" onfocus="this.value = '';" onblur="if(this.value == ''){this.value ='Recipe Search'}">
 <input type="submit" name="" value="submit" class="button">
</form>
</div>

Now, for a working form:

Login.php Controller:
PHP Code:
<?php
class Login extends MY_Controller{

 function 
__construct() {
 
 parent::__construct();
 
  $this->session->keep_flashdata('uri');
 }
 
 
 
function index(){
 
 $data = array(
 
    'title' => "Shirley's Recipes : Admin Login",
 
  'columns' => array('toc''login_form')
 
 );
 
 $this->load->view('includes/template'$data);
 }
 
 
 
function validate(){
 
 $this->load->model('user_model');
 
 $username $this->input->post('username');
 
 $password $this->input->post('password');
 
 
  $query 
$this->user_model->validate($username$password);
 
  if($query){
 
   $data = array(
 
     'username' => $username,
 
    'logged_in' => true,
 
           'tz' => $this->user_model->timezone($username)
 
  );
 
  $this->session->set_userdata($data);
 
  $uri $this->session->flashdata('uri');
 
  if(isset($uri) && $uri !== FALSE){
 
   redirect($uri);
 
  }
 
  else{
 
   redirect('/');
 
  }
 
 }
 
 else{
 
  redirect('login');
 
 }
 }
 
 function 
logout(){
 
 $this->session->unset_userdata('logged_in');
 
 $uri $this->session->flashdata('uri');
 
 if(isset($uri) && $uri !== FALSE){
 
  redirect($uri);
 
 }
 
 else{
 
  redirect('/');
 
 }
 }
}
// End - login.php
?>

Login View:
Code:
<div id="login" class="content">
<h1>Admin Login</h1>
 ***ATTENTION***:You are attempting to access a secure area of this site. If you are not an administrator, please click on any of the navigation bars above and to the left to exit this area.
 <p>
 <div id="login_form">
  <form action="/login/validate" method="post">
   <input type="text" name="username" onfocus="this.value = '';" onblur="if(this.value == ''){this.value ='User Name'}" value="User Name"/>
   <input type="password" name="password" onfocus="this.value = '';" onblur="if(this.value == ''){this.value ='Password'}" value="Password"/>
   <input type="submit" name="submit" value="Login"/>
  </form>
 </div>
</div>

Does that help shed any light on things? I realize a login would be a far more complex form then a search but it is just one example.

Thank you again, sorry for any lack of information or clarity on my part!
Reply

#12
We needed to see what you got into your controller, to see where the problem where in the first place. As you didn't get any information from it, we can fix the controller or the view.

Your searchform output are this:
<input type="submit" name="" value="submit" class="button">

Login form:
<input type="submit" name="submit" value="Login"/>

As you can see the name="" field are empty, and that's why your $this->input->post('post') never validated.

If you want your old code, use this instead in your view.
echo form_submit('submit', 'submit', 'class="button"');

___________________EDIT______________________

Hold on a second... I can't even see how you got the following:
<input type="submit" name="" value="submit" class="button">

With your provided code you should have:
<input type="submit" name="submit" value="" class="button">

But none the less, your field are empty. And an empty value can't be matches as true in an if statement. You can reverse it with ! in case you want an empty button.

___________________EDIT2______________________

Or match it with an empty string. if($this->input->post('submit') === '') {
Reply

#13
(06-13-2019, 02:16 PM)jreklund Wrote: We needed to see what you got into your controller, to see where the problem where in the first place. As you didn't get any information from it, we can fix the controller or the view.

Your searchform output are this:
<input type="submit" name="" value="submit" class="button">

Login form:
<input type="submit" name="submit" value="Login"/>

As you can see the name="" field are empty, and that's why your $this->input->post('post') never validated.

If you want your old code, use this instead in your view.
echo form_submit('submit', 'submit', 'class="button"');

___________________EDIT______________________

Hold on a second... I can't even see how you got the following:
<input type="submit" name="" value="submit" class="button">

With your provided code you should have:
<input type="submit" name="submit" value="" class="button">

But none the less, your field are empty. And an empty value can't be matches as true in an if statement. You can reverse it with ! in case you want an empty button.

___________________EDIT2______________________

Or match it with an empty string. if($this->input->post('submit') === '') {

Okay, so I added those changes but I still get a Parse Error.

Recipe.php Search Controller:
PHP Code:
function search(){
 
  if($this->input->method() === 'post')
 
   $search $this->input->post('query');
 
  $results $this->recipe_model->search($search);
 
     $data = array(
 
     'title' => "Shirley's Recipes : Search Results",
 
     'columns' => array('toc''recipe_search'),
 
     'recipes' => $results,
 
  );
 
  $this->load->view('includes/template'$data);
 
  }
 
  else{
 
   redirect('recipe');
 
  }
 } 

Recipe_model.php Search Model:
PHP Code:
function search($search){
 
 $terms explode(" "$search);
 
 $match "";
 
 foreach($terms as $term){
 
  $match .=  $term;
 
 }
 
 $querystr "SELECT *, MATCH(name, linkname, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('".$match."') as score FROM recipe WHERE MATCH(name, linkname, ingredients, equipment, preparation, directions, category, subcategory, keywords) AGAINST('".$match."') ORDER BY score DESC;";
 
 $q $this->db->query($querystr);
 
 return $q->result();
 } 

Recipe.php View:
Code:
  <div id="searchform" class="box noprint">
   <?php
    echo form_open('recipe/search', array('class' => 'form'));
    echo form_input(array('name' =>'query','onfocus' => "this.value = '';", 'onblur' => "if(this.value == ''){this.value ='Recipe Search'}", 'value' => 'Recipe Search'));
    echo form_submit('submit', 'submit', 'class="button"');
    echo form_close();
   ?>
 </div>  

Recipe_search.php View
Code:
<div id="recipelist" class="content">
<h1>Recipe Search</h1>
 <div class="recipesearch">
  <ol>
   <?php if(!empty($recipes)): foreach($recipes as $recipe): ?>
    <li><a href="/recipe/<?=$recipe->id ?>"><?=$recipe->name ?></a><span class="confidence"><?=$recipe->score ?></span></li>
    <?php endforeach; ?>
  </ol>
 </div>
 <?php else: ?>
 <h3>No matching recipes</h3>
 <?php endif; ?>
</div>

Parse Error:
PHP Code:
An uncaught Exception was encountered

Type
ParseError

Message
syntax errorunexpected 'else' (T_ELSE), expecting function (T_FUNCTION) or const (T_CONST)

Filename: /home/www/shirleysrecipes.100webspace.net/application/controllers/Recipe.php

Line Number
62

Backtrace
:

File: /home/www/shirleysrecipes.100webspace.net/index.php
Line
315
Function: require_once 

Am I still missing something? Thank you for your dedication!
Reply

#14
Hi,

It looks like the code in controller recipe.php is missing the opening curly brace for the if statement.
A good decision is based on knowledge and not on numbers. - Plato

Reply

#15
(06-13-2019, 11:15 PM)salain Wrote: Hi,

It looks like the code in controller recipe.php is missing the opening curly brace for the if statement.

Thank you for catching that! It now loads the recipe results page. BUT, no matter what I search for it just outputs no results found.... It used to almost always find SOMETHING. Any thoughts?
Reply

#16
Hi,

I am not familiar with MATCH but I can see in the model search method you split (explode) the search string on spaces then concatenate it with no space.
So my guess is you end up with a search string like this word1word2word3etc...
A good decision is based on knowledge and not on numbers. - Plato

Reply

#17
I would appreciate any further feedback on this issue for anyone who understands MATCH so I can get this search feature back up and running again. Thank you all again for all your help and suggestions.
Reply

#18
I would also check your database and make sure that it was not changed from FULLTEXT search
IE;
FULLTEXT(title,description)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.