从数据库中删除所选选项

时间:2015-12-18 14:41:57

标签: php jquery ajax

我有一个用我的数据库中的项动态创建的select元素。如果选择删除按钮,我希望能够从数据库中删除记录。目前我有一个AJAX函数,它发送一个GET请求到我当前页面删除它,它从我的选项中删除项目,但是,我用来访问我的数据库的PHP永远不会被调用,因此当我刷新时,查询是跑了,我刚刚去掉了什么"再次显示,因为它仍在我的数据库中。我知道我必须遗漏一些简单化的东西,我不能完全放弃我的东西 用手指指着它,感谢任何帮助或建议。我也对其他方法持开放态度。这绝对不是我的强项。

我的AJAX:

     window.onload = function () {
    document.getElementById("deleteLoc").onclick = function () {
        jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "adminLocationEdit.php", //Where to make Ajax calls
        data:{deleteLoc : $("#lList option:selected").val() },
        dataType:"text", // Data type, HTML, json etc.
        });
       $("#lList option:selected").remove();
        }
    };

我的HTML:

    <select id ="lList" multiple="multiple" style="width:400px;height:400px;">  
    <?php   
    //Query that selects all locations from the database and creates the list dynamically.
    $qry = "SELECT * FROM location";
    $qry_result = odbc_exec($admconn,$qry) or die("A database error has been detected. Err: adminLocationListEdit-1");

    //Echo the db results into the select box.
    while($row = odbc_fetch_array($qry_result)){
    //Sets each row to variable. 
    $locID = $row['locationName'];
    echo "<option id =\"$locID\" name = \"$locID\" onclick =\"$('#form1').load('incl/adminLocationEdit.php?loc='+$(this).attr('id'));displayFieldsets('form1', 'locList', 'lList');\">" . $locID . "</option>";
    }
    ?>  

我的PHP:

//If user wants to add a new location
if(isset($_POST['addLoc'])){
    //Re-directs
    //header("Location: adminLocation.php");
    exit;  

}
//If user wants to Delete a location from the database.
if(isset($_POST['deleteLoc'])){
    $contentToDelete = $_POST['deleteLoc']; 
    //Deletes current location from the database.
    $qry = "DELETE FROM location WHERE locationName = '" . $contentToDelete . "'";
    $qry_result = odbc_exec($admconn,$qry) or die("A database error has been detected. Err: adminLocationListEdit-2");

}

1 个答案:

答案 0 :(得分:0)

您正在混合使用GETPOST

如果您正在寻找帖子,

这应该是

data:{deleteLoc : $("#lList option:selected").val() },

或者如果您正在寻找fr GET

type: "GET",// this should be GET not POST