Welcome Guest, Not a member yet? Register   Sign In
Ajax that can reload a form with the validation errors
#1

[eluser]bobbob[/eluser]
I want to submit a form with Ajax but if it fails it should load the whole page again instead of trying to put the whole page into the div that was intended for the success message of the form processing.
I am thinking that whatever is returned from that controller has to go to the target div of the ajax response
I'd like to be wrong. Any chance??
#2

[eluser]Colin Williams[/eluser]
I don't understand. What do you suppose is limiting you from writing it to work in whichever way you determine appropriate?
#3

[eluser]bobbob[/eluser]
When the ajax form is submitted I don't know how to stop the result being loaded in the div if it doesn't pass the validation.
My form is loaded as a regular page with header, footer etc. Loaded like this:
$this->load->view('form_view');

I need that page loaded on validation failure and the response text to the div tag on success.


The response text is simply:
echo 'Form submitted';

which loads into the same page it came from. The other creates a whole header footer etc in the div tag.

How to get the ajax call to not return to ajax if needed?
#4

[eluser]Colin Williams[/eluser]
I'd imagine a few conditional blocks within your controller could solve the problem. You have complete control over what you serve back, whatever the request type.
#5

[eluser]bobbob[/eluser]
My controller has this in it:

Code:
function index()
        {
                $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('name', 'Friend name', 'trim|required|min_length[2]|max_length[30]|xss_clean');
        $this->form_validation->set_rules('name1', 'Friend name', 'trim|min_length[2]|max_length[30]|xss_clean');
        $this->form_validation->set_rules('name2', 'Friend name', 'trim|min_length[2]|max_length[30]|xss_clean');
        $this->form_validation->set_rules('name3', 'Friend name', 'trim|min_length[2]|max_length[30]|xss_clean');
        $this->form_validation->set_rules('name4', 'Friend name', 'trim|min_length[2]|max_length[30]|xss_clean');
        $this->form_validation->set_rules('message', 'Message', 'trim|required');
        $this->form_validation->set_rules('known_name', 'Known Name', 'trim|required');
        $this->form_validation->set_rules('email', '1st Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('email1', '2nd Email', 'trim|valid_email');
                $this->form_validation->set_rules('email2', '3rd Email', 'trim|valid_email');
                $this->form_validation->set_rules('email3', '4th Email', 'trim|valid_email');
                $this->form_validation->set_rules('email4', '5th Email', 'trim|valid_email');
                
            
             if ($this->form_validation->run() == FALSE)
            
            {
            $data['title'] = 'Tell a friend';
        
        
        $data['header'] = $this->load->view('header',$data);
        
        $this->load->view('tell/tell_friend_view', $data);
            }
            else
            {
//some email function...
echo "success";
}

So the form calls this function and so does any link to the tell a friend page.
If there is a way of validating the form so that the whole page is loaded with the validation errors when there are some or if there are none for the response to just go in the div tag saying Success?
My question is how to prevent the response from an ajax call from returning to the div?
#6

[eluser]DIMM[/eluser]
You need to load only the part of page(like "validation successful") is an answer into <div>. did I get it right?
look at this tutorial




Theme © iAndrew 2016 - Forum software by © MyBB