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

[eluser]LuckyFella73[/eluser]
Code:
function add_person()
    {
        $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)
        {
            $this->load->view('form_view');
        }
        else
        {
            $this->load->model('Form_model', '', TRUE);
            $this->Form_model->add_person();
            // when you got here your form submit was
    // valid and you have to send the message to
    // your view. FOR EXAMPLE:
    $array = '{ "message" : "Your data has been saved" }';
        echo $array;

        }
}

In the js part in your view file you do this:

Code:
$(document).ready(function()
{
    $("#submit_item").click(function ()
    {
        var item = $('#fname').val();
        var item = $('#email').val();

        $.post("Form/add_person", { "fname" : item, "email" : item }, // extend the brackets {} with your items seperated with ","
        
        function(data) { // when your callback func. answers here we go
            // in this example your callback function returned variable "result"
            // in json encoded

            alert(data.message);

            // return a flag (from callback funct.) when the formdata is saved and everything is fine
            // print message here -> depending on the flag ("success" or "try again")
        }, 'json');

    });
});

Instead of
Code:
alert(data.message);

you now define with js where to push the message. One way would be to set up
a DIV tag (maybe on to of the page) and first hide it (before submit) with css. set an id for
that DIV tag. now you can assign the message to the element and make the element visible with jquery via the id instead of alerting it like I did just for example.

Hope that helps you


Messages In This Thread
Help with adding a jquery alert box after form is correctly submitted - by El Forum - 05-11-2010, 01:23 PM



Theme © iAndrew 2016 - Forum software by © MyBB