edit.php server-side ajax button

edit.php server-side ajax button

chessGuru64chessGuru64 Posts: 79Questions: 18Answers: 1
edited September 2018 in Free community support

Without datatables library for a server-side you do something like this:

    while ($row = mysqli_fetch_array($records)) { ?>
    <tr>
    <td><?php echo $row['id']; ?> </td>
    <td><?php echo $row['first_name']; ?> </td>
    <td><?php echo $row['last_name']; ?> </td>
    <td><?php echo $row['position']; ?> </td>
    <td class="hidden-xs"><?php echo $row['date']; ?> </td>
    <td class="hidden-xs"><?php echo $row['updated']; ?> </td>
    <td>
    <a href="edit.php?edit=<?php echo $row['id']; ?>" name="edit" class="button green_btn"><span class="glyphicon glyphicon-pencil"> </a>

Normally with datatables you do this:

<input type="button" class="edit_btn" id=s-"' + meta.row + '" value="edit" name="edit_btn"> </a>';

Can you somehow mix the two like this?

'<a href="edit.php?edit=<?php echo meta.row ; ?>"type="button" class="delete_btn" id=n-"' + meta.row + '" value="delete"/>`

It does not seem to work.

What is the easiest method to do this?

Here is my index.php

$(document).on('click', '.update', function() {
    //$( "#form2" ).show();
    var id = $(this).attr("id").match(/\d+/)[0];
    var edit_id = $('#example').DataTable().row(id).data();
    var edit_id = edit_id[0];
    //console.log(edit_id[0]); 
    $.ajax({
        type: 'POST',
        url: 'edit.php',
        data: {
            edit_id: $("#edit1").val()
            edit2: $("#edit2").val(),
            edit3: $("#edit3").val(),
            edit4: $("#edit4").val(),
            edit5: $("#edit5").val(),
        },
        success: function(data) {
            alert(data);
            if (data == 'EDIT_OK') {
                alert("success");
            } else {
                //   alert('something wrong');
            }
        }
    })
});

Here is my edit.php

    $edit_id = $_POST['edit_id']; 
    $stmt = $conn->prepare("UPDATE employees SET first_name=?, last_name=?, position=?, updated=? WHERE id=?");
    $stmt->bind_param('ssssi', $_POST['edit1'], $_POST['edit2'], $_POST['edit3'], $_POST['edit4'], $_POST['edit_id']);
    $confirmUpdate = $stmt->execute();
    
    if($confirmUpdate) {
        echo "EDIT_OK";
    }else {
        trigger_error($conn->error, E_USER_ERROR);
        return;
    }

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 62,994Questions: 1Answers: 10,368 Site admin
    <?php echo meta.row ; ?>

    You can't do that because the PHP doesn't know anything about the DataTables internal indexing. The Javascript is executed on the client-side after the PHP was executed on the server-side and sent to the client.

    You could use columns.render to create the link tag, but I don't understand why you would want to send the meta.row client-side data index to the server. It is meaningless there. You'd want to send the primary key value which is going to be id in this case, which the server already knows about.

    Allan

This discussion has been closed.