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

发布于 2024-11-19 17:49:48 字数 5956 浏览 1 评论 0原文

我的目标是删除通讯员 使用 AJAX 获取 mysql 数据库表中的数据。 所以我生成了一个包含多行的表,并且想要一个非特定行...

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:

<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>

我的代码仅删除第一行... 如何参考正确的 td 值? 并将它们传递给ajax?

编辑: 我收到此错误: TypeError: object is not a function [http://127.0.0.1/somesite/index.php?page=consAlerts:[35]

该错误指的是以下代码:

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

所以这不是一个函数,这意味着语法错误?

My goal here is to delete the correspondent <td> data in mysql db table using AJAX.
So I've generated a table with several rows and want to a non specific row...

PHP Code:

<?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:

<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?
And pass them to the ajax?

EDIT:
I'm getting this error:
TypeError: object is not a function [http://127.0.0.1/somesite/index.php?page=consAlerts:[35]

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁人与我共长歌 2024-11-26 17:49:48

使用该运算符。

 $.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){

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){
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文