Welcome Guest, Not a member yet? Register   Sign In
Redirecting after validate form data
#1

[eluser]alvaroeesti[/eluser]

Hello

I have been wrestling a full day with this issue.

I have my home page at

http://localhost/housing/index.php/home_C/index/

In there I have a search form. (home_view) which is loaded by the controller home_C

Then I have the Controller 'resultados_search_C.php' which should validate the data (although I am hearing the validation should be at the Model).

So, if the validation fails, then it should reload the form and display the errors. And this is where I have the problem. It reloads me the form, but if I look at the url it stays here:

http://localhost/housing/index.php/resul...lidateData

Even though I am telling it at the controller to go back to the home_view page (the frontpage)

Code:
if($this->form_validation->run() == FALSE)
  {
  
   $this->load->view('header');
   $this->load->view('home_view'); # this one is the central body of the page
   $this->load->view('footer.html');
  
    
  }

So the problems are multiple with staying there:

1) On the one side, the select lists which were populated dynamically from a DB are not.

2) Below and out of the form I also had some tables which were populated from the DB on loading the page. So now they don't display, but display the php errors of not finding data.


Question:

why is not going where I tell it to? of course, if I do a header ("location: http:// ..) yes it will go but no error messages are displayed and the user won't understand what went wrong.

I have other pages where I don't have that problem, for example at the login page. But the reason I don't have that problem is because the same controller that loads the form is the same controller that validates the data. And if you see in the case mentioned, I have 2 controllers, one that loads me the view, and another that validates me the data.

The reason I wanted to do that is to avoid a bottleneck. The site can get a lot of traffic. If I have thousands of people pulling the select lists and searching, being that at the frontpage I feared it would clog the controller. So that pretty much seems to be the reason, but I don't understand why it should if I am telling it anyways to reload the form, unless, the validation process by default reloads you the same page, because it assumes that the same controller that validated the data is the one that initially loaded the page. Any insight into this ?


Any idea?

best regards


Alvaro
#2

[eluser]CroNiX[/eluser]
I'm guessing it is from whatever code you have after when you run form_validation. Maybe you need an else there for if it DOES pass, because it will load those views and go to the next line and do whatever is there next if it doesn't...

Code:
if ($this->form_validation->run() == FALSE)
{
  //didn't pass, do this
}
else
{
  //did pass, do this
}
#3

[eluser]alvaroeesti[/eluser]


Hello

I do have the else. The problem comes when the validation does not pass. if the validation passes, all goes okay. See

Code:
if($this->form_validation->run() == FALSE)
  {
  
   $this->load->view('header');
   $this->load->view('home_view'); # this one is the central body of the page
   $this->load->view('footer.html');
  
    
  }
  
  else
  
  {
    
   $freetext = $this->input->post('freetext');
   $object = $this->input->post('object');

so the else, as you see is just filling the variables with values sent from the form to send them to the Model.
#4

[eluser]CroNiX[/eluser]
Well, don't make us keep guessing. Post all of your controller code, even if you don't think it's relevant. It's very hard to troubleshoot unless we can see all of the code...
#5

[eluser]alvaroeesti[/eluser]
ok sorry, here it goes. I am including all the relevant redirections and leaving out repeated lines that make no difference

Here is the controller that loads the first home page (home_view)

As you see it contacts the Model in order to populate the select lists...It does not have anything else, just 3 functions to fill the dropdown lists.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home_C extends CI_Controller {



function __construct()
{
  
  parent::__construct();
  $this->load->model('home_model');
  
}


public function index()
{
  $data['paises'] = $this->home_model->devolverPaises();
  $data['ofertas'] = $this->home_model->devolverOfertas();


the home_page, where the form starts, as you see it is in a form that sends the data to the validation controller:
Code:
<?php echo form_open(base_url() . 'index.php/resultados_search_C/validateData'); ?>

<ul>

<li>
  <select name ="object" id = "object">

          #more select lists down here ...

&lt;?php echo form_close(); ?&gt;




And the controller that checks the data
results_search_C.php controller which checks the data
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Resultados_search_C extends CI_Controller {



function __construct()
{
  
  parent::__construct();
    
  $this->load->model('resultados_search_M');
  
}





public function validateData()
{
  $this->form_validation->set_rules('freetext', 'freetext', 'trim|max_length[100]|xss_clean');
  $this->form_validation->set_rules('object', 'object', 'trim|max_length=[30]|xss_clean');

  if($this->form_validation->run() == FALSE)
  {
  
   $this->load->view('header');
   $this->load->view('home_view'); # this one is the central body of the page
   $this->load->view('footer.html');
  
    
  }
  
  else
  
  {
    
   $freetext = $this->input->post('freetext');

                # Now sending the data to the Model

              $data = $this->resultados_search_M->devolverResultados($freetext, $object,...etc)




Here is the model that is contacted by the resultados_search_C.php controller, the one that validates. It gets the validated data and makes a sql query and returns the results back to the controller which (when there have not been errors when filling the form) sends it to another view that loads correct data.

Code:
class Resultados_search_M extends CI_Model{



public function devolverResultados ($freetext, $object,
        {

$sql = $this->db->query("SELECT town FROM atributos where price < 150000");
  
  return $sql->result();

#6

[eluser]CroNiX[/eluser]
Your form is being submitted to a different method, validateData(). That method isn't loading your dropdown values... and it's a new request, so it doesn't know anything that was loaded in your Home_C controller from the previous request...

Load the dropdown values in validateData() and pass it to your home_view if the form_validation is FALSE.
#7

[eluser]alvaroeesti[/eluser]


Yes, that is. Yes, like I said, I don't have that problem in the login page for the backoffice because I only have one controller there as it just has to care to 2 input fields, and that controller both validates and loads initially the page.

So you mean, as a solution that I should do all the validation at the first controller home_C.php ? I wanted to avoid that because the frontpage was going to be very busy, see, there are 3 interdependent select boxes that load countries and regions and cities plus 6 more plus some input fields...

what do you feel it is better to do ?

best regards

and truly appreciate the time you have dedicated

Alvaro
#8

[eluser]CroNiX[/eluser]
You can do it how you want. All I'm saying is you aren't loading your dropdown values in your validateData() method, but your home_view that you load there when validation fails is obviously trying to use them. So, load and pass the dropdown data just like you are in Home_C/index (I'm guessing, because you didn't post all of your code) and it should work.
#9

[eluser]alvaroeesti[/eluser]

UPDATE:

You know what I am going to do?

If they enter wrong data, which the validate_data function will determine, I will do one thing: header("location:/go-back-to-where-you-tried-to-mess-around), that is, back to the form fully reset at the beginning.

The reason for this cold shower is that if the validate_data fails is because someone tried to upset my application entering wrong data. So why should I be that nice as to explain them where they failed? let them try for the rest of their lives.

Other option is that someone is retarded and does not know how to write numbers. If he is so retarded, what is he doing on my web ? My web is for economists and cultivated people, so they should not fail

============================
OLD POST

I have been wrestling around but it ain't that easy. The home_view is a quite complicated page with a lot of jquery needed to make the interdependent select lists work Country Region and City, this jquery has code like this:

Code:
var cd = $('#regiones').val();
$.get(path + 'home_C/ciudades', {'id_region':cd}, function(r
that home_C is a we see above, the first controller, resending that to yet another controller is impossible.

There has to be another way. I heard of people saying that the validation should be done at the Model.

Other option that I am seeing in webs is that if someone enters rubbish, it just ignores it and carries on with the data it has. For example you can indicate country region and town and select houses, if you enter 'wersdfsd' as price, it would just show all the prices instead of complaining, it ignores that 'specification'





Theme © iAndrew 2016 - Forum software by © MyBB