irwan dwiyanto irwan dwiyanto - 21 days ago 7
Javascript Question

Error fetching data with ajax

I tried to edit the data, with modal boostrap and ajax, but when on the run after the error occurred errors traced the fault is in this script

onclick="editUser('.$row->id.');"
how to fix it ??

my script

function edit(id) {
$.ajax({
url : "<?php echo site_url('edit')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="name"]').val(data.name);
$('[name="id"]').val(data.id);
$('[name="name"]').focus();
$('#edit').modal('show'); // show bootstrap modal when complete loaded
},
error: function (jqXHR, errorThrown)
{
alert('Error ajax');
}
});

}


HTML

<?php
$no = 1;
foreach ($user as $row) {
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $row->nik; ?></td>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->name; ?></td>
<td align="center">
<a href="javascript:void(0)" onclick="editUser('.$row->nik.');" class="btn btn-warning"><span class="glyphicon glyphicon-pencil"></span></a> &nbsp;
</td>
<?php $no++; }?>

Answer

You have a bit of php/javascript soup. Your href value needs to be enclosed in php tags. Change:

 onclick="editUser('.$row->nik.');" 

to

 onclick="editUser('<?php echo $row->nik;?>');" 
Comments