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