是否可能复选框获胜';t检查数据库中的行中是否有值


Is it Possible checkbox won't check if there a value in row from database?

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

这是我的示例表

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>

从外观上看,它不需要通过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";
     }
}

这里有一个非常简单的代码更改复选框,添加了一个条件&禁用复选框上的属性&发送完整的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>';
    }
}

代码没有经过测试,但肯定会起作用。