为什么我的删除按钮不起作用?

时间:2016-12-16 09:09:25

标签: php jquery

我的代码是:(在回答建议后编辑

UNKNOWN

我希望从数据库中删除选中复选框的元素,包含在选中的[]数组中。在删除之前,我想要打开一个确认对话框,其中包含“提交”按钮。这将导致实际删除。但是,出于某种原因,上述代码不起作用。我甚至无法确定是否提交了帖子,因为行<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>SomuFinance - Personal Finance Manager</title> <link rel="stylesheet" type="text/css" href="indexStyle.css"> <script src="scripts/jquery-3.1.0.min.js"></script> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <div id="container"> <input type="submit" class="button" name="edit" value="Edit" /> <input type="button" class="button" name="delete" value="Delete" /> <input type="text" id="action" name="action"> <table id="listDB"> <tr> <th>Select</th> <th>ID</th> <th>Category ID</th> <th>Shop</th> <th>Item</th> <th>Quantity</th> <th>Unit</th> <th>Price Based On</th> <th>MRP</th> <th>Seller's Price</th> <th>Last Updated On</th> </tr> <?php $dbc = mysqli_connect('localhost','root','atlantis2016','itemDB') or die("Error Connecting to Database"); if(isset($_POST['submit'])) { echo "Action Set to ".$_POST['action']; if($_POST['action']=='confirmDelete') { echo "Now Deleting!!"; foreach ($_POST['selected'] as $delete_id) { $query = "DELETE FROM grocery WHERE id = $delete_id"; mysqli_query($dbc, $query) or die('Error querying database.'); } } } $query1 = "SELECT DISTINCT category FROM grocery"; $result1 = mysqli_query($dbc, $query1) or die("Error Querying Database"); while($row = mysqli_fetch_array($result1)) { $category = $row['category']; $query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC"; $result2 = mysqli_query($dbc, $query2) or die("Error Querying Database"); echo '<tr>'; echo '<td class="catHead" colspan=11>'.$category.'</td>'; echo '</tr>'; $catCount=1; while($inRow = mysqli_fetch_array($result2)) { $id = $inRow['id']; $shop = $inRow['shop']; $item = $inRow['item']; $qnty = $inRow['quantity']; $unit = $inRow['unit']; $price_based_on = $inRow['price_based_on']; $mrp = $inRow['MRP']; $sellers_price = $inRow['sellers_price']; $last_updated_on = $inRow['last_updated_on']; echo '<tr>'; echo '<td><input type="checkbox" value="' . $id . '" name="selected[]" /></td>'; echo '<td>'.$id.'</td>'; echo '<td>'.$catCount.'</td>'; echo '<td>'.$shop.'</td>'; echo '<td class="leftAligned">'.$item.'</td>'; echo '<td>'.$qnty.'</td>'; echo '<td>'.$unit.'</td>'; echo '<td>'.$price_based_on.'</td>'; echo '<td class="pri">₹'.$mrp.'</td>'; echo '<td class="pri">₹'.$sellers_price.'</td>'; echo '<td>'.$last_updated_on.'</td>'; echo '</tr>'; $catCount++; } } mysqli_close($dbc); ?> <input type="submit" value="Submit"> </table> </div> <div class="dialogBG"> <div id="deleteConfirmDialog" class="dialog"> <div class="closeDialog"></div> <p>Sure you want to delete the selected Data?</p> <input type="submit" id="confirmDelete" class="dialogButton" name="edit" value="Delete" /> <input type="button" class="dialogButton cancelButton" name="delete" value="Cancel" /> </div> </div> </form> <script type="text/javascript"> $(document).ready(function(){ $('.button').click(function(){ if($(this).val()=="Delete") { $(".dialogBG").fadeIn(200); $("#deleteConfirmDialog").show(200); $("#action").val('confirmDelete'); } else if($(this).val()=="Edit") { } }); $('#confirmDelete').click(function(){ $(".closeDialog").trigger("click"); }); $('#cancelDelete').click(function(){ }); $(".closeDialog").click(function (e){ $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200'); }); $(".cancelButton").click(function (e){ $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200'); }); $("form").submit(function(e){ alert("Form is being sumbitted!"); }); }); </script> </body> </html> 没有返回任何输出。请帮忙。

我相信整段代码都不起作用(来自人工测试)。

echo "Action Set to ".$_POST['action'];

任何想法为什么?

2 个答案:

答案 0 :(得分:1)

您需要将这些输入放在表单中并设置操作。如下所示:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" role="form">
    <input></input>
    <input></input>
</form>

这将是您的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SomuFinance - Personal Finance Manager</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script src="scripts/jquery-3.1.0.min.js"></script>
</head>
<body>
    <div id="container">
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" role="form">
        <input type="submit" class="button" name="edit" value="Edit" />
        <input type="button" class="button" name="delete" value="Delete" />
        <input type="text" id="action" name="action">
        <table id="listDB">
            <tr>
                <th>Select</th>
                <th>ID</th>
                <th>Category ID</th>
                <th>Shop</th>
                <th>Item</th>
                <th>Quantity</th>
                <th>Unit</th>
                <th>Price Based On</th>
                <th>MRP</th>
                <th>Seller's Price</th>
                <th>Last Updated On</th>
            </tr>
            <?php
                $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                            or die("Error Connecting to Database");

                if(isset($_POST['submit']))
                {
                    echo "Action Set to ".$_POST['action'];
                    if($_POST['action']=='confirmDelete')
                    {
                        echo "Now Deleting!!";
                        foreach ($_POST['selected'] as $delete_id)
                        {
                            $query = "DELETE FROM grocery WHERE id = $delete_id";
                            mysqli_query($dbc, $query)
                                or die('Error querying database.');
                        }
                    }
                }

                $query1 = "SELECT DISTINCT category FROM grocery";
                $result1 = mysqli_query($dbc, $query1)
                            or die("Error Querying Database");

                while($row = mysqli_fetch_array($result1))
                {
                    $category = $row['category'];
                    $query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
                    $result2 = mysqli_query($dbc, $query2)
                            or die("Error Querying Database");

                    echo '<tr>';
                        echo '<td class="catHead" colspan=11>'.$category.'</td>';
                    echo '</tr>';
                    $catCount=1;

                    while($inRow = mysqli_fetch_array($result2))
                    {
                        $id = $inRow['id'];
                        $shop = $inRow['shop'];
                        $item = $inRow['item'];
                        $qnty = $inRow['quantity'];
                        $unit = $inRow['unit'];
                        $price_based_on = $inRow['price_based_on'];
                        $mrp = $inRow['MRP'];
                        $sellers_price = $inRow['sellers_price'];
                        $last_updated_on = $inRow['last_updated_on'];

                        echo '<tr>';
                            echo '<td><input type="checkbox" value="' . $id . '" name="selected[]" /></td>';
                            echo '<td>'.$id.'</td>';
                            echo '<td>'.$catCount.'</td>';
                            echo '<td>'.$shop.'</td>';
                            echo '<td class="leftAligned">'.$item.'</td>';
                            echo '<td>'.$qnty.'</td>';
                            echo '<td>'.$unit.'</td>';
                            echo '<td>'.$price_based_on.'</td>';
                            echo '<td class="pri">₹'.$mrp.'</td>';
                            echo '<td class="pri">₹'.$sellers_price.'</td>';
                            echo '<td>'.$last_updated_on.'</td>';
                        echo '</tr>';

                        $catCount++;
                    }
                }

                mysqli_close($dbc);
            ?>
        </table>
      </form>
    </div>
    <script type="text/javascript">
        $(document).ready(function(){
            $('.button').click(function(){
                if($(this).val()=="Delete")
                {
                    $(".dialogBG").fadeIn(200);
                    $("#deleteConfirmDialog").show(200);
                    $("#action").val('confirmDelete');
                }
                else if($(this).val()=="Edit")
                {

                }
            });

            $('#confirmDelete').click(function(){
                $(".closeDialog").trigger("click");
            });
            $('#cancelDelete').click(function(){

            });
            $(".closeDialog").click(function (e){
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            });
            $(".cancelButton").click(function (e){
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            });
        });
    </script>

    <div class="dialogBG">
        <div id="deleteConfirmDialog" class="dialog">
            <div class="closeDialog"></div>
            <p>Sure you want to delete the selected Data?</p>
              <input type="submit" id="confirmDelete" class="dialogButton" name="edit" value="Delete" />
              <input type="button" class="dialogButton cancelButton" name="delete" value="Cancel" />
        </div>
    </div>
</body>
</html>

答案 1 :(得分:1)

解决方案很简单。 if(isset($_POST['confirmDelete']))需要在问题的问题部分而不是if(isset($_POST['submit'])),因为没有名为&#34;提交&#34;的按钮。在整个形式。

相关问题