Welcome Guest, Not a member yet? Register   Sign In
js ajax doesn't work
#1

(This post was last modified: 02-21-2016, 09:49 PM by kilishan. Edit Reason: Added code tags for better readability. )

Hi,

I have probably a simple yet annoying issue with a json query. I want to verify an input value against the value in de customers database (column cf1).

With the code so far i recognize that the ajax is not functioning well, the result is always false therefore the function is ending without the desired check.

I have a function in pos.js which i rewrited to add a checkup on input value. In this code i check the input value (ref) and check it on a function in pos.php if it belongs to the current user. 

js file
------
Code:
    $('#suspend_sale').click(function () {
        ref = $('#reference_note').val();
        if (!ref || ref == '') {
     bootbox.alert(lang.wrong_pin);
              return false;
        } else {
        $.ajax({type: 'get',
            url: base_url+'pos/get_user_pin',
            data: "cf1=ref",
            dataType: "json",
            cache: false,
            success: function(data) {
                if (data !== null) {
  get_user_pin(data);
                } else {
     bootbox.alert(lang.wrong_pin);
                }
            }
        });
        }
        code = null;
    });

In pos.php i have a function to check the input value retrieved from the js code.

Code:
    function get_user_pin($code) {
if ($this->pos_model->GetUserPINbyID($code)) {
      echo true;
        } 
        else {
        echo NULL;
        }
    }

In pos_model.php i have a function that actually calls the database and retrieves the value based upon the current user id
Code:
    public function GetUserPINbyID($pincode) {
        $user_id = $this->session->userdata('user_id');
        $this->db->select($this->db->dbprefix('customers') . '.id as id, ' . $this->db->dbprefix('customers') . '.cf1 as cf1');
        $q = $this->db->get_where('customers', array('id' => $user_id), 1);
        if ($q->num_rows() > 0) {
            foreach (($q->results()) as $row) {
               return true;
           }
        } else {
        return false;
        }
    }


What is false in my approach, and how can i resolve this?
Reply
#2

Maybe something more like this, where the ref(code) is passed in the URL, as the controller method is expecting it:


Code:
$('#suspend_sale').click(function () {
    ref = $('#reference_note').val();
    if (!ref || ref == '') {
        bootbox.alert(lang.wrong_pin);
        return false;
    } else {
        $.ajax({
            type: 'get',
            url: '/pos/get_user_pin/' + ref,
            dataType: "json",
            cache: false,
            success: function(data) {
                if (data !== null) {
                    get_user_pin(data);
                } else {
                    bootbox.alert(lang.wrong_pin);
                }
            }
        });
    }
    code = null;
});
Reply




Theme © iAndrew 2016 - Forum software by © MyBB