CodeIgniter Forums
Validation and internal anchors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Validation and internal anchors (/showthread.php?tid=12706)



Validation and internal anchors - El Forum - 10-28-2008

[eluser]Joe_Archer[/eluser]
Hi, I have a view which contains multiple forms, each being processed by a different method in my controller. Basically what i'd like to know is: is there any way to add an internal anchor (#div-id) onto the URL when validation fails.

Currently when a validation error occurs, the user is redirected back to the form as expected, only the form in question, isn't at the top of the page, so the user has to scroll to the correct form to see what the individual errors were. I'd like to load the page directly at the div containing the form that the user submitted.

I can't think of any way to do it.

and yes I know having a big page of forms is poor interface design, but I have to do as my clients ask! Smile


Validation and internal anchors - El Forum - 10-28-2008

[eluser]Sumon[/eluser]
Code:
is it helpful for you
function add_item()
{
  if ($_POST)
  {
    if ($this->form_validation->run('portion_of_validation') == FALSE)
    {
        $data['msg'] = $this->form_validation->error_string();
        $data['default'] = $_POST;
                $this->load->view('view_page.php#div-id',$data);
    }
  }
  else
  {
       $this->load->view('view_page');
  }
}



Validation and internal anchors - El Forum - 10-28-2008

[eluser]Joe_Archer[/eluser]
This doesn't work for me.

If i do
Code:
$this->load->view('page_view.php#form1');

i get:

Unable to load the requested file: page_view.php#form1


Validation and internal anchors - El Forum - 10-28-2008

[eluser]Sumon[/eluser]
what about if you
use
Code:
redirect('add_item/#div-id);
instead of
Code:
$this->load->view('view_page.php#div-id',$data);
Hope this time it will work for you.


Validation and internal anchors - El Forum - 10-28-2008

[eluser]Joe_Archer[/eluser]
That doesn't work either, as I can then no longer use the
Code:
set_value('field_name')
function to maintain the form data.


Validation and internal anchors - El Forum - 10-28-2008

[eluser]Colin Williams[/eluser]
Just have your form actions contain the proper fragment identifier. And when the form goes well, you can redirect to a page w/o the fragment identifier.

Code:
<form action="<?= site_url('controller/function') ?>#div-id" method="post">



Validation and internal anchors - El Forum - 10-28-2008

[eluser]Joe_Archer[/eluser]
brilliant! I can't believe I didn't think of that.

thank you so much.


Validation and internal anchors - El Forum - 10-29-2008

[eluser]drewbee[/eluser]
Joe,

that was exactly what I was going to suggest. You just point the form to the anchor in the action attribute!

You can't do the previous because the load method is for loading the specified files, it really could care less about what exists where and how the page is layed out.