1

Intro I am doing a server-side datatables library.

This code below is not printing either $stmt or $row so I cannot see what is wrong. Which sucks because I wanted to solve this without posting....

<?php
    if (isset($_POST["edit_id"])) {
        $stmt = $conn->prepare("SELECT * FROM `employees` WHERE `id` = ?");
        print_r($stmt);
        $stmt->bind_param("i",$id);
        $id = $_POST['edit_id'];
        $stmt->execute();
        $result = mysqli_query($stmt);
        $row=mysqli_fetch_assoc($result);
        //$result = $stmt->get_result();
        //$row = mysqli_fetch_array($stmt);
        print_r($row);
    }
?>

My ajax call: This ajax call works by itself on its own...

$(document).on('click','.edit_btn',function (){
  var id = $(this).attr("id").match(/\d+/)[0];
  var edit_id = $('#example').DataTable().row( id ).data();
  var edit_id = edit_id[0];
  $.ajax({
    type:'POST',
    url: 'index.php',
    datatype: "json",
    data: { edit_id : edit_id },
    success: function(result) {
      //alert(edit_id);
      $("#edit_id").val(edit_id);
    } //success func
  }); //ajax
}); //end ready

My edit button (as you can clearly see, I named it edit_btn):

columnDefs: [
      {  targets: -1,
         render: function (data, type, row, meta) {
            return '<button type="submit"  class="edit_btn btn btn-success btn-md active" data-id=s-"' + meta.row + '"  id=s-"' + meta.row + '" value="edit" name="edit_btn" data-toggle="modal" data-target="#editForm">  <span class="glyphicon glyphicon-pencil"></span>  </a>';
         }
      }
    ]

Eventually I am going to echo the values into my edit form here like this in a form:

 <input type="text" value="<?php echo $row['first_name']  ?>"  class="" id="edit2" name="first_name" required>
Krupal Panchal
  • 1,553
  • 2
  • 13
  • 26
  • why doea tge $stmt has "id =?" ? shouldn't there be a variable, specifically $i, there? – Guy Louzon Oct 02 '18 at 06:01
  • 3
    @GuyLouzon - The OP is using prepared statements. – M. Eriksson Oct 02 '18 at 06:02
  • 1
    prepared statements for sqli injections. i binded $id to it which is edit_id from ajax call. –  Oct 02 '18 at 06:02
  • still, there is no mention of $i nor of i, not in the $stmt and not in the code prior... maybe its me – Guy Louzon Oct 02 '18 at 06:04
  • I don't see you using the result of the ajax query at all. What response are you expecting here? Also, you've defined `dataType: 'json'` while you're just dumping the result of `$row` (no json). – M. Eriksson Oct 02 '18 at 06:04
  • @GuyLouzon - Why would the OP have `$i` in the code? it's never defined or used. The OP is binding the ID to that placeholder: `$stmt->bind_param("i",$id);` which means that `$id` is an integer. – M. Eriksson Oct 02 '18 at 06:06
  • 3
    @GuyLouzon so you don't have to keep asking questions here, please thoroughly read this ~ http://php.net/manual/mysqli.quickstart.prepared-statements.php – Phil Oct 02 '18 at 06:07
  • Three things I see wrong with your client-side (JS) code... 1) `dataType: 'json` - your PHP code is not responding with JSON so this will cause the `$.ajax` call to fail. 2) `contentType: 'application/json; charset=utf-8'` - your PHP code is not expecting a JSON request body so don't set this. 3) The default `$.ajax` method is `GET`. If you want to POST data, you need to add `method: 'POST'` – Phil Oct 02 '18 at 06:12
  • Also, I don't see an element with `id="edit_id"` so your success function won't update anything – Phil Oct 02 '18 at 06:18

1 Answers1

3

The first part of your PHP code is checking if edit_btn is set in the data..

if (isset($_POST["edit_btn"])) {

The data you pass is set in...

data: { edit_id : edit_id },

As you don't set this, it will always fail, you can easily change this to check if the ID is set...

if (isset($_POST["edit_id"])) {

Edit: Also thanks to Phil...

The ajax call needs to be

$.ajax({
  url: 'index.php',
  dataType: "json",
  method: 'POST',
  data: { edit_id : edit_id },
  success: function(result) {
          //alert(result);
              $("#edit_id").val(result);
    } //success func
}); //ajax

Also noticed that your success method was passing result and you were using eidt_id in the code inside the method.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • ok. i did those 3 things (and agree). will edit in my new code. i do not think it fully solved it yet still nothing printing. –  Oct 02 '18 at 06:11
  • Updated the `success` method. – Nigel Ren Oct 02 '18 at 06:14
  • putting in `$("#edit_id").val(result)` gets me my entire webpage as of follows: DataTables Server-Side –  Oct 02 '18 at 06:25
  • Is that bad? putting in `$("#edit_id").val(edit_id)` gets me the edit_id. –  Oct 02 '18 at 06:25
  • for some reason, it still does not print after click. but you guys def got me in the right direction. –  Oct 02 '18 at 06:27
  • It may be worth looking through other examples of PHP and AJAX - https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php they might give you some more ideas. – Nigel Ren Oct 02 '18 at 06:29
  • ^Yeah, i think I need a separate form.php page as that example has. was just about to post that. –  Oct 02 '18 at 06:30