【问题标题】:Is it Possible checkbox won't check if there a value in row from database?是否可能复选框不会检查数据库中的行中是否有值?
【发布时间】:2013-11-20 02:38:42
【问题描述】:

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

这是我的示例表

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 php jquery checkbox mysqli


    【解决方案1】:

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

    【讨论】:

    • 有PR的项目怎么样?如果所有复选框都被禁用,我将无法编辑
    • 当然,在您的while loop 中,您需要检查每个循环的 PR。如果没有 PR,则将 disabled 添加到输入。我会添加一些伪代码,因为我不知道 php
    • 我在 elementDisabled($row['pr']) 中出现错误,我在哪里包含功能代码?
    • 并不奇怪,它是伪代码,即不是真正的代码。我不是 php 开发人员,所以我不知道确切的语法。在谷歌搜索后,我已经尽我所能进行了编辑,试试吧。还看:tutorialspoint.com/php/php_functions.htm如果这不起作用,希望其他人能提供帮助
    • 感谢您的帮助。我仍然希望有人可以帮助我解决这个问题。
    【解决方案2】:

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

    代码未经测试,但肯定可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2021-07-16
      • 2018-02-09
      • 1970-01-01
      相关资源
      最近更新 更多