0

I am trying to edit some data using boostrap and ajax, however when I run the code an error occurs.

The error is on the line

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

How do I fix this?

My JavaScript code is

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');
        }
    });     

}

My HTML code is

<?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++; }?>
6
  • What is the error? Commented Nov 14, 2016 at 15:36
  • Please provide what error you are getting. Commented Nov 14, 2016 at 15:36
  • error alert show cannot success Commented Nov 14, 2016 at 15:37
  • dump site_url('edit') and check your url rendrerd Commented Nov 14, 2016 at 15:39
  • when I change onclick="editUser('.$row->nik.');" with onclick="editUser('123456');" ajax can work Commented Nov 14, 2016 at 15:41

2 Answers 2

2

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;?>');" 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @hairmot, I have tried but instead form modal not appear
could you have a look in developer tools (f12) at how the anchor element is rendering in the browser?
oops just edited my answer - try again (i'd left a character in that didnt belong)
0

An addition to the hairmot's answer:

You would also like to escape any characters that would interfere with the html:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.