CodeIgniter Forums
Using anchors in form URLs - 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: Using anchors in form URLs (/showthread.php?tid=52562)



Using anchors in form URLs - El Forum - 06-15-2012

[eluser]Kenneth Vogt[/eluser]
It is a pretty common approach for a controller to both process a form and render the form. In other words, you will often see a controller that looks like this:

Code:
class Account extends CI_Controller
{
  function index()
  {
    $this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
    if ($this->form_validation->run() == TRUE)
    {
      redirect('home', 'refresh');
    }
    else
    {
      $this->load->view('account');
    }
  }
}

My form might be far down the account.php page however. So it would make sense for me to use an anchor tag so that I can reference the url as:

Code:
./account.php#my_form
However I don't see any way to do this with load->view(). How can I accomplish this?


Using anchors in form URLs - El Forum - 06-15-2012

[eluser]Kenneth Vogt[/eluser]
And the answer is you don't do it in the controller, you do it in the view. So instead of using:
Code:
form_open('account');
you would use:
Code:
form_open('account#my_form');



Using anchors in form URLs - El Forum - 06-15-2012

[eluser]InsiteFX[/eluser]
To name an internal anchor:
place this just above your form.
Code:
<a name="top-form"></a>

To link to an internal anchor:
place this all over in your html code where you want the user to click to get to your form
Example code:
Code:
<a href="#top-form">Go to Form</a>

This will create an internal link in your html code.



Using anchors in form URLs - El Forum - 06-16-2012

[eluser]Kenneth Vogt[/eluser]
While I can reference a page anchor with the form_open() method in the view script, that only allows for the use case where the page is being loaded after the posting of a form. If you need a more general case where you access it from the controller, you can use something like this:
Code:
redirect('account#settings', 'refresh');