简体   繁体   English

如何使用 ajax 从表 td 中删除 mysql DB 中的数据?

[英]How to delete data in mysql DB from table td using ajax?

My goal here is to delete the correspondent <td> data in mysql db table using AJAX.我的目标是使用 AJAX 删除 mysql db 表中对应的 <td> 数据。 So I've generated a table with several rows and want to a non specific row...所以我生成了一个包含几行的表,并且想要一个非特定的行......

PHP Code: PHP 代码:

<?php
if(isset($_SESSION['login_status'])){

require 'classes/datahandle.class.php';
$data = new Datahandle();

    $query = "
        SELECT
            e.place,
            e.id_station,
            g.description,
            ac.max, ac.min,
            ac.mensagem
        FROM unit g, tableA ac, station e
        WHERE client = '" . $_SESSION['user_id'] . "'
            AND ac.unit=g.field
            AND ac.id_station=e.id_station
    ";

    $result = $data->selectDataQuery($query);
    $numRows = $data->getAffectedRows();

    if($numRows > 0){
            ?>
            <table id="consAlerts" border="0" align="center">
                    <tr>
                    <td style="text-align: center; color: white; font-weight: bold;">Value 1</td>
                    <td style="text-align: center; color: white; font-weight: bold;">Value 2</td>
                <td style="text-align: center; color: white; font-weight: bold;">Value 3</td>
                <td style="text-align: center; color: white; font-weight: bold;">Value 4</td>
                <td style="text-align: center; color: white; font-weight: bold;">Value 5</td>
                <td colspan="2"></td>
                    </tr>
        <?php
            while ($alert = mysql_fetch_array($result)) {
            ?>
                    <tr>
                    <td>
                    <select class="stationCons" disabled="disabled">
                    <?php
                        $query = "SELECT id_station,place FROM station";
                        $queryResult = $data->selectDataQuery($query);

                        while($option = mysql_fetch_array($queryResult)){
                                if($alert['id_station'] == $option['id_station']){
                                ?>
                                        <option value="<?php echo $option['id_station']?>" selected ><?php echo $option['place']?></option>
                                <?php
                                }if($alert['id_station'] != $option['id_station']){
                                ?>
                                        <option value="<?php echo $option['id_station']?>"><?php echo $option['place']?></option>
                                <?php
                                }
                        }
                    ?>
                    </select>
               </td>
                                    <td>
                    <select class="unitCons" disabled="disabled">
                    <?php
                        $query = "SELECT field, description FROM unit";
                        $queryResult = $data->selectDataQuery($query);

                        while($option = mysql_fetch_array($queryResult)){
                                if($alert['description'] == $option['description']){
                                ?>
                                        <option value="<?php echo $option['field']?>" selected="selected"><?php echo $option['description']?></option>
                                <?php
                                }if($alert['description'] != $option['description']){
                                ?>
                                        <option value="<?php echo $option['field']?>"><?php echo $option['description']?></option>
                                <?php
                                }
                        }
                    ?>
                    </select>
                            </td>
                            <td>
                    <input type="text" class="consMax" value="<?php echo $alert['max'] ?>"/>
                </td>
                            <td>
                    <input type="text" class="consMin" value="<?php echo $alert['min'] ?>"/>
                </td>
                            <td>
                    <input type="text" class="msg" value="<?php echo $alert['mensagem'] ?>"/>
                            </td>
                            <td>
                    <input type="submit" class="delete" value="Delete"/>
                </td>
                            <td>
                    <input type="submit"  class="edit" value="Edit"/>
                </td>
                    </tr>
            <?php
            }
            ?>
    </table>
    <?php
    }if ($numRows == 0) {?>
            <div style="color:#FFF; font-weight:bold; padding:10px; text-align:center;">Sometext</div>
<?php
    }
} else
    echo "<meta http-equiv='refresh' content='1; URL=../index.php'>";
?>

JQUERY: JQUERY:

<script type="text/javascript">
<!--
$(document).ready(function() {
    $(".delete").click(function(){
            //AJAX CALL
                    $.post("phpValidationScript.php", { station:$(".stationCons").val(), grandeza:$(".unitCons").val(), consMax:$(".consMax").val(), consMin:$(".consMin").val(), msg:$(".msg").val() }, function(data){
                            alert(data);
                    });
    });
    return false;
});
//-->
</script>

My code deletes ONLY the first row... How can I refer to the proper td values?我的代码只删除了第一行...我怎样才能引用正确的 td 值? And pass them to the ajax?并将它们传递给 ajax?

EDIT: I'm getting this error: TypeError: object is not a function [http://127.0.0.1/somesite/index.php?page=consAlerts:[35]编辑:我收到此错误: TypeError: object is not a function [http://127.0.0.1/somesite/index.ZE1BFD762321E409CEE4AC0B6E841963C0B6E841963C?

The error refers to the bellow code:错误是指下面的代码:

$.post("validaConsApagar.php", { estacao:$(this)(".estacaoCons").val(), grandeza:$(this)(".grandezaCons").val()}, function(data){...

So this is not a function that means that the syntax is wrong?所以这不是一个 function 那意味着语法错误?

use this operator.使用此运算符。

 $.post("phpValidationScript.php", { station:$(this)(".stationCons").val(), grandeza:$(this)(".unitCons").val(), consMax:$(this)(".consMax").val(), consMin:$(this)(".consMin").val(), msg:$(this)(".msg").val() }, function(data){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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