Reputation: 61
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.
Upvotes: 0
Views: 1128
Reputation: 999
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.
Upvotes: 0
Reputation: 41
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 });
}
});
Upvotes: 1