KaHeL
KaHeL

Reputation: 4371

Get current value of a textbox in codeigniter on keyup event

Hi I'm currently learning about the AJAX in jQuery and now I'm trying to use it in codeigniter. Now what happen is that I have a textbox wherein on keyup event it will call the function on my controller and it's working anyways but the controller itself doesn't get the current value of the textbox and it remains blank since I haven't submitted the form yet so it can't get the POST data. Well I don't want to submit the form either since I only have one shot for submit and what I only want for this one is to check whether the event is already existing or not (for validation purposes). Here's my code so far with added comments for better viewing.

View:

<div>
<form>
<table border="0">
    <tr>
        <td>
            <title>Name: </title>
        </td>
        <td>
            <input id="name" type="text" name="eventname" />
        </td>
        <td>
            <div id="name1"></div>
        </td>
    </tr>
</table>

<script type="text/javascript" src="<?php echo $base; ?>javascripts/jquery-1.8.2.min.js">
     </script>
<script type="text/javascript" src="<?php echo $base; ?>javascripts/ajax.js">
     </script>

</form>
</div>

The controller:

<?php

class Login extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('test_model');
    }

    public function view()
    {
        $data['base'] = $this->config->base_url();

        //CHECK IF THE $PAGE VIEW IS EXISTING
        if ( !file_exists('test/views/pages/test_form/test_form.php'))
        {
            // IF PAGE DOESN'T EXISTS SHOW 404 ERROR
            show_404();
        }

        //FOR THE TITLE PART
        $data['title'] = ucfirst('Test'); 

        $this->load->view('templates/header', $data);
        $this->load->view('pages/test_form/test_form', $data);
        $this->load->view('templates/footer', $data);

    }

    public function message()
    {
        //this one will access database
        echo $this->test_model->check_event($this->input->post('eventname'));
        //username doesn't get the current value on keyup unlike the AJAX part
    }

}

The model:

<?php
class Test_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }

    public function check_event($test_input)
    {
        if($test_input==null){
            return "True";
        }
        else
            return "False";
    }

}

and the AJAX:

$('#name').keyup(function() { //LISTENER FOR BUTTON CLICK BASED ON ID button ON THE PHP PAGE
    var name = $('#name').val();

    $.ajax({ //STARTS AJAX
    type: 'POST',
    url: '/CodeIgniter_2.1.3/index.php/login/message/', //THIS PAGE IS LOADED ON THE AJAX data IS THE CONTENTS ON THE PAGE NOTE COMMA SEPARATED VALUES
    data: 'name='+name,
    statusCode: { //THE STATUS FOR THE PAGE. NOTE THIS IS CASE SENSITIVE
        404: function(){ //IF 404 ERROR OCCURS THIS MESSAGE WILL BE DISPLAYED
            $('#name1').text('Page not found');
        }, //NOTE COMMA SEPARATED VALUES
        202: function(){ //IF 202 SHOW THIS TEXT
            $('#name1').text('Please wait');
        }
    }, //NOTE COMMA SEPARATED VALUES
    success: function(data){ //IF PAGE EXISTS OR FOUND THE data FROM THE GIVEN PAGE WILL BE LOADED AND PASSED
        $('#name1').html(data); //THE content ID ON THE PHP PAGE WILL CONTAIN ALL THE html data OF THE GIVEN PAGE
    }
    });
});

Well sorry for this AJAX part. it's full of comments which I don't think necessary It's just a note for me to quickly review the codes. :) Is there's something wrong with my codes or do I need to use some function to retrieve the data?

Upvotes: 1

Views: 5520

Answers (3)

Drazzi
Drazzi

Reputation: 96

It seems some error about naming, let's trace it:

1、Get the input:

<input id="name" type="text" name="eventname" />

var name = $('#name').val();
ajax{...
data: 'name='+name,...}

from the code above, you give the post param 'name' valued.

2、As it gave the post param 'name', you need replace:

$this->test_model->check_event($this->input->post('eventname'));

with:

$this->test_model->check_event($this->input->post('name'));

Upvotes: 2

bipen
bipen

Reputation: 36541

you are posting the data as "name"....

check your ajax

data: 'name='+name,   // this is data that is posted by ajax..you are posting it with a name as "name"

so u have to get the input post as "name" and not "eventname"...

either you have to rename the data's name data: 'name='+name, to data: 'eventname='+name,

OR

replace this

echo $this->test_model->check_event($this->input->post('eventname'));

with this

echo $this->test_model->check_event($this->input->post('name'));

in your controller and it should work....

Upvotes: 1

Sanjay
Sanjay

Reputation: 781

There is no field called value in the textbox so please modify your textbox like this and try

<input id="name" type="text" name="eventname" value="eventname"/>

Now i think you will get value,please check this

Upvotes: 0

Related Questions