-1

I have a page in view that has two parts actually which are accessed through # tags, like login#signin and login#signup. When the page loads for the first time it shows login form without having #signin without a problem.

So signin is not causing a problem as it loads at folder/login. But when I try to put folder/login#signup to load directly signup part it gives an error that there is no view login#signup.php. How to cope with this situation?

$this->load->view('workers/login#signup'); is not working.

When I don't put #signup it loads login form that is weird.

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
  • @Naim Malek if( $this->form_validation->run() == FALSE ) { $this->load->view('workerswelcome/login'); } and here instead of "/login" alone I need "/login#signup" which is not working – Saud Ashfaq Oct 30 '17 at 11:33
  • @Naim Malek thanks a lot it worked perfectly thanks once again. – Saud Ashfaq Oct 30 '17 at 11:42
  • @NaimMalek but in this case the validation errors are not showing up, is there a way that I can directly load view like $this->load->view('workers/login#signup') or if still redirecting how to show validation errors as well. am using the same you said in your answer and tried without refresh as well with no luck. – Saud Ashfaq Oct 30 '17 at 11:50
  • it's giving the error Unable to load the requested file: workers/login#signup.php – Saud Ashfaq Oct 30 '17 at 12:04
  • You cannot use `view('workers/login#signup')` (with the hash tag). The `view()` method will load a view file, and that filename does not include the `#signup` in it. _"When I don't put #signup it loads login form that is weird."_ — there is nothing weird about this, you need to read the [documentation](http://www.codeigniter.com/user_guide/general/views.html) again. – Kirk Beard Oct 30 '17 at 12:21
  • If you want to load a URL with a `#signup` at the end, you need to use the [`redirect('your-url-goes-here/#signup')`](http://www.codeigniter.com/user_guide/helpers/url_helper.html#redirect) function to load the appropriate URL. – Kirk Beard Oct 30 '17 at 12:26
  • @KirkBeard it's weird in a sense that user should view registration page again at the moment when they make some mistake while filling the and submitting the registration page. I got your point. But how would it load validation_errors(); it doesn't show these if I use redirect. Should I use $this->session->set_flashdata() to store the errors and then show using session again at the right place? – Saud Ashfaq Oct 30 '17 at 12:51
  • 1
    If you set the validation error to session data you will not lose any validation errors. You have to understand that a "#" in URL is client-side feature. It is not a part of the URL that gets processed by the server. That's why you cannot add this to the "load->view" part as well as the URL behind that function is handled differently. (As it should). You can use redirect, just remember to set the validation errors to sesssion data, use redirect and voila. – Martin Oct 30 '17 at 14:06
  • Just as Martin said, store the errors in the session and use `redirect()`, or you can reload the page on errors and set a variable to enable JavaScript to scroll down to the `#signup` part of the page. – Kirk Beard Oct 30 '17 at 22:39
  • @SaudAshfaq I've posted an answer with more details on the cause of the error, and how to handle it properly. If you need further clarification, please let me know. – Kirk Beard Oct 30 '17 at 23:53

1 Answers1

0

I'll expand more on my initial comments for the cause of this error, and how to fix things.

The cause of the issue

As mentioned throughout the comments, you cannot a view using an anchor point. For example, this does not work:

view('workers/login#signup'); // The #signup should not be here.

The documentation states:

Loading a View

To load a particular view file you will use the following method:

$this->load->view('name');

Where name is the name of your view file.

The name is the file is "name", not "name#signup".

Further down,

The .php file extension does not need to be specified unless you use something other than .php.

This implies, that when you use view('name'), CodeIgniter will, by default, load the file name.php. If you include a #signup in it, then CodeIgniter will not be able to find name#signup.php because that file does not exist.

Correct way to handle things

You mentioned you're using the form validation, so we need to ensure no value is lost during the transition process.

Here's a simplified explanation for how to handle it:

function login() {

    // Data to be passed to the view (you may or may not already have this)
    // More info: https://codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view
    $data = array();


    // Validation has failed...
    $this->form_validation->run() == FALSE ) {

        // Set variable to redirect to #signup upon page load
        $data['redirect_to_signup'] = true;
    }


    // Load view with $data which contains values to be passed to the view
    $this->load->view('workers/login', $data);

}

In your workers/login view file, we just need to check if the redirect_to_signup value exists. If it does exist, then we can use some simple JavaScript to scroll down the #signup form:

<?php if (isset($redirect_to_signup) && $redirect_to_signup === true): ?>
<script>
    var top = document.getElementById('signup').offsetTop;
    window.scrollTo(0, top);
</script>
<?php endif; ?>

Because your validation object is still valid, you can use the built-in CodeIgniter functions to preload your form elements with the set_value() helper functions. For example:

<input type="text" name="email" value="<?php echo set_value('email'); ?>">

That hopefully explains how to achieve what you're after:

  1. Validate user submitted form; and
  2. If there are errors, reload the form with validation messages; and
  3. Scroll down to the #signup form on the page.

One alternative is using redirect('login#signup'), but I would not recommend this method. You would need to save your form values and validation errors to the session to show them on the next page. You also run into the issue that the user might click the refresh button and all values would be lost then.

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47