[eluser]Unknown[/eluser]
Hello! I just downloaded CodeIgniter few days ago and already love it. It will save you so much time in developing web apps. However, I'm having a trouble with making ajax requests. I've made an view for login and "forgot password" forms and the view looks like this:
Note: Those action parts are written correctly, I just don't know why this will show them as they are seen in below.
Code:
<div id="wrapper">
<div id="center-login-logo"></div>
<form acti base_url()?>users/login" method="post" class="login_form">
<?php
if($error == 1) { ?>
<div class="login_error">
<p>Wrong username or password.</p>
</div>
<?php } ?>
<p><input type="text" name="email" placeholder="E-mail address" class="user" /></p>
<p><input type="password" name="password" placeholder="Password" class="password" /></p>
<div class="form_footer">
<h1>Forgot your password?</h1>
<p><a href="#" id="forget_pass_link">Click here</a> to get your password</p>
<input type="submit" name="submit" value="Login" class="login_submit" />
</div>
</form>
<form acti base_url()?>users/forgot_pass" method="post" id="forget_pass_form">
<div class="form_header">
<h1>Forgot your password?</h1>
</div>
<p>Please fill in your e-mail address and we will send you the password</p>
<div class="front_success" id="forgot-pass-success">
<p>The password has successfully send to your e-mail address.</p>
</div>
<div class="login_error" id="forgot-pass-error">
<p>We couldn't find the e-mail address from our system. Please try again.</p>
</div>
<div id="loading_spinner"></div>
<p><input type="text" name="to_email" id="to_email" placeholder="E-mail address" class="user" -200px; margin-top: 20px;" />
<input type="submit" name="to_submit" value="Send Password" class="login_submit" id="forget-pass" /></p>
</form>
</div>
Here I have my Controller for both of the methods, login and forgot password. Here's my controller:
Code:
<?php
class Users extends CI_Controller {
public function login()
{
$data['error'] = 0;
if(isset($_POST['submit'])) {
$this->load->model('user');
$email = $_POST['email'];
$password = $_POST['password'];
$user = $this->user->login($email, $password);
if(!$user) {
$data['error'] = 1;
} else {
echo "Login successfull";
}
}
$this->load->view('header');
$this->load->view('index', $data);
$this->load->view('footer');
}
public function forgot_pass()
{
if(empty($_POST['to_submit']))
{
exit("No post value");
}
$this->load->model('user');
$email = $_POST['to_email'];
$email_addr = $this->user->get_email_address($email);
if(empty($email_addr))
{
exit("Email address not found");
}
$password = $email_addr[0]['password'];
$this->load->library('email');
$this->email->from('[email protected]', 'Your Name');
$this->email->to($email);
$this->email->subject('Password');
$this->email->message($password);
echo ($this->email->send()) ? 1 : 0;
}
}
?>
I won't post my model since I know it's already working. Here's my ajax code for that forgot_pass() - method:
Code:
$(document).ready(function() {
$("#forget_pass_link").on("click", function(e) {
e.preventDefault();
$("form#forget_pass_form").easyModal({
autoOpen: true,
overlayOpacity: 0.1,
overlayColor: "#000000",
overlayClose: false,
});
});
$("form#forget_pass_form").on('submit', function(){
var from = $(this);
$.ajax({
url: from.attr('action'),
type: from.attr('method'),
data: $(from).serialize(),
success: function(data) {
alert(data);
}
});
return false;
});
});
Every time I'll press the forgot_pass() submit button, I'll always get that first exit() from the method saying "No post value". What I'm doing wrong and how should I implement the code to actually get the result 1 or 0 based on the condition if user's email address is actually found in the database.
Thanks in advance