Welcome Guest, Not a member yet? Register   Sign In
Loding a view file and redirecting to anchor
#1

[eluser]tintamarre[/eluser]
Hello,

I am trying to load a view file which I do correctly in my controler using :

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


but then I would like the browser to go to the anchor #contact on the form home_view.php meaning to index.php/home/#contact

Here is my function that send the email. In case of errors, I would like the screen to go to /home/#contact as the contact form is far below in my web page. I would like to avoid the user to scroll down to the form.

So instead of

Code:
$this->load->view('header_view',$data);
$this->load->view('home_view', $data);

If would need to sth like this (but this one below is not working :-()

Code:
$this->load->view('header_view',$data);
$this->load->view('home_view#contact', $data);

if I do this :

Code:
redirect('home#contact','refresh');

The page goes correctly to the form, I do not have the error message of my form. I am in need of this for two web sites... I have included the whole code below.

I am wondering if the this->load->view() method support anchors ? If not, can this be considered as a bug ? Or shall it be an enhancement of the next version ?

Any help would be greatly appreaciated!

Thanks,

T.

This is in the controler :

Code:
Function mail(){
    
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('nom', 'nom', 'trim|required|alpha_numeric|xss_clean');
        $this->form_validation->set_rules('prenom', 'prenom', 'trim|required|alpha_numeric|xss_clean');
        $this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');
        $this->form_validation->set_rules('texte', 'texte', 'trim|required|xss_clean');
        
        $this->form_validation->set_message('required', 'Le champ %s est obligatoire');
        $this->form_validation->set_message('alpha_numeric', 'Le champ %s doit ĂȘtre alphanumerique');
        $this->form_validation->set_message('valid_email', "L'%s doit ĂȘtre un email valide");
        
        $this->form_validation->set_error_delimiters('<li>', '</li>');

        if ($this->form_validation->run() == TRUE){
            
            //$this->email->clear();
            $this->email->from($email,$nom." ".$prenom );
            $this->email->to('[email protected]');
            $this->email->reply_to($email,$nom." ".$prenom);
            $this->email->cc($email);
            $this->email->subject('atravers.fr - contact');
            $this->email->message($texte);
            $this->email->send();
            
            $data['formsucess'] = 'Merci pour votre message';
        
            // redirect to home page    
            $this->load->view('header_view',$data);
            $this->load->view('home_view', $data);
        }
        else
        {
            
            $this->load->view('header_view');
            $this->load->view('home_view');
            
        }
        
    }

this is in the view file :


Code:
<div id="contact">
<div id="formulaire">
&lt;!--- FORMULAIRE --&gt;
                     <p><ul>&lt;?php echo validation_errors(); ?&gt;</ul></p>
                    &lt;?php echo form_open('home/mail'); ?&gt;
                        <p>Nom</p>
                        &lt;input name='nom' type='text' size='40' value="&lt;?php echo set_value('nom'); ?&gt;"/&gt;
                        <p>Prenom</p>
                        &lt;input name='prenom' type='text' size='40' value="&lt;?php echo set_value('prenom'); ?&gt;"/&gt;&lt;br/>
                        <p>Email</p>
                        &lt;input name='email' type='text' size='40' value="&lt;?php echo set_value('email'); ?&gt;"/&gt;&lt;br/>
                        <p>Message</p>
                        &lt;textarea class="textareasize" name='texte' cols="" rows=""&gt;&lt;?php echo set_value('texte'); ?&gt;&lt;/textarea&gt;&lt;br/><br/>
                        &lt;input name='soumettre' type='submit' value='Envoyer'/&gt;&lt;br/>
                    &lt;/form&gt;
</div>
</div>
#2

[eluser]danmontgomery[/eluser]
[quote author="tintamarre" date="1310410044"]I am wondering if the this->load->view() method support anchors ? If not, can this be considered as a bug ? Or shall it be an enhancement of the next version ?[/quote]

No, and no. It's a feature that requires javascript (as such will never be built in), but you are welcome to implement it yourself.

http://flesler.blogspot.com/2007/10/jqueryscrollto.html
#3

[eluser]tintamarre[/eluser]
Hello,

Thanks a lot for your reply. So you mean I should implement something that in javascript will move the window to my anchor (only in the case where my form has been submitted?

I think this make sense and the article you indicated is good.

I am just wondering how would I trigger the javascript only if my form has been submitted. Shall I include my javascript using PHH only when it should be triggered ? Maybe there are some more clean way to do this.

Thanks a lot for your answer!

T.
#4

[eluser]danmontgomery[/eluser]
Well, generally speaking, you can check for form submission using $_POST or $this->input->post(), the same way you do when doing form validation. You could include the necessary files then.

I reread your post and technically you could accomplish this without javascript, but not by loading a view. The anchor is part of the URL, and the view is not. You can do it using the redirect method you have described, but you would have to save the form data to the session in order to access it on the other side of the redirect. Not really sure which way is better for you.
#5

[eluser]tintamarre[/eluser]
Thanks for this. I have done this and it's working properly!
Code:
&lt;?php if(isset($scrolltocontact) && $scrolltocontact == TRUE):?&gt;
$.smoothScroll({ easing: 'swing', scrollTarget: '#contact', speed: 0});
&lt;?php endif;?&gt;

T.




Theme © iAndrew 2016 - Forum software by © MyBB