Welcome Guest, Not a member yet? Register   Sign In
Can not access post value in controller using post .serialize() in codeigniter
#1

[eluser]chantheoun[/eluser]
Now I am facing a problem with post form value through jquery .serialize(). when past form value to jquery function, it can get them all as method Post. But when using .serialize() to send form value to controller, the controller can't get that value. Here is my code:

in view:

<form id="loginForm" name="loginForm" method="post">
Email: <input type="text" class="contactStyle required email" id="email" name="email"/>
Password:<input type="password" class="contactStyle required" id="password" name="password"/>
</form>
<a href='#' class="loginBut">Login</a>

in jquery:

login = function(){
.post(base_url + "elements/ajax_login", $("#loginForm").serialize(),
function(data){
if(data.success == 'success'){
top.location = top.location;
}else if(data.success == 'admin'){
top.location = base_url + "admin";
}else if(data.success == 'failed'){
alert('Incorrect login');
//ADD POPUP
$('#warn').hide().html('Your email / password combination is not correct.').show('slow');
}else{
alert("An error has occured please try refreshing the page.")
}
},'json');
}

in controller:
function ajax_login() {
$email = $this->input->post('email');
$result = array();

if ($this->ion_auth->login($email, $this->input->post('password'), 0)) { //if the login is successful
//if its an admin send them to the dashboard
if ($this->ion_auth->is_admin()) {
$result['success'] = 'admin';
} else {
$result['success'] = 'success';
}
} else { //if the login was un-successful
$result['success'] = 'failed';
}
echo json_encode($result);

}


I got the error that first step that passing value from view to jquery is Method Post but when passing value from jquery to controller is Method Get.

If anyone has solution or idea, please help.

Regards,

Chantheoun
#2

[eluser]InsiteFX[/eluser]
Try this:
Code:
$email = json_decode($this->input->post('email'));

And when you created he post topic did you read the top about using code tags?

#3

[eluser]CroNiX[/eluser]
Check out the missing $ for $.post() on this line:
Code:
.post(base_url + "elements/ajax_login",  $("#loginForm").serialize(),

Please use code tags when posting code.
#4

[eluser]InsiteFX[/eluser]
He should have seen it it was added to the top of the New Topic editor!
#5

[eluser]chantheoun[/eluser]
Sorry for my mistake. In jquery function, I already put $ but was missing when I wrote here.
For json_decode(), it still does not work.




Theme © iAndrew 2016 - Forum software by © MyBB