Welcome Guest, Not a member yet? Register   Sign In
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


Messages In This Thread
RE: Search Criteria Ignored, Only Redirects to Else Statement - by Josh1985 - 06-13-2019, 01:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB