使用带有ajax请求的ajax删除

时间:2016-12-23 07:46:01

标签: php jquery ajax

我有以下似乎不起作用。我哪里错了。 请求数据工作正常,但是当我想使用检索到的数据中的id删除时,它没有做任何事情。

date.php

<script type="text/javascript" src="http://ajax.googleapis.com/
    ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document)
        .ready(function () {
            $(".date,.site")
                .change(function () {
                    var site = $("#site")
                        .val();
                    var id = $("#date")
                        .val();
                    var dataString = 'id=' + id + '&site=' + site;

                    $.ajax({
                        type: "POST"
                        , url: "process.php?process=dselect"
                        , data: dataString
                        , cache: false
                        , success: function (html) {
                            $(".data")
                                .html(html);
                        }
                    });

                });

        });
    $(document)
        .ready(function () {
            $(document)
                .click('.delete', function () {
                    var id = $("#data")
                        .val();
                    var dataString = 'id=' + id;
                    alert(dataString);
                    $.ajax({
                        type: 'POST'
                        , url: 'process.php?process=delete'
                        , data: dataString
                        , success: function (data) {
                            if (data == "YES") {
                                alert("Holiday Deleted")
                            } else {
                                alert("can't delete the row")
                            }
                        }

                    });
                });

        });

</script>
</head>

<body>
    <form method="get" action="index.php">
        <select name="site" class="site" id="site">
                    <option>Select Site</option>
                    <option value="Self Park North">Self Park North</option>
                    <option value="Self Park South">Self Park South</option>
                    <option value="Valet North">Valet North</option>
                    <option value="Valet South">Valet South</option>
                    <option value="Summer Special">Summer Special</option>
                    <option value="cleaners">Cleaners</option>
                                </select>
        <br />
        <input name="date" type="date" value="" id="date" class="date" />
        <p>Select Patroller</p>
        <select name="data" class="data" id="data" size="20" style="width:400px;">

                        </select>
        <br />
        <input name="delete" type="button" value="delete" class="delete" id="delete" />
        <input name="Submit1" type="submit" value="submit" />

    </form>

process.php

case "delete":
 include('dbconnect.php');
 if ($_POST['id']) {
     $id = $_POST['id'];
     $query = "DELETE FROM taken WHERE id = '$id'";
     if ($conn - > query($sql) === TRUE) {
         echo "Record deleted successfully";
     } else {
         echo "Error deleting record: ".$conn - > error;
     }
 }
 break;

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果没有完整的信息,我无法分辨 - 错误消息,您在PHP片段中显示的变量的定义等。您应该检查错误日志以查看实际情况。

那就是说,这有一个错误:

if ($conn - > query($sql) === TRUE) {

如果你尝试在PHP中运行一些简单的东西来测试你是否可以通过这种方式扩展运算符,你会看到如下内容:

$ php -a
Interactive mode enabled

php > $a = (object) array("1" => "one", "two" => 2);

php > echo $a->two . PHP_EOL;
2

php > echo $a -> two . PHP_EOL;
2

php > echo $a - > two;
PHP Parse error:  syntax error, unexpected '>' in php shell code on line 1

你可以看到用空格围绕运算符是可以的,但是将它拆分不是。

这会导致您的脚本中断,但由于您没有包含您看到的错误,我无法确定这是唯一的问题。