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

[eluser]iwillsoarlikeaneagle[/eluser]
@LuckyFella73: Sorry for this late reply.

Here's a part of my form_view.php from the views folder:
Code:
<?php echo form_open('Form');?>
    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);
    ?&gt;
    &lt;?php echo form_reset('reset', 'Reset Fields'); ?&gt;
&lt;?php echo form_close(); ?&gt;

..the js found at the header(I've put the js tags and the source for the jquery.js):
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.result);

            // 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');

    });
});

..here's the add_person function in my form.php controller:
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();
            //
        }
}

Help:
1. What's the json_encode function for?
2. I'm confused. With the "starting pt." that you've shared (esp. for the callback function process()), I don't know what to do next with my codes.


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



Theme © iAndrew 2016 - Forum software by © MyBB