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
#2

[eluser]Karman de Lange[/eluser]
Hi,

1: try not to use $_POST , Rather use $this->input->post() .. This helps with security

2: I did not read your code properly but to debug, just do a var_dump($this->input->post()); above your check if(empty($_POST['to_submit'])) and use firebug or similar to see the data sent/received. With firebug you can see the actual post values that was sent.

L:
#3

[eluser]LuckyFella73[/eluser]
Regarding your last question:
Code:
// call your model method and assign the return value to a variable:
$email_exists = $this->user->mail_exists($email); // let the model return TRUE or FALSE

if( $email_exists === TRUE )
{
    exit("Email address not found");
}

for you didn't provide your model code I can't edit that right now but I guess you are able to manage that.
#4

[eluser]EyeAmN8[/eluser]
If you have csrf protect enebled in the config file, ajax requests will always fail for posts. In order to fix this you will need to pass the csrf token along with your post data. easiest way to do it is use codeiigniter's form helper, it will make one for you. Make sure that you either post that form or post the token from the form. There are other ways to get the token but that is the easiest to explain.
#5

[eluser]Sanjay Sarvaiya[/eluser]
I think the problem at serialize form data. could you test without $ sign as bellowed.
Code:
$.ajax({
         ....
         ...
           data: from.serialize(),//remove $ sign
                                success: function(data) {

                                 alert(data);
                }
        });




Theme © iAndrew 2016 - Forum software by © MyBB