Welcome Guest, Not a member yet? Register   Sign In
Form validation with redirect
#12

(10-27-2015, 03:30 AM)joseph.dicdican Wrote: Hi,

I would like to share my opinion.

From the thread, I found this block of statement.

Quote:$this->data['body_id'] = 'home';
$this->data['main'] = 'pages/home_view#anchor';    
$this->load->view('templates/template_main_view', $this->data);
The second line causes the error you had. (trying to reach a page .../home#anchor.php which obviously doesn't eist)

I supposed you had a code in templates/template_main_view.php like this

PHP Code:
<!--With some html elements -->
<?
php $this->load->view($main); ?>
$main is not a link it is most likely a string that is referring to a specific file under the View folder. I suggest you can remove the #anchor from your controller method home to remove the error.

Now what I understood, you want to autoscroll to the specific part of your html document and show the errors. 

Quote:To retain your goal to have autoscroll, redirect method will help you. But consequently, you will lose the validation messages since, the page is reloaded.
You can use flashdata for this matter. Storing all the validation messages to flashdata and printing it to your view will solve the problem.

I have searched from the internet and found some helpful links here. 
https://www.codeigniter.com/user_guide/l...#flashdata
http://stackoverflow.com/questions/12353...flash-data

Please refer to my example,

In the Controller,

PHP Code:
public function home($any '') {
 
   $this->session->set_flashdata('error''')
 
   $this->load->view('samples/test');
}

public function 
send() {
 
   $this->load->library('form_validation');

 
   if($this->form_validation->run()) {
 
       //do your stuff here
 
   } else {
 
       $std = new stdClass(); // just and example (to show that flash_data can hold complex values)
 
       $std->error 'some error'
 
       
        
// you can get the validation messages from form_validation
 
       // $std = validation_errors(); //just uncomment this part, this should work
 
       
        $this
->session->set_flashdata('error'$std);
 
   }

 
   redirect(base_url('index.php/sample/home#anchor'));


In View,

Code:
// form above

// The viewport should autoscroll in this part if url has #anchor (any id)
<section id="anchor">
  <?php echo $this->session->flashdata('error')->error; ?>
</section>

I hope I help you somehow. I will make some research on this matter and revise this post if ever I find new approach..

Thank you. Have a nice day.

- joseph.dicdican  Big Grin

Thanks for that. I have used your flashdata solution successfully. The only issue is that the flashdata variable is passed as a string and I want it to be an array so that I can show each error individually. Fortunately this is easily done using a separator parameter of '/n'. Of course I can't use the codeigniter function form_errors() but that isn't a problem. Although this works fine, it may still not be the best solution I suspect.
Reply


Messages In This Thread
Form validation with redirect - by msheath - 10-26-2015, 04:42 AM
RE: Form validation with redirect - by Martin7483 - 10-26-2015, 05:34 AM
RE: Form validation with redirect - by msheath - 10-26-2015, 07:26 AM
RE: Form validation with redirect - by Martin7483 - 10-26-2015, 07:44 AM
RE: Form validation with redirect - by msheath - 10-26-2015, 08:09 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:39 AM
RE: Form validation with redirect - by Narf - 10-27-2015, 03:42 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:54 AM
RE: Form validation with redirect - by Narf - 10-28-2015, 04:21 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 04:50 AM
RE: Form validation with redirect - by Martin7483 - 10-27-2015, 12:16 PM
RE: Form validation with redirect - by msheath - 10-28-2015, 02:15 AM
RE: Form validation with redirect - by mwhitney - 10-27-2015, 12:29 PM
RE: Form validation with redirect - by Martin7483 - 10-28-2015, 02:41 AM
RE: Form validation with redirect - by Martin7483 - 10-28-2015, 05:00 AM
RE: Form validation with redirect - by msheath - 10-28-2015, 05:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB