如何使用php,AJAX或MySQL使用正确的ID使删除按钮完全正常工作?

时间:2022-08-25 17:22:15

I am having a little problem with this, every delete button is supposed to delete the record of its own id. If we click 164 it must delete the record of 164. It works fine if I remove the ajax and ask the form to validate directly, but if I use AJAX it only deletes the record of 1st record regardless of what button I press e.g. in current scenario it will always delete the record of 159 even if I press 164 button. My code gives the following output: Remember it works fine if I ask the form to validate directly from other PHP file. This is my output please have a look at it. Its quite simple!

我有一个小问题,每个删除按钮应该删除自己的id记录。如果我们点击164它必须删除164的记录。如果我删除ajax并要求表单直接验证,它工作正常,但如果我使用AJAX它只删除第一条记录的记录,无论我按什么按钮,例如在当前情况下,即使按下164按钮,它也将始终删除159的记录。我的代码提供了以下输出:如果我要求表单直接从其他PHP文件验证,请记住它工作正常。这是我的输出请看一下。它很简单!

if(is_numeric($lumens) && $lumens < 5000 && $lumens >250){

if(is_numeric($THD) && $THD <= 20 && $THD  >=0){

    if(is_numeric($scaled_power_factor) && $scaled_power_factor >=0.9){

        if(is_numeric($scaled_cct) && $scaled_cct <=5700){

            if(is_numeric($scaled_cri) && $scaled_cri >=65){

                if(is_numeric($scaled_input_power)){




                    $con = new mysqli(localhost, asd, myp, rec);
                    if(!$con){

                        echo "Couldn't connect to the database";
                    }
                    else{
                        $id = $_SESSION['user_id'];

                        $query = "INSERT INTO scaling_performance_data SET
                            MODEL_NUMBER = '$model_number',
                            LUMENS = '$lumens',
                            scaled_luminaire_efficacy = '$lm_w',
                            scaled_input_power = '$scaled_input_power',
                            THD = '$THD',
                            SCALED_POWER_FACTOR = '$scaled_power_factor',
                            SCALED_CCT = '$scaled_cct',
                            SCALED_CRI = '$scaled_cri',
                            HOUSING_VARIATION = '$housing_variation',
                            user_id = '$id'
                            ";
                        if($con->query($query)){

                            $sql = "SELECT * FROM scaling_performance_data WHERE user_id='$id';";
                            $result = $con->query($sql);
                            if($result){
                                if($result->num_rows > 0){
                                    while($row = $result->fetch_assoc()){




                                        ?>
                                        <form>
                                            <table>
                                                <tr>
                                                    <th>adsf</th><th>adsf</th><th>adsf</th><th>adsf</th><th>adsf</th><th>adsf</th><th><input type="button" name ="delete_id" id="delete_id" value="<?php echo $row['ID'];?>" onclick="vlid();"/></th>
                                                </tr>
                                            </table>
                                            <script type="text/javascript">
                                                function vlid(){

                                                    var delete_id = $('#delete_id').val();
                                                    alert(delete_id);
                                                    $.post('validator.php',{

                                                            postdelete_id : delete_id

                                                        },
                                                        function(data){

                                                            $('#del').html(data);

                                                        }
                                                    )
                                                }

                                            </script>
                                        </form>

                                    <?php

                                    }
                                }

validator.php is:

$id = $_POST['postdelete_id'];

$con = new mysqli(localhost, asd, myp, rec); 
if(!$con){

    echo "Couldn't connect to the database";
}
else{

    $query="DELETE FROM scaling_performance_data WHERE ID='$id';";

    if($con->query($query)){
        echo "Your Result was deleted successful";
        echo $id;

    }else{

        echo "There was a problem Please try again later";
    }
}

3 个解决方案

#1


0  

The problem is that in your vlid() function, JQuery is only selecting the first element with id = delete_id. I would try passing the ID to the vlid() function like this:

问题是在你的vlid()函数中,JQuery只选择id = delete_id的第一个元素。我会尝试将ID传递给vlid()函数,如下所示:

<input type="button" ... onclick="vlid(<?php echo $row['ID'];?>)"/>

And then modify your vlid() function to accept the ID parameter.

然后修改你的vlid()函数以接受ID参数。

#2


0  

Try var delete_id = $(event.target).val(); instead of: var delete_id = $('#delete_id').val();

尝试var delete_id = $(event.target).val();而不是:var delete_id = $('#delete_id')。val();

#3


0  

1st ID must be unique so use

第一个ID必须是唯一的,所以使用

class="delete_id"

instead of

id="delete_id"

2nd remove onclick="vlid();" and use

2nd remove onclick =“vlid();”并使用

$(document).ready(function(){
    $('body').on('click','.delete_id',function(){
       var getValue = parseInt($(this).val());
       $.post('validator.php',{postdelete_id : getValue},function(data){
          $('#del').html(data);
       });
    });
});

and to remove the tr which deleted use

并删除删除使用的tr

$(document).ready(function(){
        $('body').on('click','.delete_id',function(){
          var thisBtn = $(this);
          var getValue = parseInt(thisBtn .val());
           $.post('validator.php',{postdelete_id : getValue},function(data){
              $('#del').html(data);
              thisBtn.closest('tr').remove();
           });
        });
    });

#1


0  

The problem is that in your vlid() function, JQuery is only selecting the first element with id = delete_id. I would try passing the ID to the vlid() function like this:

问题是在你的vlid()函数中,JQuery只选择id = delete_id的第一个元素。我会尝试将ID传递给vlid()函数,如下所示:

<input type="button" ... onclick="vlid(<?php echo $row['ID'];?>)"/>

And then modify your vlid() function to accept the ID parameter.

然后修改你的vlid()函数以接受ID参数。

#2


0  

Try var delete_id = $(event.target).val(); instead of: var delete_id = $('#delete_id').val();

尝试var delete_id = $(event.target).val();而不是:var delete_id = $('#delete_id')。val();

#3


0  

1st ID must be unique so use

第一个ID必须是唯一的,所以使用

class="delete_id"

instead of

id="delete_id"

2nd remove onclick="vlid();" and use

2nd remove onclick =“vlid();”并使用

$(document).ready(function(){
    $('body').on('click','.delete_id',function(){
       var getValue = parseInt($(this).val());
       $.post('validator.php',{postdelete_id : getValue},function(data){
          $('#del').html(data);
       });
    });
});

and to remove the tr which deleted use

并删除删除使用的tr

$(document).ready(function(){
        $('body').on('click','.delete_id',function(){
          var thisBtn = $(this);
          var getValue = parseInt(thisBtn .val());
           $.post('validator.php',{postdelete_id : getValue},function(data){
              $('#del').html(data);
              thisBtn.closest('tr').remove();
           });
        });
    });