douqiao6563 2012-03-30 03:49
浏览 13
已采纳

使用MySQL和PHP删除页面上的项目

I am unsure of what path I should take for what I am wanting to do. The page loads some data from a mysql database with php. I made it so that a check box is generated with an incremented value starting wit 0 and so on for every value it finds in the database. What I am wanting to do is have that check box be so that when it is checked and they push the trashcan icon it will delete that row from the database. Where I am unsure is how to go about deleting it on button click and then reload the page. What would be the best way to do this? I am not needing the code or anything I just cant think of the best path to take to accomplish this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Print Run</title>

<style type="text/css">
    .openRun{
        width:500px;    
    }
    .openRun th{
        text-align:center;
        background-color:#CCC;
    }
</style>
</head>
<body>
<div>
    <?php
        $totals = 0;
        $i = 0;
        $companyName = "";
        $quantity = "";
        $cardSize = "";
        $dateAdded = "";
        require("serverInfo.php");
        $printRun = mysql_query("SELECT * FROM `printRun` WHERE status = 'Open'");
        $row = mysql_fetch_array($printRun);
        echo 'Print Run #: ' . $row['Run'];
        $result = mysql_query("SELECT * FROM `printRun` WHERE status = 'Open'");

        while($row = mysql_fetch_array($result)){
            $companyName[$i] = $row['Company'];
            $quantity[$i] = $row['Quantity'];
            $cardSize[$i] = $row['Size'];
            $dateAdded[$i] = $row['Date'];
            $totals = intval($row['Quantity']) + $totals;
            $i++;
        }

        $arraySize = count($companyName);
        echo '<table class="openRun">';
        echo '<tr><th>Company</th><th>Quantity</th><th>Size</th><th>Date Added</th><th></th></tr>';
        for($i = 0; $i < $arraySize; $i++){     
            echo '<tr>';
            echo '<td>' . $companyName[$i] . "</td>";
            echo '<td>' . $quantity[$i] . "</td>";
            echo '<td>' . $cardSize[$i] . "</td>";
            echo '<td>' . $dateAdded[$i] . "</td>";
            echo '<td><input type="checkbox" name="box'. $i .'" value="'. $i .'" /></td>';
            echo '</tr>';
        }
        echo '<tr>';
        echo '<td style="font-weight:bold; border-style:solid; border-top-width:1px;">Totals</td>';
        echo '<td style="font-weight:bold; border-style:solid; border-top-width:1px;">' . $totals . "</td>";
        echo '<td></td>';
        echo '<td></td>';
        echo '<td><img src="images/trash.png" /></td>';
        echo '</tr>';
        echo '</table>';
        mysql_close($link);

    ?>
  <br />
  </div>


</body>
</html>
  • 写回答

3条回答 默认 最新

  • dou7851 2012-03-30 04:01
    关注

    There's two ways of doing this. If you're not familiar with AJAX, or are scared about engaging with it, I'd suggest having a quick read of this tutorial just to get an overview.

    With AJAX

    If you don't want the page to reload, you're going to need to use AJAX to asynchronously send and receive data from the server. A typical example would be:

    $('.trash_link').on('click', function() {
       $.ajax({
          url: 'path/to/script.php',
          data: 'variable='+$('.input_class').val(),
          dataType: 'html',
          success: function(response) {
             $('.container').html(response);
          }
       });
    });
    

    The crucial thing I'd advise is to put all of your layout logic in a controller or model function so that you don't repeat yourself. When data is sent to your server it can use this function to send back all your layout via the AJAX function. You can then insert the HTML inside your container element.

    Without AJAX

    All you do here is submit a form and your page reloads. For that reason you need to make sure you've already specified tags on your page. It really depends on what style you want your page to have - some people prefer reloads, some prefer the seamlessness of AJAX. Here is what you'd need if you chose to omit AJAX:

    $('.trash_link').on('click', function() {
       $('form').submit();
    });
    

    You can also add a pop-up confirmation if you want to:

    $('.trash_link').on('click', function() {
       if(confirm("Are you REALLY sure?") {
          $('form').submit();
       }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?