Welcome Guest, Not a member yet? Register   Sign In
$.post() not working on simple request
#1

[eluser]Unknown[/eluser]
Hello!

I'm not a professional php programmer and I started using codeigniter some month ago for a personal website.

I wanted to try some ajax request and form but now I'm stuck!

I created a simple form with a jquery $.post() command but I can't find where is my mistake on that.

this is the controller (pretty simple)
Code:
function register()
    {
        
        //is_logged_in placeholder
        $this->load->library('form_validation');
        
        if($this->input->post('go'))
        {
            //form submitted, process it and return response
            
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[16]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('confirmation', 'Password Confirm', 'required|matches[password]|xss_clean');
            $this->form_validation->set_rules('birthdate', 'Data di Nascita', 'required|xss_clean');
            
            $data = array('email'           => $this->form_validation->set_value('email'),
                          'password'        => $this->form_validation->set_value('password'),
                          'confirmation'    => $this->form_validation->set_value('confirmation'),
                          'birthdate'       => $this->form_validation->set_value('birthdate')
                        );
            
            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('user/register', $data);
            }
            else
            {
                $this->load->view('user/registerAnswer', $data);
            }
            
            
        }
        else
        {
            $data = array('email'           => '',
                          'password'        => '',
                          'confirmation'    => '',
                          'birthdate'       => '');
            
            //Print the form
            $this->layout->view('user/register', $data);
        }
        
    }
- If I use the page itself it works perfectly
- the $this->layout->view() command is just a plugin for CI I got a month ago from the wiki, it's self-explanatory Wink (notice that I call ->layout only if the form is not submitted)

here is the html part (->view('user/register')):
Code:
[removed]
    $(function() {
            $( "#datepicker" ).datepicker();
            $( "#datepicker" ).datepicker( "option", "showAnim", 'bounce' );
    });
    
    $(document).ready(function(){
        $("#regform").submit(function(e)
        {
            e.preventDefault();
            //alert($("#regform").serialize());
            $("#formcontainer").html('Loading...').delay('1000').fadeIn();
            $.post("<?=base_url();?>user/register", $("#regform").serialize(),function(data){
                $("#formcontainer").append(data).fadeIn();
            });
        })
    });
[removed]

<div id="formcontainer">
    <div align="center" id="formError">&lt;?php echo validation_errors(); ?&gt;</div><br />
&lt;form method="post" action="" id="regform"&gt;
<table border="0" width="100%">
        <tr><td align="right">
    <label for="email">Email:</label></td>
        <td align="left">&lt;input type="text" name="email" placeholder="Il tuo indirizzo email" value="&lt;?=$email;?&gt;" /&gt;&lt;/td>
            </td></tr>
        <tr><td align="right">
    <label for="password">Password:</label></td>
        <td align="left">&lt;input type="password" name="password" value="&lt;?=$password;?&gt;" /&gt;
</td></tr>
<tr><td align="right">
    <label for="confirmation">Conferma Password:</label></td>
        <td align="left">&lt;input type="password" name="confirmation" value="&lt;?=$confirmation;?&gt;" /&gt;&lt;/td>
    
    <tr><td align="right"><label for="birthdate">Data di Nascita:</label></td>
        <td align="left">&lt;input type="text" id="datepicker" name="birthdate" value="&lt;?=$birthdate;?&gt;" /&gt;&lt;br /></td>
    </tr>
    <tr><td colspan="2" align="center">&lt;input type="hidden" name="go" value="1" /&gt;&lt;br />
      &lt;input type="submit" value="Registrati!" /&gt;&lt;/td>
    </tr>
</table>
&lt;/form&gt;
</div>

- I correctly load jquery and jqueryui on the layout page

Where does the problem stands?

when I submit the form with the jquery code the form disappear (and it's right) but the POST values are not submitted!

$this->input->post('go') is FALSE Sad

If I try the code without the jquery string the pages are loaded correctly so there is probably a problem in my jquery lines.

how can I fix this?

ty Smile


Messages In This Thread
$.post() not working on simple request - by El Forum - 09-27-2011, 05:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB