Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter and AJAX
#1

[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>

&lt;form acti base_url()?&gt;users/login" method="post" class="login_form"&gt;

&lt;?php

  if($error == 1) { ?&gt;

  <div class="login_error">

   <p>Wrong username or password.</p>

  </div>

&lt;?php } ?&gt;
<p>&lt;input type="text" name="email" placeholder="E-mail address" class="user" /&gt;&lt;/p>
<p>&lt;input type="password" name="password" placeholder="Password" class="password" /&gt;&lt;/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>
   &lt;input type="submit" name="submit" value="Login" class="login_submit" /&gt;
</div>

&lt;/form&gt;

&lt;form acti base_url()?&gt;users/forgot_pass" method="post" id="forget_pass_form"&gt;
  <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>&lt;input type="text" name="to_email" id="to_email" placeholder="E-mail address" class="user"  -200px; margin-top: 20px;" /&gt;

&lt;input type="submit" name="to_submit" value="Send Password" class="login_submit" id="forget-pass" /&gt;&lt;/p>

&lt;/form&gt;
</div>

Here I have my Controller for both of the methods, login and forgot password. Here's my controller:

Code:
&lt;?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;
    }
}
?&gt;

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 advanceSmile


Messages In This Thread
CodeIgniter and AJAX - by El Forum - 04-07-2014, 08:32 AM
CodeIgniter and AJAX - by El Forum - 04-08-2014, 02:43 AM
CodeIgniter and AJAX - by El Forum - 04-08-2014, 08:45 AM
CodeIgniter and AJAX - by El Forum - 05-07-2014, 09:02 AM
CodeIgniter and AJAX - by El Forum - 05-09-2014, 10:35 PM



Theme © iAndrew 2016 - Forum software by © MyBB