简体   繁体   中英

Delete row with Ajax Php Mysql, what am I doing wrong?

I am trying to delete rows of dynamic input fields with ajax: Below is my code, which is not working for some reason.

I have ajaxData.php is where I am posting the id to, but I am not sure if the id is actually sent (POST). The row is not getting deleted from the database.

The Form

<div class="form-group col-sm-3">
    <input value="<?php echo $child_data->child_name; ?>" class="form-control">
</div>
<div class="form-group col-sm-2">
    <a class="btn btn-danger btn-add-child remove-existing-child" style="margin-top:0;" id="<?php echo $child_data->child_id; ?>"><i class="fa fa-close fa-2x"></i></a>
</div>

<script>
//AJAX CALL HERE
$(document).ready(function() {
    $(".remove-existing-child").click(function() {
      var child_id = $(this).attr('id');
      $el = $(this).parent().parent();
      $.ajax({
        type: 'POST',
        url: 'ajaxData.php',
        data: {child_id: child_id},
        success: function(data) {
          if(response=="success") {
                    $el.remove();
                    alert("SUCCESSFULLY DELETED ELEMENT");
                }
        }
      });
    });
});
</script>

ajaxData.php

<?php
session_start();
include("db/config.php");
include("includes/function.php");

    $parent = $_GET['parent'];

    if($parent!="") {
      $result = mysql_query("select * from tbl_child where parent_id=$parent");
      while($row = mysql_fetch_array($result))
      {
        echo "<option value='$row[child_id]'>"; echo $row['name']; echo "</option>";
      }
    }

    // CHILD DELETE REQUEST BELOW

    $delete_child = $_REQUEST['child_id'];

    if(isset($delete_child)) {
      mysql_query("delete from tbl_child where child_id=$delete_child");
      echo "success";
      exit();
    }
    ?>

I have pasted all code from my ajaxData.php. Only the code that is below //CHILD DELETE REQUEST BELOW is related to the delete request. The code above that works perfectly well.

Try changing the

data: {child_id: child_id},

to

data:{'child_id': child_id}, // object attribute name enclosed in 's

then finally in the php

$delete_child = $_POST['child_id'];

Like Musa said.

I am not sure why, but when I added the full url like url: 'https://xxxxx.xxxx/ajaxData.php?child_id=+child_id and things started working . Can someone explain to me why this works with the full url but not with relative urls?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM