我如何禁用其他选中的选中项以防止其他用户再次选择它

时间:2019-11-28 06:45:08

标签: php laravel

这就是我将复选框保存到数据库的方式。我不希望该复选框在数据库中存在两次

这是如果用户选择一个复选框,则另一个用户不能再次选择同一复选框

2 个答案:

答案 0 :(得分:0)

从屏幕截图中我了解到,您不想在同一部门的同一部门分配多位讲师。

因此,在创建allocated行之前,您需要检查那些条目是否已经存在。

$data = compact('course_id', 'department', 'unit');
// check the department and unit already assigned or not. 
if(!allocated::where($data)->exists()){
   $data['lecturers_name'] = $lecturers_name;
   allocated::create($data);
}

答案 1 :(得分:-1)

对于初学者,您没有提供任何代码示例。因此,我以后的示例将不会基于您的代码。

您可以禁用一个复选框,但是如果有人真的愿意,可以很容易地忽略它。检查器可用于更改html,以便禁用复选框被启用。当然,您可以在后端对此进行验证。

我将选择仅显示其值尚未被其他人选中的复选框。我的示例包含两种解决方案。

// create connection with your database.
$conn = new mysqli($servername, $username, $password, $dbname);

// array with values that can be picked.
$values = ['Apple', 'Banana', 'Lemon'];

// loop trough the array.
foreach( $values as $fruit ) 
{
    // select query -> check if someone else already choose the fruit.
    $sql = "SELECT chosen_fruit FROM friends WHERE chosen_fruit = " . $fruit;

    // execute the query.
    $result = $conn->query($sql);

    // if no results are found, meaning nobody else have picked this fruit yet.
    if ($result->num_rows == 0) {
        // enabled checkbox that is free to use
        echo '<input type="checkbox" name="chosen_fruit" value="' . $fruit . '">'
    } else {
        // disabled checkbox because this fruit has already been chosen.
        echo '<input type="checkbox" name="chosen_fruit" value="' . $fruit . '" disabled>'
    }
}

编辑:发布答案后,我看到了您的编辑。