带有下拉菜单操作的PHP post复选框值

时间:2014-09-30 18:21:32

标签: javascript php jquery mysql

我有文章列表,对于每个文章我都有一个像这样的复选框:

<tr>
   <td>
     <input type="checkbox" name="selectedposts[]" class="check" value"1"/>
   </td>
   <td>TTILE   </td>
   <td>DATE    </td>
   <td>ACTIVITY</td>
</tr>
<tr>
   <td>
     <input type="checkbox" name="selectedposts[]" class="check" value"2"/>
   </td>
   <td>TTILE   </td>
   <td>DATE    </td>
   <td>ACTIVITY</td>
</tr>

我有下拉菜单,用于为每篇文章设置操作(delete,approved,sort.....):

<select class="selectD" name="action" id="action">
   <option value="delete">delete</option>
   <option value="approved">approved</option>
   <option value="noapproved">noapproved</option>
   <option value="sort">sort</option>
</select>
<input type="submit" value="OK" class="btn btn-success btn-sm" name="S1" />

在php页面中我有:

if ($_POST['action'] == "delete")
{
$selected = $_POST['selectedposts']; //LINE 15
if (count($_POST['selectedposts']) !== 0) // LINE 16
{
foreach($selected as $uid)
{
SQL::DEL("DELETE FROM " . NEWS_ARTICLES . " WHERE id = ?", $uid);
}

echo "<div class=\"success\">delete success</div>";
}
}

编辑:我检查了我的鳕鱼并看到了这个错误:

Notice: Undefined index: selectedposts in C:\xampp\htdocs\user\modules\allnews.php on line 15

Notice: Undefined index: selectedposts in C:\xampp\htdocs\user\modules\allnews.php on line 16

如何为每篇文章设置删除/批准/排序操作?我的意思是:如何选择带复选框的任何文章,从下拉列表中选择一个操作并发送到带有提交按钮的页面以进行删除/批准等操作?

3 个答案:

答案 0 :(得分:0)

最后我找到了你问题的确切答案...但是对于这个你需要jquery首先下载它并存储在当前程序文件夹中名为js的文件夹中

html文件:

<html>
<body>
<form action="my.php" method="post">
<tr>
   <td>
     <input type="checkbox" name="selectedposts[]" class="check" value"1"/>
   </td>
   <td>TTILE   </td>
   <td>DATE    </td>
   <td>ACTIVITY</td>
</tr>
<tr>
   <td>
     <input type="checkbox" name="selectedposts[]" class="check" value"2"/>
   </td>
   <td>TTILE   </td>
   <td>DATE    </td>
   <td>ACTIVITY</td>
</tr>
<select class="selectD" name="action" id="action">
   <option value="delete">delete</option>
   <option value="approved">approved</option>
   <option value="noapproved">noapproved</option>
   <option value="sort">sort</option>
</select>
<input type="hidden" name="name" id="name" value="false">
<input type="submit" value="OK" class="btn btn-success btn-sm" name="S1" />
</form>
<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('.check').click(function() {

document.getElementById('name').value="false";
$(".check:checked").each(function() {
    document.getElementById('name').value="true";
});



    });
});
</script>
</body>
</html>

php文件:(my.php)

<?php
if ($_POST['action'] == "delete")
{
    if($_POST['name']=="true")
    {
if (count($_POST['selectedposts']) != 0)
{
$selected = $_POST['selectedposts'];
foreach($selected as $uid)
{
SQL::DEL("DELETE FROM " . NEWS_ARTICLES . " WHERE id = ?", $uid);
}
echo "<div class=\"success\">delete success</div>";
}
}
else {
    echo "Nothing selected to delete";
}
}?>

进一步的查询评论如下。如果它满足你,不要忘记接受并upvote ans

答案 1 :(得分:0)

试试这个:

HTML:

<tr>
   <td>
     <input type="checkbox" name="selectedposts[]" class="check" value"1"/>
   </td>
   <td>TTILE   </td>
   <td>DATE    </td>
   <td>ACTIVITY</td>
</tr>
<tr>
   <td>
     <input type="checkbox" name="selectedposts[]" class="check" value"2"/>
   </td>
   <td>TTILE   </td>
   <td>DATE    </td>
   <td>ACTIVITY</td>
</tr>

<!-- Only do this form once, not for every row! -->
<form action="yourphpfile.php" method="post">
    <select class="selectD" name="action" id="action">
        <option value="delete">delete</option>
        <option value="approved">approved</option>
        <option value="noapproved">noapproved</option>
        <option value="sort">sort</option>
    </select>
    <input type="submit">
</form>

PHP:

if(count($_POST['selectedposts']) > 0){
    if($_POST['action'] == 'delete'){
        delete_posts($_POST['selectedposts']);
    }
    //Your other if-statements
}

function delete_posts($array){
    $ids = array();
    foreach($array as $key => $value){
        $ids[] = $value;
    }

    //Use PDO for your deletion
    $db = new PDO(...); //Use YOUR PDO instantiation
    $inQuery = implode(',', array_fill(0, count($ids), '?'));
    $stmt = $db->prepare(
        "DELETE FROM `table` WHERE id IN (" . $inQuery . ")"
    );

    $stmt->execute(array_values($ids))
}

我还没有对它进行过测试,但看看它能帮到你!

答案 2 :(得分:0)

你可以通过检查选中哪个复选框然后在它们上使用开关案例来实现这一点,我只是敲了一个基本的演示,我的意思是你可以在看到它如何工作之后制作你自己的工作代码,唯一的一点是,sort的选择选项中的值是一个PHP关键字,因此我将其更改为sortthem

<?php 
function getActionFromSelect($option){
    switch($option){
        case "delete": 
            // ur delete query
            echo "delete article "; 
            break;
        case "approved":
            // your approved query
            echo "approve article"; 
            break;
        case "noapproved":
            // your noapproved query
            echo "noapprove article"; 
            break;
        case "sortthem": 
            // your sortthem query
            echo "sort article "; 
            break;
        default:
            // default query but as you know what the 
            // values are going to be a little unneeded 
            // but its good practice to have it anyway  
            echo "default article query"; 
    }
}
if(!empty($_POST['Chk1'])){
    $article = $_POST['Chk1'];
    $option = $_POST['select1'];

    $action = getActionFromSelect($option);
    echo $action, ' ', $article;

}elseif(!empty($_POST['Chk2'])){
    $article = $_POST['Chk2'];
    $option = $_POST['select2'];

    $action = getActionFromSelect($option);
    echo $action, ' ', $article;

}
?>
<form action="" method="post">
    <div id="field">
        <input type="checkbox" id="Chk1" name="Chk1" value="1"/>
        <select id="select1" name="select1">
            <option value="delete">delete</option>
            <option value="approved">approved</option>
            <option value="noapproved">noapproved</option>
            <option value="sortthem">sort</option>
        </select>
    </div>
    <div id="field">
        <input type="checkbox" id="Chk2" name="Chk2" value="2"/>
        <select id="select2" name="select2">
            <option value="delete">delete</option>
            <option value="approved">approved</option>
            <option value="noapproved">noapproved</option>
            <option value="sortthem">sort</option>
        </select>
    </div>
    <input type="submit" value="submit"/>
</form>

switch编辑成函数以缩短所需的代码量