javascript - Represent json from jquery ajax response into a html's table -


i create search form using ajax action. in success ajax, how can load specific div without load refresh page. this form

<?php         $properties = array('id' => 'form1');         echo form_open("", $properties);         ?>          <fieldset>             <div class="controls" id="chekboxes">                 <label class="checkbox "><input type="checkbox" name="criteria[]" id="nomorcb"/> nomor request </label>                 <input type="text" class="input-xlarge focused" id="no" name="no" placeholder="masukkan no request..."/>                  <label class="checkbox "><input type="checkbox" name="criteria[]" id="namacb"/> nama user </label>                 <input type="text" class="input-xlarge focused" id="nama" name="nama" placeholder="masukkan nama user..."/>                  <label class="checkbox "><input type="checkbox" name="criteria[]" id="departementcb" /> departement</label>                 <div class="control-group">                     <div class="controls">                         <select class="input-xlarge" id="selecterror" name="dep">                             <option value="">-- pilih departement --</option>                              <?php                             foreach ($dep $data) {                                 echo "<option value='" . $data['nama_departement'] . "'>" . $data['nama_departement'] . "</option>";                             }                             ?>                         </select>                     </div>                 </div>                  <label class="checkbox "><input type="checkbox" name="criteria[]" id="rentangcb"/> rentang waktu</label>                 <div class="controls" id="tanggal-rentang">                     <input type="text" class="input-small datepicker" id="tanggal" value="" name="tgl_awal"><span> &nbsp; &nbsp; &nbsp;s/d </span>                     <input type="text" class="input-small datepicker" id="tanggal2" value="" name="tgl_akhir">                 </div>             </div>              <div class="form-actions">                 <button type="submit" class="btn btn-primary" id="submit">cari</button>                 <button type="reset" class="btn" id="cancel">cancel</button>             </div>         </fieldset>         <?php echo form_close(); ?>  

this action's code execute form.

  public function search() {     $id_request = $this->input->post('nomor');     $nama_user = $this->input->post('nama');     $departement = $this->input->post('departement');     $awal = $this->input->post('tgl_awal');     if ($awal != "") {         $awal = $this->input->post('tgl_awal');     } else {         $awal = "2014-01-01";     }     $timestamp_awal = strtotime($awal);     $tgl_awal = date("y-m-d h:i:s", $timestamp_awal);      $akhir = $this->input->post('tgl_akhir');     if ($akhir != "") {         $akhir = $this->input->post('tgl_akhir');     } else {         $akhir = "2020-01-01";     }      $timestamp_akhir = strtotime($akhir);     $tgl_akhir = date("y-m-d h:i:s", $timestamp_akhir);      $data = $this->model_request->search($id_request, $nama_user, $departement, $tgl_awal, $tgl_akhir);      echo json_encode($data); } 

and ajax jquery form above :

$('form').on('submit', function() {    var nomor = $('#no').val();    var nama = $('#nama').val();    var departement = $('#selecterror').val();    var tgl_awal = $('#tanggal').val();    var tgl_akhir = $('#tanggal2').val();     $.ajax({         url: '<?php echo base_url() . 'it_team/control_it/search' ?>',         type: 'post',         data: {nomor: nomor,                nama: nama,                departement: departement,                tgl_awal: tgl_awal,                tgl_akhir: tgl_akhir},                datatype: 'json',                success: function(obj) {                   //please give me idea                }         });      return false; 

for testing, try seearch , thank god, gives me success on json this:

[{"id_request":"015","nama_user":"dzil","departement":"it","waktu_it_terima":"2015-06-19 02:51:05"}, {"id_request":"017","nama_user":"dzil","departement":"it","waktu_it_terima":"2015-06-19 13:32:46"}] 

my problem is, result of search form above displaying table in same page form above. know, in tbody's table generate object on based return of json. newbie using json. table looked this

 <div class="box-content" id="things_table2">          <table class="table table-bordered table-striped table-condensed" id="table1">             <thead>                 <tr>                     <th>no.  </th>                     <th>no request</th>                     <th>nama user</th>                     <th>departement</th>                                                                 <th>tanggal request</th>                     <th>action</th>                 </tr>             </thead>                <tbody id="hasil-pencarian">                 // result showing here             </tbody>         </table>     </div> 

any appriciated.

try (beware there less fields in json in table):

var data = [   {     'id_request': '015',     'nama_user': 'dzil',     'departement': 'it',     'waktu_it_terima': '2015-06-19 02:51:05'   },   {     'id_request': '017',     'nama_user': 'dzil',     'departement': 'it',     'waktu_it_terima': '2015-06-19 13:32:46'   } ];  var html = ''; (var = 0; < data.length; i++) {   var td="";   for(var key in data[i]){     td+='<td>'+data[i][key]+'</td>';    }   html+="<tr>"+td+"</tr>"; }  $('#hasil-pencarian').html(html); 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -