2011-07-12 75 views
0

我的目标是使用AJAX删除mysql db表中的记录<td>数据。 所以我已经生成的表几行,并希望非特定的行...如何使用ajax从表td中删除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?

编辑: 我得到这个错误: 类型错误:对象不是一个函数[http://127.0.0.1/somesite/index.php?page=consAlerts:[35]

错误指的是波纹管代码:

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

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

回答

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){ 
+0

这不工作是否有任何具体的代码在phpValidationScript.php? – obinoob