Welcome Guest, Not a member yet? Register   Sign In
How fill input fields with ajax
#1

Hello, I have a question about how to handle this situation.

I'm creating an edit page, I have and input field for the user to type the "ID" of the record I want to modify and a search button, when the user click on the search button I want to populate the form and if anything need to be changed give to the user the option to do that.

Controller ncmr
PHP Code:
class ncmr extends CI_Controller {

function 
edit()
 
    
        $this
->load->view('ncmr_edit');
 
   }

}

 function 
get_ncmr_by_id()
 
   {
 
       // GET VALUES BY ID
 
       $id $this->input->post('idncmr');
 
       $data['ncmr'] = $this->model->get_ncmr_by_id($id);
 
     
        print json_encode
($data);

 
   

View ncmr_edit
Code:
<html>
<head>

<script href="jquery" >

<script>
   $(document).ready(function () {
       $("#searchbtn").click( function()
          {        
            var ncmrid = $('#search').val();
           
            $.ajax({  
               url: "http://localhost/ncmr/index.php/ncmr/get_ncmr_by_id",
               async: false,
               type: "POST",
               data: "idncmr="+ncmrid,
               dataType: "html",

               success: function(data) {
                       $('#form').html(data);
                       }
                   })
   
          });
   });
   </script>

</head>
<body>

<input type="text" placeholder="Search by NCMR ID" id="search" />
<button class="btn btn-default" type="button" id="searchbtn">Search</button>

<br />

<input type="text" name="ncmr_num" value=""/>
<br />

<br />
<hr />
<br />
<div class="form">
<div>

</body>
</html>

So basically I'm showing the results on the ncmr_edit, but I would like to populate the ncmr field with the data from the query.
Code:
 
Reply
#2

Make your controller generate the value for the ncmr_num field only. Echo that out, so it will be returned to the Jquery script.
Also give the ncmr_num field an ID, e.g. id="ncmr_num".
Then, in the success part of AJAX, put $('#ncmr_num').val(data);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB