表单 - 具有相同"名称"的按钮组值

时间:2018-02-23 08:23:00

标签: php html forms

我尝试使用一些在我的数据库中插入数据的输入来创建表单,问题是 - 输入之一(名称为&#34的按钮组;优先级" PHP解释器无法找到它们。我应该把这个名字?我认为这很糟糕......这是我的代码:

<form action="" method="post">
    <div class="form-group">
    <strong><label for="towho">Do kogo:</label></strong>
    <select class="form-control" name="towho">
        <option> - - - </option>
        <!-- Skrypt pobierania danych (username) do Selecta -->
        <?php
            get_users();
        ?>
    </select>
    </div>
    <hr></hr>
    <div class="form-group">
        <label for="usr">Temat:</label>
        <input type="text" class="form-control" name="topic">
    </div>
    <hr></hr>
        <div class="form-group" style="padding-top:10px">
            <strong><label for="comment">Opis zadania:</label></strong>
            <textarea class="form-control" rows="5" name="comment"></textarea>
        </div>
        <hr></hr>
        <center>
        <div class="form-group">
            <strong><p>Priorytet:</p></strong>
            <div class="btn-group btn-group-justified" role="group" aria-label="Justified button group">
                <div class="btn-group" role="group">
                    <input type="button" class="btn btn-danger" value="WYSOKI" name="priority">
                </div>
                <div class="btn-group" role="group">
                    <input type="button" class="btn btn-warning" value="ŚREDNI" name="priority">
                </div>
                <div class="btn-group" role="group">
                    <input type="button" class="btn btn-success" value="NISKI" name="priority">
                </div>
            </div>
        </div>
        </center>
        <hr></hr>
        <div class="form-group">
            <button name="submit" type="worksubmit" class="btn btn-info btn-block">Zaakceptuj</button>
        </div>
    </form>

这是我的PHP代码:

if(isset($_POST['submit'])) {
include 'db.php';

$sf = "INSERT INTO zadania (id, user, tablename, dodano, dokogo, tytul, opis, priorytet, zaakceptowano, status) VALUES (NULL, '".$_SESSION['valid_user']."', 'zadania', '".date('d.m.Y')."', '".$_POST['towho']."', '".$_POST['topic']."', '".$_POST['comment']."', '".$_POST['priority']."', '2018-01-01 1:00:00', 'start')";

mysqli_query($conn,$sf);
echo "Succes";

一切正常,只有&#34;优先&#34;不起作用。它在我的INPUT查询中显示 UNDEFINED INDEX&#34; priority&#34; 。另外......我的按钮在整个表单上可能不宽,它们的宽度由字符定义,但我的意图却不同。

1 个答案:

答案 0 :(得分:0)

使用JQUERY更改名为priority的隐藏输入字段的值,并在按下任一按钮时更改该值。

$(document).ready(function () {
  $("#priority1").click(function(){
    $("#priority").attr("value", "WYSOKI");
  });
  $("#priority2").click(function(){
    $("#priority").attr("value", "ŚREDNI");
  });
  $("#priority3").click(function(){
    $("#priority").attr("value", "NISKI");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post">
    <div class="form-group">
    <strong><label for="towho">Do kogo:</label></strong>
    <select class="form-control" name="towho">
        <option> - - - </option>
        <!-- Skrypt pobierania danych (username) do Selecta -->
        <?php
            get_users();
        ?>
    </select>
    </div>
    <hr></hr>
    <div class="form-group">
        <label for="usr">Temat:</label>
        <input type="text" class="form-control" name="topic">
    </div>
    <hr></hr>
        <div class="form-group" style="padding-top:10px">
            <strong><label for="comment">Opis zadania:</label></strong>
            <textarea class="form-control" rows="5" name="comment"></textarea>
        </div>
        <hr></hr>
        <center>
        <div class="form-group">
            <strong><p>Priorytet:</p></strong>
            <input type="text" value="" id="priority" name="priority" hidden>
            <div class="btn-group btn-group-justified" role="group" aria-label="Justified button group">
                <div class="btn-group" role="group">
                    <input type="button" class="btn btn-danger" value="WYSOKI" id="priority1">
                </div>
                <div class="btn-group" role="group">
                    <input type="button" class="btn btn-warning" value="ŚREDNI" id="priority2">
                </div>
                <div class="btn-group" role="group">
                    <input type="button" class="btn btn-success" value="NISKI" id="priority3">
                </div>
            </div>
        </div>
        </center>
        <hr></hr>
        <div class="form-group">
            <button name="submit" type="worksubmit" class="btn btn-info btn-block">Zaakceptuj</button>
        </div>
    </form>