Welcome Guest, Not a member yet? Register   Sign In
Help with adding a jquery alert box after form is correctly submitted
#15

[eluser]LuckyFella73[/eluser]
I tested the code and realized that you have to load the form_validation
class in your callback function and that your form-tag has to be removed.

Here the complete code, tested:

view css:
Code:
<style type="text/css">
div.msg-hide {display:none;}
div.msg-show {display:block;}
</style>
view form:
Code:
Full Name:<br />
    &lt;?php
    $attrib = array('id' => 'fname', 'name' => 'fname');
    echo form_input($attrib).form_error('fname');
    ?&gt;
    <br />E-mail Address:<br />
    &lt;?php
    $attrib = array('id' => 'email', 'name' => 'email');
    echo form_input($attrib).form_error('email');
    ?&gt;
    <br /><br />
    &lt;?php
    $attrib = array('id' => 'submit_item', 'name' => 'submit_item', 'value' => 'Add Person');
    echo form_submit($attrib);
    echo form_reset('reset', 'Reset Fields');
    echo form_close(); ?&gt;
global . js:
Code:
$(document).ready(function()
{
    $("#submit_item").click(function ()
    {
        var fname = $('#fname').val();
        var email = $('#email').val();
        
        $.post("form/add_person", { "fname" : fname, "email" : email },
        function(data)
        {
            $('#box_message').removeClass("msg-hide");
            $('#box_message').addClass("msg-show");

            $("#box_message").html(data.message);
        }, 'json');

    });

});

controller:
Code:
&lt;?php

class Form extends Controller {

    function Form()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
    }
    
    function index()
    {
        $this->load->view('form_view');
    }
    
    function add_person()
    {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('fname', ' ', 'trim|required|xss_clean');
        $this->form_validation->set_rules('email', ' ', 'trim|required|valid_email');
            
        if ($this->form_validation->run() == FALSE)
        {
            $array = '{ "message" : "Input not valid" }';
            echo $array;
        }
        else
        {
            #$this->load->model('Form_model', '', TRUE);
            #$this->Form_model->add_person();
            
            $array = '{ "message" : "All data has been saved" }';
            echo $array;
        }
    }
}

/* End of file form.php */
/* Location: ./system/application/controllers/form.php */

Don't forget to uncomment the 2 model-calls I had to deactivate ..


Messages In This Thread
Help with adding a jquery alert box after form is correctly submitted - by El Forum - 05-13-2010, 05:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB