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

时间:2011-07-12 10:03:33

标签: php javascript jquery

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

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不是函数[http://127.0.0.1/somesite/index.php?page=consAlerts:[35]

错误指的是以下代码:

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

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

1 个答案:

答案 0 :(得分:0)

使用此运算符。

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