是否可能复选框不会检查数据库中的行是否有值?

时间:2013-11-20 02:38:42

标签: javascript php jquery checkbox mysqli

是否可能复选框不会检查数据库中的行是否有值?

这是我的示例表

item_name | PR     | checkbox  |

ballpen   | pr100  |     □     |
pencil    | pr111  |     □     |
Paper     | pr500  |     □     |
Clip      |        |     □     |

< Edit > < Add >

示例item_name Clip没有PR,当我检查Clip行并点击Edit时,它将不允许检查或保留取消选中。如果数据库中有值,我想基于Edit按钮进行验证。

这是我的短代码

app_list2.php

  <?php
if (isset($_POST['submit'])) {
    $mysqli = new mysqli("localhost", "root", "", "app");

    //unset the session value 
    $_SESSION['content'] = '';

    $drop     = $_POST['drop_1'];
    $tier_two = $_POST['tier_two'];

    $where = "WHERE a.app_cn='$drop' AND a.app_plan_no='$tier_two'";

    $result1 = $mysqli->query("
    SELECT a.*, a.counter as sucks, b.counter, b.pr
    FROM app a
    LEFT OUTER JOIN purchase_request b
    ON a.counter=b.counter
    $where 
    GROUP BY a.counter ORDER BY a.item_name
");
?>
  <?php
    echo '<table id="tfhover" cellspacing="0" class="tablesorter" style="text-transform:uppercase;">
        <thead>
            <tr>
        <th rowspan="2">PR</th>
        <th rowspan="2" id="none"></th>
        </tr>
        </thead>';
    echo '<tbody>';
    $i = 1;
    while ($row = $result1->fetch_assoc()) {
        if ($row['app_cn'] != '') {
            echo '<tr>
            <td>' . $i++ . '</td>
            <td>' . $row['item_name'] . '</td>
            <td>' . $row['pr'] . '</td>
            <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['sucks'] . '"></td>
       </tr>';
        }
    }
    echo "</tbody></table>";
    $sPageContent        = ob_get_clean();
    $_SESSION['content'] = $sPageContent;
    echo $_SESSION['content'];
} else {
    if (isset($_SESSION['content'])) {
        echo $_SESSION['content'];
    }
}
?>
  <a class="button" href="javascript:document.forms[0].submit();" onclick="f1.action='editpr.php'; return true;"><span><b>Edit Purchase Request</b></span></a>

  <a class="button" href="javascript:document.forms[0].submit();" onclick="f1.action='addpr.php'; return true;"><span><b>Add to Purchase Request</b></span></a>
</form>

2 个答案:

答案 0 :(得分:0)

从它的外观来看,它不需要通过JavaScript或jQuery来完成。

在输入复选框中设置disabled参数:DEMO

<input name="checkbox1" type="checkbox" id="checkbox1" value="XYZ" disabled />

http://www.w3.org/TR/html-markup/input.checkbox.html

我不知道php所以无法帮助你,但伪代码:

// in your while loop - replace the existing line of code
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['sucks'] . '" ' . elementDisabled($row['pr']) .'/></td>

// function to test whether PR is empty
// put this on the line after the second <?php
function elementDisabled($input)
{
     if  (!isset($input) || trim($input)==='')
     {
         return "disabled";
     }
}

答案 1 :(得分:0)

这是一个非常简单的代码更改复选框,添加了一个条件&amp;禁用复选框上的属性&amp;发送完整的while循环:

while ($row = $result1->fetch_assoc()) {
    if ($row['app_cn'] != '') {
        echo '<tr>
        <td>' . $i++ . '</td>
        <td>' . $row['item_name'] . '</td>
        <td>' . $row['pr'] . '</td>
        <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['sucks'] . '"'.($row['item_name'] == ""?"disabled ":"").'></td>
   </tr>';
    }
}

代码未经过测试但肯定会有效。