This code shows nothing as a result. I want to pass a variable from ajax to the controller and then find the value from the model and pass it to ajax; And I want show a message like in the code.
View - test_view.php
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btnclick").click(function(){
var cname=$("#txt_uname").val();
//alert(cname);
$.ajax({
type:"post",
url:"http://localhost/NPOS/test/nameget",
data:{txt_uname : cname},
success:function(data){
if(data==0){
$("#message").html("<font color='#55DF00'>Name Available</font>");
}
else{
$("#message").html("<font color='#FF0000' >Name Already taken</font>");
}
}
});
});
});
</script>
<input type="text" name="txt_uname" id="txt_uname"/>
<input name="btnclick" type="button" id="btnclick" value="Search">
Controller - test.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public function index()
{
$this->load->view('test_view');
}
function nameget()
{ $this->load->model('test_model');
$name= $this->input->post('txt_uname');
$find=$this->test_model->nameget($name);
}
}
?>
model - test_model.php
class Test_Model extends CI_Model
{
function nameget($name)
{
$query=$this->db->query("SELECT * from countries where country_name='$name'");
return $query->result();
}
}
Use this Ajax code
<script type="text/javascript">
$(document).ready(function(){
$("#btnclick").click(function(){
var cname=$("#txt_uname").val();
//alert(cname);
$.ajax({
type:"post",
url:"<?php echo base_url() ?>test/nameget",
data:{txt_uname : cname},
success:function(data){
if(data==0){
$("#message").html("<font color='#55DF00'>Name Available</font>");
}
else{
$("#message").html("<font color='#FF0000' >Name Already taken</font>");
}
}
});
});
});
</script>
Data sending and URL changed
Opz forget this
In model
function nameget($name)
{
$query=$this->db->query("SELECT * from countries where country_name='$name'");
$result = $query->result_array();
return $result;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With