Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter PHP - loading a view at an anchor point

I have a form at the bottom of a long page, if a user fills out the form but it doesn't validate the page is reloaded in the typical codeigniter fashion:

$this->load->view('template',$data);

however because the form is way down at the bottom of the page I need the page to load down there like you do with HTML anchors. Does anyone know how to do this in codeigniter?

I can't use the codeigniter

redirect();

function because it loses the object and the validation errors are gone. Other frameworks I've used like Yii you can call the redirect function like:

$this->redirect();

which solves the problem because you keep the object. I've tried using:

$this->index()

within the controller which works fine as a redirect but the validation errors are in another method which is where the current page is loaded from:

$this->item($labs)

but when I use this it get stuck in a loop

Any ideas? I've seen this question a lot on the net but no clear answers. I'm researching using codeigniter "flash data" but think it's a bit overkill.

cheers.

like image 707
James Billings Avatar asked Sep 21 '12 15:09

James Billings


1 Answers

I can't personally vouch for this, but according to this thread if you append the anchor to the form's action, it will work.

CodeIgniter helper:

<?php echo form_open('controller/function#anchor'); ?>

Or vanilla HTML:

<form method='post' action='controller/function#anchor'>

If you were open to using Javascript, you could easily detect a $validation_failed variable and appropriately scroll. Or, even better, use AJAX.

Another option is to put the form near the top of the page?

like image 140
Jordan Arseno Avatar answered Oct 06 '22 01:10

Jordan Arseno