0

I want to know how I can pass the value of input textbox into php variable without button and refresh of page, because I want to pass the value into controller and model. I'm using CodeIgniter.

This is just an example.

     Controller.php
     class Home extends CI_Controller {

        public function main(){
         $entry_id = $this->input->post('entry_id');
        $datas['query'] = $this->model->samplemodel($entry_id);
        $this->load->view('sampleview',$datas);
        }
      }

.

     Model.php

      class Model extends CI_Model{

      public function samplemodel(entry_id){
       $this->load->database();
       $this->db->select("fullname");
       $this->db->where('entry_id',entry_id);
       $query = $this->db->get('position');     
       return $query->result();
    }
   }

.

   sampleview.php

  <input type="textbox" name="id">

 <?php
 foreach($query as $row){

   echo $row->fullname;


   }

  ?>

. Output will be...

    grace
    paul
    mang juan

Every time you change the value of input textbox the output will be change.

2
  • Use a change event on the textbox instead of a click event on a button. Commented Nov 27, 2015 at 8:24
  • you have answered it yourself you need an on change event or on key press or up event for that input. Commented Nov 27, 2015 at 8:39

2 Answers 2

1

add an id to the textbox

 <input type="textbox" id="id">

send via POST by jquery after event keydown pressed

$('#id').keydown(function (event)
{

    if (eventer code hereent.keyCode == 13)
    {
        event.preventDefault();
        var id = $(this).val();
        $.post( "/home/main", { entry_id: id });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Give class or ID to textBox then use that id or class to target keyup event of textbox.
for eg. Textbox HTML

<input type="textbox" id="txtId">

Display area html

<div id='innerHTMLDiv'></div>

Jquery live KeyUp event

$('#txtId').live( 'keyup', function() {
       // ajax call to post data         
});

Now add your ajax call to post data in php file.

$('#txtId').live( 'keyup', function() {
      var id = $('#txtId').val();
      $.ajax({
        url: 'sampleview.php',
        type: "POST",
        data: {id: id},
        success: function (res) {
            try {
                $('#innerHTMLDiv').html(res);
            } catch (error) {
                $('#innerHTMLDiv').html("<b>Error in load data</b>");
            }
        }
    });             
});

Now get id value in your sampleview.php file as below.

$id = $_REQUEST['id'];

Get Data by query and echo it. this will display your result in your innerHTMLDiv div where you want to see the result in browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.